/// <summary> /// Save answer vote /// </summary> /// <param name="voteVote"></param> /// <returns>Id of the answer whos description was voted.</returns> public DataOperationResult VoteAnswer(AnswerVoteDto answerVote) { if (answerVote == null) { throw new ServicesException("Null parameter VoteService.VoteAnswer(answerVote)"); } if (answerVote.AnswerId <= 0) { throw new ServicesException("Unexpected AnswerId in VoteService.VoteAnswer(answerVote)"); } if (answerVote.UserId == null) { throw new ServicesException("Unexpected UserId in VoteService.VoteAnswer(answerVote)"); } var result = new DataOperationResult(); // Find if vote is already there. var existingVote = _answerVoteRepository.Queryable() .FirstOrDefault(x => x.UserId == answerVote.UserId && x.AnswerId == answerVote.AnswerId); // Do not re-add existing vote. if (existingVote != null) { result.IntId = existingVote.AnswerId; return(result); } // Add new answer vote var answerVoteObject = new AnswerVote(); answerVoteObject.FromDto(answerVote); _answerVoteRepository.Insert(answerVoteObject); var task = _answerVoteRepository.SaveChangesAsync(); task.Wait(); // Add to cache. var cachedData = GetVotesCachedData(); cachedData.Insert(answerVoteObject); // Add to user cache if there is a user if (answerVoteObject.UserId != null) { var userCachedData = GetUserVotesCachedData(); userCachedData.Insert(new AnswerVoteUserMask(answerVoteObject)); } result.IntId = answerVoteObject.AnswerId; result.IsNew = true; return(result); }
/// <summary> /// Save answer description flag /// </summary> /// <param name="answerFlag"></param> /// <returns></returns> /// <remarks>Flag is always considered new</remarks> public DataOperationResult FlagAnswerDescription(AnswerDescriptionFlagDto answerDescriptionFlag) { if (answerDescriptionFlag == null) { throw new ServicesException("Null parameter FlagService.FlagAnswerDescription(answerDescriptionFlag)"); } if (answerDescriptionFlag.AnswerDescriptionId <= 0) { throw new ServicesException("Unexpected AnswerDescriptionId in FlagService.FlagAnswerDescription(answerDescriptionFlag)"); } if (answerDescriptionFlag.UserId == null) { throw new ServicesException("Unexpected UserId in FlagService.FlagAnswerDescription(answerDescriptionFlag)"); } var answerDescriptionFlagObject = new AnswerDescriptionFlag(); answerDescriptionFlagObject.FromDto(answerDescriptionFlag); _answerDescriptionFlagRepository.Insert(answerDescriptionFlagObject); var task = _answerDescriptionFlagRepository.SaveChangesAsync(); task.Wait(); // Add to user cache if there is a user if (answerDescriptionFlagObject.UserId != null) { var userCachedData = GetUserDescriptionFlagsCachedData(); userCachedData.Insert(answerDescriptionFlagObject); } // Find the id of the answer whos description was flagged // We need to give it caller so that caller can redirect to answer page var answerDescriptionDto = _answerDescriptionService .FindByAnswerDescriptionId(answerDescriptionFlag.AnswerDescriptionId); var result = new DataOperationResult(); result.IntId = answerDescriptionDto.AnswerId; result.IsNew = true; return(result); }