What is CRUD operation PART 4 With Example Code

Delete operation involves removing existing records or entities from a database.
It typically includes deleting a single record based on its unique identifier, removing multiple records that meet certain criteria, or deleting related records in a cascading manner.
For example, in a web application, deleting a user account or removing a product from inventory would be considered delete operations. 



using FreshWebApp.Data;
using FreshWebApp.Models;
 
namespace FreshWebApp.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class InventoryController : ControllerBase
    {
        private readonly FreshWebAppDBContext _context;
 
        public InventoryController(FreshWebAppDBContext context)
        {
            _context = context;
        }
 
        // GET: api/Inventory
        [HttpGet]
        public ActionResult<IEnumerable<ProductModel>> GetInventory()
        {
            return _context.ProductDetails.ToList();
        }
 
        // GET: api/Inventory/5
        [HttpGet("{id}")]
        public ActionResult<ProductModel> GetInventoryItem(int id)
        {
            var product = _context.ProductDetails.Find(id);
 
            if (product == null)
            {
                return NotFound();
            }
 
            return product;
        }
 
        // POST: api/Inventory
        [HttpPost]
        public ActionResult<ProductModel> AddInventoryItem(ProductModel product)
        {
            _context.ProductDetails.Add(product);
            _context.SaveChanges();
 
            return CreatedAtAction(nameof(GetInventoryItem), new { id = product.Id }, product);
        }
 
        // PUT: api/Inventory/5
        [HttpPut("{id}")]
        public IActionResult UpdateInventoryItem(int id, ProductModel product)
        {
            if (id != product.Id)
            {
                return BadRequest();
            }
 
            _context.Entry(product).State = EntityState.Modified;
            _context.SaveChanges();
 
            return NoContent();
        }
 
        // DELETE: api/Inventory/5
        [HttpDelete("{id}")]
        public IActionResult DeleteInventoryItem(int id)
        {
            var product = _context.ProductDetails.Find(id);
 
            if (product == null)
            {
                return NotFound();
            }
 
            _context.ProductDetails.Remove(product);
            _context.SaveChanges();
 
            return NoContent();
        }
    }
}