Deleting resources

The Delete action method is marked with the HttpDelete attribute. It usually accepts the identifier of the resource to remove.

In real-world applications, Delete action methods do not perform physical delete operation on the database. They actually carry out update operations. Real-world applications and web services typically perform soft deletes instead of physical deletes because it is essential to keep track of historical data and information that's stored by our services. If we think about an e-commerce application, for example, it would be crucial to keep track of orders that have been dispatched by our system. Soft deletes are usually implemented using an isDeleted flag or an object that represents the status of our resource. All the other routes that are implemented in our APIs should implement logic to filter out all the resources in a deleted state. For example, a Get route should exclude all the deleted resources before presenting them to the client.

In our case, we are going to implement a real delete operation on our IOrderRepository. The Delete action methods usually return a 204 No Content error to confirm the deletion or a 404 Not Found error if the client passes a non-existent identifier:

[HttpDelete("{id:guid}")]
public IActionResult Delete(Guid id)
{
var order = _orderRepository.Get(id);

if (order == null)
{
return NotFound(new { Message = $"Item with id {id} not exist." });
}

_orderRepository.Delete(id);
return NoContent();
}

The implementation fetches the resource with the corresponding id from the IOrderRepository interface. It proceeds by checking whether the resource is null, and in this case, it will produce a not found error. If the order entity is present in the data source, it continues with the deletion process, and it returns a no content result. Now, let's proceed by having a look at the asynchronous process using a web service.  

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

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