> For the complete documentation index, see [llms.txt](https://raviram.gitbook.io/webapi-module-1/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://raviram.gitbook.io/webapi-module-1/create-pizza-api/implement-crud.md).

# Implement CRUD

## Implement CRUD operation

Let's continue extending our web API controller to add the ability to create (`POST`), update (`PUT`), and delete (`DELETE`) pizza from our inventory.

### Add a pizza <a href="#add-a-pizza" id="add-a-pizza"></a>

Let's enable a pizza to be added through the web API by using a `POST` method.

Replace the `// POST action` comment in *Controllers/PizzaController.cs* with the following code:

```csharp
[HttpPost]
public IActionResult Create(Pizza pizza)
{            
    PizzaService.Add(pizza);
    return CreatedAtAction(nameof(Create), new { id = pizza.Id }, pizza);
}
```

The preceding action:

* Responds only to the HTTP `POST` verb, as denoted by the `[HttpPost]` attribute.
* Inserts the request body's `Pizza` object into the in-memory cache.

&#x20;Note

Because the controller is annotated with the `[ApiController]` attribute, it's implied that the `Pizza` parameter will be found in the request body.

The first parameter in the `CreatedAtAction` method call represents an action name. The `nameof` keyword is used to avoid hard-coding the action name. `CreatedAtAction` uses the action name to generate a `location` HTTP response header with a URL to the newly created pizza.

### Modify a pizza <a href="#modify-a-pizza" id="modify-a-pizza"></a>

Now, let's enable a pizza to be updated through the web API by using a `PUT` method.

Replace the `// PUT action` comment in *Controllers/PizzaController.cs* with the following code:

```csharp
[HttpPut("{id}")]
public IActionResult Update(int id, Pizza pizza)
{
    if (id != pizza.Id)
        return BadRequest();
           
    var existingPizza = PizzaService.Get(id);
    if(existingPizza is null)
        return NotFound();
   
    PizzaService.Update(pizza);           
   
    return NoContent();
}
```

The preceding action:

* Responds only to the HTTP PUT verb, as denoted by the `[HttpPut]` attribute.
* Requires that the `id` parameter's value is included in the URL segment after `pizza/`.
* Returns `IActionResult`, because the `ActionResult` return type isn't known until runtime. The `BadRequest`, `NotFound`, and `NoContent` methods return `BadRequestResult`, `NotFoundResult`, and `NoContentResult` types, respectively.

&#x20;Note

Because the controller is annotated with the `[ApiController]` attribute, it's implied that the `Pizza` parameter will be found in the request body.

### Remove a pizza <a href="#remove-a-pizza" id="remove-a-pizza"></a>

Finally, let's enable a pizza to be removed through the web API by using a `DELETE` method.

Replace the `// DELETE action` comment in *Controllers/PizzaController.cs* with the following code:

```csharp
[HttpDelete("{id}")]
public IActionResult Delete(int id)
{
    var pizza = PizzaService.Get(id);
   
    if (pizza is null)
        return NotFound();
       
    PizzaService.Delete(id);
   
    return NoContent();
}
```

The preceding action:

* Responds only to the HTTP `DELETE` verb, as denoted by the `[HttpDelete]` attribute.
* Requires that the `id` parameter's value is included in the URL segment after `pizza/`.
* Returns `IActionResult` because the `ActionResult` return type isn't known until runtime. The `NotFound` and `NoContent` methods return `NotFoundResult` and `NoContentResult` types, respectively.
* Queries the in-memory cache for a pizza that matches the provided `id` parameter.

Remember to save the *Controllers/PizzaController.cs* file before proceeding,

### Build and run the finished web API <a href="#build-and-run-the-finished-web-api" id="build-and-run-the-finished-web-api"></a>

Try to create a pizza as shown:

<figure><img src="/files/dS9ND7ZufZ2XDPR1ojfz" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/kus1fWeYLOgnVdAuYcVO" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
Note how the new pizza is shown in the response body and also a location Uri is created for pizza 3. This is required when clients want to do more with the new object!
{% endhint %}

You can also see the newly created pizza if you do a get request for showing all pizzas. Note that this has to be done before you stop the application since we don't have a backing database and we are reading from cache!

<figure><img src="/files/ac3R1OFIKXGds5WgXC89" alt=""><figcaption></figcaption></figure>

Similarly, you can try editing and deleting a pizza!

### Videos for learning more <a href="#videos-for-learning-more" id="videos-for-learning-more"></a>

* [.NET 101](https://learn.microsoft.com/en-us/shows/NET-Core-101/?WT.mc_id=Educationaldotnet-c9-scottha)
* [ASP.NET Core Web API 101](https://learn.microsoft.com/en-us/shows/Beginners-Series-to-Web-APIs/)
* [ASP.NET Core 101](https://learn.microsoft.com/en-us/shows/ASPNET-Core-101/?WT.mc_id=Educationaspnet-c9-niner)
