Creating a repository method to get a single question

Let's implement the GetQuestion method now:

  1. Start by opening the connection and executing the Question_GetSingle stored procedure:
public QuestionGetSingleResponse GetQuestion(int questionId)
{
using (var connection = new SqlConnection(_connectionString))
{
connection.Open();
var question =
connection.QueryFirstOrDefault<QuestionGetSingleResponse>(
@"EXEC dbo.Question_GetSingle @QuestionId = @QuestionId",
new { QuestionId = questionId }
);

// TODO - Get the answers for the question

return question;
}
}

This method is a little different from the previous methods because we use the QueryFirstOrDefault Dapper method to return a single record (or null if the record isn't found) rather than a collection of records. 

  1. We need to execute a second stored procedure to get the answers for the question, so let's do this:
public QuestionGetSingleResponse GetQuestion(int questionId)
{
using (var connection = new SqlConnection(_connectionString))
{
connection.Open();
var question =
connection.QueryFirstOrDefault<QuestionGetSingleResponse>(
@"EXEC dbo.Question_GetSingle @QuestionId = @QuestionId",
new { QuestionId = questionId }
);
question.Answers =
connection.Query<AnswerGetResponse>(
@"EXEC dbo.Answer_Get_ByQuestionId
@QuestionId = @QuestionId",
new { QuestionId = questionId }
);

return question;
}
}
  1. The question may not be found and return null, so let's handle this case and only add the answers if the question is found:
public QuestionGetSingleResponse GetQuestion(int questionId)
{
using (var connection = new SqlConnection(_connectionString))
{
connection.Open();
var question =
connection.QueryFirstOrDefault<QuestionGetSingleResponse>(
@"EXEC dbo.Question_GetSingle @QuestionId = @QuestionId",
new { QuestionId = questionId }
);
if (question != null)
{
question.Answers =
connection.Query<AnswerGetResponse>(
@"EXEC dbo.Answer_Get_ByQuestionId
@QuestionId = @QuestionId",
new { QuestionId = questionId }
);
}
return question;
}
}
  1. Let's create the QuestionGetSingleResponse class we referenced in the method in a file called QuestionGetSingleResponse.cs in the Models folder:
public class QuestionGetSingleResponse
{
public int QuestionId { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public string UserName { get; set; }
public string UserId { get; set; }
public DateTime Created { get; set; }
public IEnumerable<AnswerGetResponse> Answers { get; set; }
}

These properties match up with the data returned from the Question_GetSingle stored procedure.

  1. Let's also create the AnswerGetResponse class we referenced in the method in a file called AnswerGetResponse.cs in the Models folder:
public class AnswerGetResponse
{
public int AnswerId { get; set; }
public string Content { get; set; }
public string UserName { get; set; }
public DateTime Created { get; set; }
}

These properties match up with the data returned from the Answer_Get_ByQuestionId stored procedure.

The GetQuestion method should compile fine now.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.23.86.219