Beispiel #1
0
        public static String GetUserStatsForGUI(XsUser user)
        {
            String userStatsForGUI = String.Empty;

            if (null != user)
            {
                userStatsForGUI = String.Concat(user.QuestionCount.ToString(), " x Q | ", user.AnswerCount.ToString(), " x A");
            }

            return userStatsForGUI;
        }
Beispiel #2
0
        public static XsUser GetUserByUsername(String ntUsername)
        {
            XsUser user = null;
            if (!String.IsNullOrEmpty(ntUsername.Trim()))
            {
                IXsUserRepository userRepository = new UserRepository();
                user = userRepository.GetByUsername(ntUsername);

                if (null == user)
                {
                    user = new XsUser();
                    user.Username = ntUsername;
                    user.DisplayName = ntUsername;
                    user = userRepository.Save(user);
                }
            }

            return user;
        }
Beispiel #3
0
 public static String GetDisplayNameForGUI(XsUser user)
 {
     String displayNameForGUI = "<Anonymous>";
     if (null != user)
     {
         displayNameForGUI = user.DisplayName;
         if (String.IsNullOrEmpty(displayNameForGUI))
         {
             if (!String.IsNullOrEmpty(user.FirstName))
             {
                 displayNameForGUI = user.FirstName;
             }
             else if (!String.IsNullOrEmpty(user.LastName))
             {
                 displayNameForGUI = user.LastName;
             }
             displayNameForGUI = user.Username;
         }
     }
     return displayNameForGUI;
 }
        public ActionResult Answer(long ID, String AnswerContent)
        {
            XsQuestion questionToUpdate = null;

            try
            {
                questionToUpdate = questionRepository.GetById(ID);
            }
            catch (Exception ex)
            {
                log.Error(String.Format("Question GetById({0}) error: ", ID) + ex.Message);
            }

            if (null == questionToUpdate)
            {
                ViewData["EntityID"] = ID;
                return View("NotFound");
            }
            else
            {
                try
                {
                    XsAnswer answer = new XsAnswer();
                    answer.CreationDT = DateTime.UtcNow;
                    answer.UpdateDT = DateTime.UtcNow;
                    answer.Content = AnswerContent.Replace("<script", "[script").Replace("</script>", "[/script]");
                    answer.ContentHtml = new Markdown().Transform(answer.Content);

                    XsUser author = userRepository.GetByUsername(User.Identity.Name);

                    if (null == author)
                    {
                        author = new XsUser();
                        author.Username = User.Identity.Name;
                        author.AnswerCount = 1;
                    }
                    author.AnswerCount++;
                    answer.Author = author;

                    questionToUpdate.AddAnswer(answer);
                    questionRepository.Save(questionToUpdate);
                }
                catch (Exception ex)
                {
                    log.Error("There was an error when saving the question in the DB " + ex.Message + "Inner Exception " + ex.InnerException);
                    ViewData["ErrorMessage"] = "There was an error when saving the question in the DB!";
                    return View("Error");
                }
            }

            return RedirectToAction("Details", new { id = ID, seoName = questionToUpdate.SlugTitle });
        }
        public ActionResult Create(XsQuestion questionToCreate, String button, String QuestionTags)
        {
            questionToCreate.CreationDT = DateTime.UtcNow;
            questionToCreate.UpdateDT = DateTime.UtcNow;

            if (button.Equals("SaveDraft"))
            {
                questionToCreate.Status = XsStatus.Draft;
            }
            else
            {
                questionToCreate.Status = XsStatus.Published;
                questionToCreate.PublishedDT = DateTime.UtcNow;
            }

            String tags = RemoveExtraSpaces(QuestionTags);

            String[] qTags = tags.Split(' ');

            foreach (String tag in qTags)
            {
                if (!String.IsNullOrEmpty(tag))
                {
                    String tagInvariantName = tag.ToLowerInvariant();

                    XsTag currentTag = tagRepository.GetTagByName(tagInvariantName);

                    if (null == currentTag)
                    {
                        currentTag = new XsTag();
                        currentTag.Name = tagInvariantName;
                    }
                    questionToCreate.AddTag(currentTag);
                }
            }
            questionToCreate.Content = questionToCreate.Content.Replace("<script", "[script").Replace("</script>", "[/script]");
            questionToCreate.ContentHtml = new Markdown().Transform(questionToCreate.Content);
            questionToCreate.Excerpt = Utils.CreateContentExcerpt(questionToCreate.ContentHtml);

            ModelState.AddModelErrors(questionToCreate.GetRuleViolations());

            if (ModelState.IsValid)
            {
                try
                {
                    //TODO: to add other article attributes
                    XsUser author = userRepository.GetByUsername(User.Identity.Name);

                    if (null == author)
                    {
                        author = new XsUser();
                        author.Username = User.Identity.Name;
                        author.QuestionCount = 1;
                    }
                    author.QuestionCount++;
                    questionToCreate.Author = author;
                    questionRepository.Save(questionToCreate);

                    ViewData["QuestionTitle"] = questionToCreate.Title;
                    return View("Confirm");
                }
                catch (Exception ex)
                {
                    log.Error("There was an error when saving the question in the DB " + ex.Message + "Inner Exception " + ex.InnerException );
                    ViewData["ErrorMessage"] = "There was an error when saving the question in the DB!";
                    return View("Error");

                }
            }
            return View();
        }