public async Task <AddedAnswerDto> AddAnswer(AnswerDto answer) { var answerObject = new Answer(); answerObject.FromDto(answer); // Repository might get a different object back. // We will also let repository do the counting. Repository increases the count. var persistResult = await PersistAnswer(answerObject); // Add to cache. var cachedData = GetCachedData(); cachedData.Insert(persistResult.Answer); // Add to left cache var leftCachedData = GetLeftCachedData(); leftCachedData.Insert(new AnswerLeftMask(persistResult.Answer)); // Add to right cache var rightCachedData = GetRightCachedData(); rightCachedData.Insert(new AnswerRightMask(persistResult.Answer)); // Add to "answer by user" cache if there is a user if (persistResult.Answer.UserId != null) { var userCachedData = GetUserCachedData(); userCachedData.Insert(new AnswerUserMask(persistResult.Answer)); } // Add to trending today AddToTrendingToday(persistResult.Answer); // Add to thrending overall AddToTrendingOverall(persistResult.Answer); var result = new AddedAnswerDto() { Answer = persistResult.Answer.ToDto() }; // Make sure we let the caller know that data is new. // We can not update user statistics here. result.IsNew = persistResult.IsNew; return(result); }
/// <summary> /// Check answer data received from user for profanity. /// Set the error message in the answer object if anything found. /// </summary> /// <param name="service"></param> /// <param name="answer"></param> /// <returns>true if profanity found</returns> private bool CheckProfanity(IProfanityService service, AddedAnswerDto answer) { var profanityCheckResult = service.CheckProfanity(answer.Answer.LeftWord); if (profanityCheckResult.HasIssues) { answer.ErrorMessage = profanityCheckResult.ErrorMessage; return(true); } profanityCheckResult = service.CheckProfanity(answer.Answer.RightWord); if (profanityCheckResult.HasIssues) { answer.ErrorMessage = profanityCheckResult.ErrorMessage; return(true); } profanityCheckResult = service.CheckProfanity(answer.Answer.Phrase); if (profanityCheckResult.HasIssues) { answer.ErrorMessage = profanityCheckResult.ErrorMessage; return(true); } return(false); }
public async Task <AddedAnswerDto> AddAnswer(AnswerDto answer) { var result = new AddedAnswerDto() { Answer = answer }; // Basic checks first if (result.Answer == null) { return(SetErrorMessage(result, "Incoming data is null.")); } // This might throw exception if there was a header but invalid. But if someone is just messing with us we will return nothing. if (!ParseAntiForgeryHeader(_antiforgery, result, HttpContext)) { return(result); } // Let's first check for profanities. bool gotProfanityIssues = CheckProfanity(_profanityService, result); if (gotProfanityIssues) { return(result); } // Add left word and right word to suggestions var addedSuggestion = _suggestionService.AddSuggestion(new SuggestionDto() { Phrase = answer.LeftWord }); // Add the right word if different from left if (answer.LeftWord != answer.RightWord) { addedSuggestion = _suggestionService.AddSuggestion(new SuggestionDto() { Phrase = answer.RightWord }); } // 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) { answer.UserId = user.UserId; // Check if user statistics is loaded _statisticsService.LoadUserStatictics(user); } // Add answer. This will not touch user's cache or user service. result = await _answerService.AddAnswer(answer); // Need to update user stats if new answer was added if (result.IsNew && user != null) { user.NumberOfAnswers++; result.UserLevelingResult = _userService.LevelUser(user, EventType.AnswerAdded); } return(result); }
private AddedAnswerDto SetErrorMessage(AddedAnswerDto answer, string errorMessage) { answer.ErrorMessage = errorMessage; return(answer); }