private AnswerDetailViewModel ConstructAnswerDetailViewModel(Answer answer) { var question = _unitOfWork.QuestionRepository.Single(q => q.Id == answer.Question.Id, nameof(Question.Topics)); var answerCount = _unitOfWork.AnswerRepository.Count(a => a.QuestionId == question.Id); var moreAnswers = _unitOfWork.AnswerRepository.Get(a => a.Id != answer.Id && a.QuestionId == question.Id, includeProperties: nameof(Answer.AnswerLikes) + "," + nameof(Answer.Comments), take: 2) .ToList(); // TODO: can we utilize ConstructQuestionDetailViewModel method in QuestionController? var userId = User.Identity.GetUserId(); var existingAnswer = _unitOfWork.AnswerRepository.SingleOrDefault( a => a.QuestionId == question.Id && a.AppUserId == userId); answer.SetLikedByCurrentUser(userId); moreAnswers.ForEach(ma => ma.SetLikedByCurrentUser(userId)); var questionDetailViewModel = new QuestionDetailViewModel { Question = question, CanUserEditQuestion = question.CanUserModify(User), UserAnswerId = existingAnswer?.Id ?? 0, CanUserDeleteAnswerPanelAnswer = existingAnswer != null, }; var viewModel = new AnswerDetailViewModel { Answer = answer, MoreAnswers = moreAnswers, QuestionDetailViewModel = questionDetailViewModel, AnswerCount = answerCount }; return(viewModel); }
// Forms the response object needs to sent. private async Task <AnswerDetailViewModel> GetAnswerDetailResponseReponseAsync(Answer answer) { var answerDetailViewModel = new AnswerDetailViewModel(); answerDetailViewModel.Id = answer.Id; answerDetailViewModel.Answer = answer.AnswerText; answerDetailViewModel.Votes = answer.Votes; answerDetailViewModel.Author = answer.Author; answerDetailViewModel.AuthorPoints = await this.usersRepository.GetPointsAsync(answer.Author); answerDetailViewModel.CreatedAt = answer.CreatedAt; answerDetailViewModel.UpdatedAt = answer.UpdatedAt; answerDetailViewModel.IsAccepted = answer.IsAccepted; answerDetailViewModel.SelfVote = (await this.answerVoteRepository.FetchPreviousVote(answer.Id, this.currentUser))?.Vote; answerDetailViewModel.SelfVoted = await this.answerVoteRepository.HasVotedAsync(answer.Id, this.currentUser); answerDetailViewModel.Comments = await this.commentsRepository.GetEntity() .Where(x => x.AnswerId == answer.Id) .Select(x => new CommentsViewModel { Id = x.Id, Comment = x.Comment, Author = x.Author, CommentedAt = x.CreatedAt }).ToPaginateAsync(0, 5); return(answerDetailViewModel); }