public IActionResult AddAnswer(AddAnswerViewModel addAnswerViewModel)
        {
            // Basic checks first
            if (addAnswerViewModel == null)
            {
                return(View("Error"));
            }
            if (!ModelState.IsValid)
            {
                return(View("Error"));
            }

            // Create answer controller because we will just use it
            var answerController = new AnswerController(_answerService, _profanityService, _suggestionService,
                                                        _userService, _antiforgery, _statisticsService)
            {
                ControllerContext = new ControllerContext()
                {
                    HttpContext = this.HttpContext
                }
            };

            // Create object to post
            var answer = new AnswerDto()
            {
                LeftWord  = addAnswerViewModel.LeftWord,
                RightWord = addAnswerViewModel.RightWord,
                Phrase    = addAnswerViewModel.Phrase,
                UserId    = this.GetUserId(this.User, _userService)
            };

            // Roll the dice and post.
            var task = answerController.AddAnswer(answer);

            task.Wait();
            var resultAnswer = task.Result;

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

            // Read the reason to return it to UI.
            // All good
            if (resultAnswer.ErrorMessage == null)
            {
                navigationData.Reason             = _resourcesService.GetString(this.Culture, Lines.OPINION_WAS_ADDED_SUCCESSFULLY);
                navigationData.AnswerId           = resultAnswer.Answer.Id;
                navigationData.UserLevelingResult = resultAnswer.UserLevelingResult;

                // Redirect to show the answer. This will prevent user refreshing the page.
                return(RedirectToAction("ShowAnswer", "AnswerAction", new { data = NavigationHelper.Encode(navigationData) }));
            }
            // All bad
            else
            {
                navigationData.Reason = string.Format(_resourcesService.GetString(this.Culture, Lines.OPINION_WAS_NOT_ADDED), resultAnswer.ErrorMessage);
                return(RedirectToAction("Index", "Home", new { reason = navigationData.Reason }));
            }
        }
示例#2
0
        public async Task <IActionResult> Edit(int id = 0)
        {
            // Create the object for passing data between controllers.
            var navigationData = new NavigationDataDto();

            navigationData.AnswerId = id;
            var nav = new { data = NavigationHelper.Encode(navigationData) };

            if (id <= 0)
            {
                return(RedirectToAction("ShowAnswer", nav));
            }

            // Let's load the answer.
            // The hope is that service will not have to go to the database and load answer from cache.
            // But please look at the servise implementation for details.
            var answer = await _answerService.FindByAnswerId(id);

            // No user, no reason to edit.
            if (answer.UserId == null)
            {
                return(RedirectToAction("ShowAnswer", nav));
            }

            // Not logged in, can not edit
            // 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);
            }
            if (user == null)
            {
                return(RedirectToAction("ShowAnswer", nav));
            }

            // Kick them if answer was added not by the current user
            // This prevents going directly to the answer
            if (answer.UserId != user.UserId)
            {
                return(RedirectToAction("ShowAnswer", nav));
            }

            return(View(answer));
        }
示例#3
0
        public async Task <AdminAnswerViewModel> FillInTheAnswer(int answerId)
        {
            var answer = await _answerService.FindByAnswerId(answerId);

            // Load descriptions directly from database
            var descriptions = _answerDescriptionService.FindDirectByAnswerId(answerId);

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

            navigationData.AnswerId = answerId;

            var model = new AdminAnswerViewModel()
            {
                Answer             = answer,
                AnswerDescriptions = descriptions,
                NavigationData     = NavigationHelper.Encode(navigationData)
            };

            return(model);
        }
        public IActionResult FlagAnswer(int answerId = 0)
        {
            _logger.LogDebug("FlagAnswer answerId = " + answerId);

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

            navigationData.AnswerId = answerId;

            // Only do something if answer id is not zero
            if (answerId <= 0)
            {
                // Redirect to some random answer that might even not exist
                return(RedirectToAction("ShowAnswer", "AnswerAction", new { data = NavigationHelper.Encode(navigationData) }));
            }

            var user = _userService.FindByUserName(User.Identity.Name);

            // Check if user statistics is loaded
            _statisticsService.LoadUserStatictics(user);

            var result = _flagService.FlagAnswer(
                new AnswerFlagDto()
            {
                AnswerId = answerId, UserId = user.UserId
            }
                );

            if (result.IsNew)
            {
                user.NumberOfFlags++;
                navigationData.UserLevelingResult = _userService.LevelUser(user, EventType.AnswerFlagAdded);
            }

            // Read the reason
            navigationData.Reason = _resourcesService.GetString(this.Culture, Lines.THANK_YOU_FOR_FLAGING);

            return(RedirectToAction("ShowAnswer", "AnswerAction", new { data = NavigationHelper.Encode(navigationData) }));
        }
示例#5
0
        public async Task <IActionResult> Edit(AnswerDto answer)
        {
            // Basic checks first
            if (answer == null || answer.Id <= 0)
            {
                return(View("Error"));
            }

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

            navigationData.AnswerId = answer.Id;
            var nav = new { data = NavigationHelper.Encode(navigationData) };

            if (!ModelState.IsValid)
            {
                return(RedirectToAction("ShowAnswer", nav));
            }

            // Find the answer that needs changes
            var answerToModify = await _answerService.FindByAnswerId(answer.Id);

            // No user, no reason to edit.
            if (answerToModify.UserId == null)
            {
                return(RedirectToAction("ShowAnswer", nav));
            }

            // Not logged in, can not edit
            // 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);
            }
            if (user == null)
            {
                return(RedirectToAction("ShowAnswer", nav));
            }

            // Kick them if answer was added not by the current user
            // This prevents going directly to the answer
            if (answerToModify.UserId != user.UserId)
            {
                navigationData.AnswerId = answerToModify.Id;
                nav = new { data = NavigationHelper.Encode(navigationData) };
                return(RedirectToAction("ShowAnswer", nav));
            }

            // Compare the categories because so far this is all we can edit.
            string newCategory = (answer.Category + "").ToLower();
            string oldCategory = (answerToModify.Category + "").ToLower();

            // only do update if changed
            if (oldCategory != newCategory)
            {
                answerToModify.Category = answer.Category;

                // Update answer
                var updatedAnswer = await _answerService.UpdateAnswer(answerToModify);
            }

            // Read the reason
            navigationData.Reason = _resourcesService.GetString(this.Culture, Lines.THANK_YOU_FOR_IMPROVING);

            // Redirect to show the answer. This will prevent user refreshing the page.
            return(RedirectToAction("ShowAnswer", new { data = NavigationHelper.Encode(navigationData) }));
        }
示例#6
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) }));
        }