public GetQuestionOutput GetQuestion(GetQuestionInput input) { var question = _questionRepository .GetAll() .Include(q => q.CreatorUser) .Include(q => q.Answers) .Include("Answers.CreatorUser") .FirstOrDefault(q => q.Id == input.Id); if (question == null) { throw new UserFriendlyException("There is no such a question. Maybe it's deleted."); } if (input.IncrementViewCount) { question.ViewCount++; } return(new GetQuestionOutput { Question = question.MapTo <QuestionWithAnswersDto>() }); }
//Getting a single question based on the id Provided public GetQuestionOutput GetQuestion(GetQuestionInput input) { // We will be including the answers and the users who have voted for this question var question = _questionRepository .GetAll() .Include(q => q.CreatorUser) .Include(q => q.Answers) .Include(q => q.UsersVoted) .Include("Answers.CreatorUser") .FirstOrDefault(q => q.Id == input.Id); if (question == null) { throw new UserFriendlyException("There is no such a question. Maybe it's deleted."); } // Incrementing the View if the question function is called if (input.IncrementViewCount) { question.ViewCount++; } // Mapping the question to another class so that we will have more defined classes which includes Answers and UsersVoted return(new GetQuestionOutput { Question = question.MapTo <QuestionWithAnswersDto>() }); }