Creating an action method for updating a question

Let's move on to updating a question:

  1. We'll start with the skeleton action method:
[HttpPut("{questionId}")]
public ActionResult<QuestionGetSingleResponse>
PutQuestion(int questionId,
QuestionPutRequest questionPutRequest)
{
// TODO - get the question from the data repository
// TODO - return HTTP status code 404 if the question isn't found
// TODO - update the question model
// TODO - call the data repository with the updated question model to update the question in the database
// TODO - return the saved question
}

We use the HttpPut attribute to tell ASP.NET Core that this method handles HTTP PUT requests. We are also putting the route parameter for the question ID in the questionId method parameter.

ASP.NET Core model binding will populate the QuestionPutRequest class instance from the HTTP request body.

  1. Let's get the question from the data repository and return HTTP status code 404 if the question isn't found:
[HttpPut("{questionId}")]
public ActionResult<QuestionGetSingleResponse>
PutQuestion(int questionId,
QuestionPutRequest questionPutRequest)
{
var question =
_dataRepository.GetQuestion(questionId);

if (question == null)
{
return NotFound();
}

// TODO - update the question model
// TODO - call the data repository with the updated question
//model to update the question in the database
// TODO - return the saved question
}
  1. Now let's update the question model:
[HttpPut("{questionId}")]
public ActionResult<QuestionGetSingleResponse>
PutQuestion(int questionId,
QuestionPutRequest questionPutRequest)
{
var question =
_dataRepository.GetQuestion(questionId);
if (question == null)
{
return NotFound();
}
questionPutRequest.Title =
string.IsNullOrEmpty(questionPutRequest.Title) ?
question.Title :
questionPutRequest.Title;

questionPutRequest.Content =
string.IsNullOrEmpty(questionPutRequest.Content) ?
question.Content :
questionPutRequest.Content;


// TODO - call the data repository with the updated question
//model to update the question in the database
// TODO - return the saved question
}

We use ternary expressions to update the request model with data from the existing question if it hasn't been supplied in the request.

Not requiring the consumer to submit the full record, rather just the information that needs to be updated, is a nice touch in making our API easy to consume.
  1. The final steps in the implementation are to call the data repository to update the question and then return the saved question in the response:
[HttpPut("{questionId}")]
public ActionResult<QuestionGetSingleResponse>
PutQuestion(int questionId,
QuestionPutRequest questionPutRequest)
{
var question =
_dataRepository.GetQuestion(questionId);
if (question == null)
{
return NotFound();
}
questionPutRequest.Title =
string.IsNullOrEmpty(questionPutRequest.Title) ?
question.Title :
questionPutRequest.Title;
questionPutRequest.Content =
string.IsNullOrEmpty(questionPutRequest.Content) ?
question.Content :
questionPutRequest.Content;
var savedQuestion =
_dataRepository.PutQuestion(questionId,
questionPutRequest);

return savedQuestion;
}
  1. Let's try this out by running the app and using Postman. Set the HTTP method to PUT and enter the path to the questions resource. Add a Content-Type HTTP header and enter an updated content field in the request body with the relevant questionId. Click the Send button to send the request:

The question is updated just as we expect.

  1. Stop our app running so that we are ready to implement another action method.

That completes the implementation of the action method that will handle PUT requests to api/questions.

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

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