示例#1
0
        public IActionResult AddDescription(AnswerDescriptionDto answerDescription)
        {
            // Basic checks first
            if (answerDescription == null || answerDescription.AnswerId <= 0 ||
                string.IsNullOrEmpty(answerDescription.Description) ||
                string.IsNullOrWhiteSpace(answerDescription.Description))
            {
                return(View("Error"));
            }

            // todo: figure out how to protect from spam posts besides antiforgery

            // cleanup the input
            answerDescription.Description = Services.TextCleaner.Clean(answerDescription.Description);
            // Clean up the endings
            answerDescription.Description = answerDescription.Description.TrimEnd(new Char[] { ' ', '\n', '\r' });

            // Let's first check for profanities.
            var profanityCheckResult = _profanityService.CheckProfanity(answerDescription.Description, this.Culture);

            if (profanityCheckResult.HasIssues)
            {
                // answer.ErrorMessage = profanityCheckResult.ErrorMessage;
                // todo: settle on displaying errors from controller posts and gets
                // Can't use error message from profanity object because it is not localized.
                // Have to localize ourself
                // Might have to move this to profanity service.
                var errorData = new ErrorViewModel();
                errorData.AddError(profanityCheckResult.ErrorMessage);

                return(View("Error", errorData));
            }

            // Save the user in case we need statistics update
            ApplicationUserDto user = null;

            // Load user if he is logged in
            if (User.Identity.IsAuthenticated && User.Identity.Name != null)
            {
                user = _userService.FindByUserName(User.Identity.Name);
            }
            // Set the user id in the answer if user is found
            if (user != null)
            {
                answerDescription.UserId = user.UserId;
                // Check if user statistics is loaded
                _statisticsService.LoadUserStatictics(user);
            }

            // Add answer description
            var operationResult = _answerDescriptionService.AddAnswerDescription(answerDescription);

            // Create the object for passing data between controllers.
            var navigationData = new NavigationDataDto();

            // Need to update user stats if new answer was added
            if (operationResult.IsNew && user != null)
            {
                user.NumberOfDescriptions++;
                navigationData.UserLevelingResult = _userService.LevelUser(user, EventType.AnswerDescriptionAdded);
            }

            // Read the reason to return it to UI.
            navigationData.Reason   = _resourcesService.GetString(this.Culture, Lines.DESCRIPTION_WAS_ADDED_SUCCESSFULLY);
            navigationData.AnswerId = answerDescription.AnswerId;

            // Redirect to show the answer. This will prevent user refreshing the page.
            return(RedirectToAction("ShowAnswer", new { data = NavigationHelper.Encode(navigationData) }));
        }