Dealing with single entries

Our updated QuizController class gives us a way to retrieve a single Quiz entry; it will definitely be very useful when our users will select one of them within the Latest list, as we'll be able to point them to something similar to a detail page. It will also be very useful when we'll have to deal with CRUD operations such as Delete and Update.

We're not dealing with the client-side code yet, so we don't know how we'll present such a scenario to the user. However, we already know what we'll eventually need, a Get, Put, Post, and Delete method for each one of our entries--Quizzes, Questions, Answers, and Results--as we'll definitely have to perform these operations for all of them.

Luckily enough, we don't need to implement them now. However, since we're working with these Controllers, it can be a good time to set up a basic interface:

 #region RESTful conventions methods 
/// <summary>
/// Retrieves the Answer with the given {id}
/// </summary>
/// &lt;param name="id">The ID of an existing Answer</param>
/// <returns>the Answer with the given {id}</returns>
[HttpGet("{id}")]
public IActionResult Get(int id)
{
return Content("Not implemented (yet)!");
}

/// <summary>
/// Adds a new Answer to the Database
/// </summary>
/// <param name="m">The AnswerViewModel containing the data to insert</param>
[HttpPut]
public IActionResult Put(AnswerViewModel m)
{
throw new NotImplementedException();
}

/// <summary>
/// Edit the Answer with the given {id}
/// </summary>
/// <param name="m">The AnswerViewModel containing the data to update</param>
[HttpPost]
public IActionResult Post(AnswerViewModel m)
{
throw new NotImplementedException();
}

/// <summary>
/// Deletes the Answer with the given {id} from the Database
/// </summary>
/// <param name="id">The ID of an existing Answer</param>
[HttpDelete("{id}")]
public IActionResult Delete(int id)
{
throw new NotImplementedException();
}
#endregion

This is the code that we have to add to AnswerController. As soon as we do that, we also need to perform the same operation with the other controllers: QuestionController, ResultController, and also QuizController (except for the Get method we added earlier). Ensure that you change the comments and the ViewModel reference accordingly (each Controller must reference the ViewModel related to their specific entry).

Our brand new RESTful methods won't do anything other than returning a not-implemented text message (or a NotImplementedException) to the client/browser issuing the request; this is by design, as we'll implement them later on.

To test our new methods, select Debug | Start Debugging from the main menu (or press F5) and type the following URLs in the browser's address bar:

  • For quizzes, type /api/quiz/1:
  • For question, type /api/question/1:
  • For answer, type /api/answer/1:
  • For result, type /api/result/1:

Everything works as it should be. As we already said, the exception brought on screen by the Response is perfectly fine; we did it on purpose, to demonstrate how we can handle these kinds of errors. This is also a good practice when dealing with ASP.NET Core API interfaces: if we don't have it implemented yet, throw a NotImplementedException until we're done to prevent any unwanted Request from having nasty effects on our still unfinished web application.

So far, so good, we got ourselves a number of server-side APIs to retrieve JSON arrays filled by a client-defined (or default) number of latest items, and an additional one to retrieve a single item from its unique ID. All these calls will be very handy in the next chapter, where we'll start developing client-side components using Angular.

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

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