/// <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); }
/// <summary> /// Save answer description Vote /// </summary> /// <param name="answerVote"></param> /// <returns>Id of the answer whos description was voted.</returns> public DataOperationResult VoteAnswerDescription(AnswerDescriptionVoteDto answerDescriptionVote) { if (answerDescriptionVote == null) { throw new ServicesException("Null parameter VoteService.VoteAnswerDescription(answerDescriptionVote)"); } if (answerDescriptionVote.AnswerDescriptionId <= 0) { throw new ServicesException("Unexpected AnswerDescriptionId in VoteService.VoteAnswerDescription(answerDescriptionVote)"); } if (answerDescriptionVote.UserId == null) { throw new ServicesException("Unexpected UserId in VoteService.VoteAnswerDescription(answerDescriptionVote)"); } var result = new DataOperationResult(); // Find if vote is already there. var existingVote = _answerDescriptionVoteRepository.Queryable() .FirstOrDefault(x => x.UserId == answerDescriptionVote.UserId && x.AnswerDescriptionId == answerDescriptionVote.AnswerDescriptionId); // Do not re-add existing vote. if (existingVote != null) { result.IntId = existingVote.Id; return(result); } // Add new description vote var answerDescriptionVoteObject = new AnswerDescriptionVote(); answerDescriptionVoteObject.FromDto(answerDescriptionVote); // Insert _answerDescriptionVoteRepository.Insert(answerDescriptionVoteObject); var task = _answerDescriptionVoteRepository.SaveChangesAsync(); task.Wait(); // Add to cache. var cachedData = GetVoteDescriptionsCachedData(); cachedData.Insert(answerDescriptionVoteObject); // Add to user cache if there is a user if (answerDescriptionVoteObject.UserId != null) { var userCachedData = GetUserVoteDescriptionsCachedData(); userCachedData.Insert(new AnswerDescriptionVoteUserMask(answerDescriptionVoteObject)); } // Find the id of the answer whos description was voted for var answerDescriptionDto = _answerDescriptionService .FindByAnswerDescriptionId(answerDescriptionVote.AnswerDescriptionId); result.IntId = answerDescriptionDto.AnswerId; result.IsNew = true; return(result); }