Lesson Wednesday

Over the last two lessons, we've written methods to GET data from our API. Now we're ready to add methods to POST, PUT and DELETE data.

POST


Models/ApiHelper.cs
...
namespace CretaceousClient.Models
{
  class ApiHelper
  {
    ...
    public static async Task Post(string newAnimal)
    {
      RestClient client = new RestClient("http://localhost:5000/api");
      RestRequest request = new RestRequest($"animals", Method.POST);
      request.AddHeader("Content-Type", "application/json");
      request.AddJsonBody(newAnimal);
      var response = await client.ExecuteTaskAsync(request);
    }
  }
}

When making a POST request (or any request that will be modifying our database) to our API, we need to add a header and a body. This way, our API can recognize the data types it receives and properly registers an argument for the controller route. On that note, the arguments of RestRequest in the code above specify the route and method that should be passed into the API controller. Note that newAnimal is a string. We will convert our objects into JSON before passing them to our ApiHelper.

The corresponding method in your Animal model should look like this:

Models/Animal.cs
...
namespace CretaceousClient.Models
{
  public class Animal
  {
    ...
    public static void Post(Animal animal)
    {
      string jsonAnimal = JsonConvert.SerializeObject(animal);
      var apiCallTask = ApiHelper.Post(jsonAnimal);
    }
  }
}

We only need to convert the Animal object into JSON and then call our helper method.

PUT


Models/ApiHelper.cs
...
namespace CretaceousClient.Models
{
  class ApiHelper
  {
    ...
    public static async Task Put(int id, string newAnimal)
    {
      RestClient client = new RestClient("http://localhost:5000/api");
      RestRequest request = new RestRequest($"animals/{id}", Method.PUT);
      request.AddHeader("Content-Type", "application/json");
      request.AddJsonBody(newAnimal);
      var response = await client.ExecuteTaskAsync(request);
    }
  }
}

Our PUT functionality is very similar to our POST functionality. The key difference is that we need to include an id for our PUT functionality. Unlike with a POST request, where we are simply adding a record to the database, we are actually modifying an existing record - and we need that record's id to correctly modify it.

Models/Animal.cs
...
namespace CretaceousClient.Models
{
  public class Animal
  {
    ...
    public static void Put(Animal animal)
    {
      string jsonAnimal = JsonConvert.SerializeObject(animal);
      var apiCallTask = ApiHelper.Put(animal.AnimalId, jsonAnimal);
    }
  }
}

The only difference between POST and PUT in our model is the helper method we call.

DELETE


Models/ApiHelper.cs
...
namespace CretaceousClient.Models
{
  class ApiHelper
  {
    ...
    public static async Task Delete(int id)
    {
      RestClient client = new RestClient("http://localhost:5000/api");
      RestRequest request = new RestRequest($"animals/{id}", Method.DELETE);
      request.AddHeader("Content-Type", "application/json");
      var response = await client.ExecuteTaskAsync(request);
    }
  }
}

Our DELETE method won't require a body - only the id of the Animal we're going to delete.

The model method is the simplest we've written so far:

Models/Animal.cs
...
namespace CretaceousClient.Models
{
  public class Animal
  {
    ...
    public static void Delete(int id)
    {
      var apiCallTask = ApiHelper.Delete(id);
    }
  }
}

Now our models have full CRUD capability! As we've already stated, we won't go into deep detail with the views and controllers because they closely reflect what we've done in past projects over the past few course sections. If you would like to see how the above material has been implemented in an example project, here is the reference repository at this point.

Lesson 21 of 22
Last updated April 6, 2022