ProductsController.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using Microsoft.AspNetCore.JsonPatch;
  2. using Microsoft.AspNetCore.Mvc;
  3. using Products.Business.Service;
  4. using Products.Common.Dtos;
  5. namespace Products.API.Controllers;
  6. [Route("api/[controller]")]
  7. [ApiController]
  8. public class ProductsController : ControllerBase
  9. {
  10. private readonly IProductService _productService;
  11. public ProductsController(IProductService productService)
  12. {
  13. _productService = productService;
  14. }
  15. [HttpGet]
  16. public ActionResult<IEnumerable<ProductDto>> Get()
  17. {
  18. return Ok(_productService.GetProducts());
  19. }
  20. [HttpGet("{id}")]
  21. public ActionResult<ProductDto> Get(int id)
  22. {
  23. return Ok(_productService.GetProduct(id));
  24. }
  25. [HttpPost]
  26. public ActionResult<ProductDto> Post(CreateProductDto product)
  27. {
  28. return Ok(_productService.AddProduct(product));
  29. }
  30. [HttpPatch("{id}")]
  31. public ActionResult<ProductDto> Patch(int id, JsonPatchDocument<UpdateProductDto> product)
  32. {
  33. return Ok(_productService.PatchProduct(id, product));
  34. }
  35. [HttpPut("{id}")]
  36. public ActionResult<ProductDto> Put(int id, UpdateProductDto product)
  37. {
  38. return Ok(_productService.UpdateProduct(id, product));
  39. }
  40. [HttpDelete("{id}")]
  41. public ActionResult Delete(int id)
  42. {
  43. _productService.DeleteProduct(id);
  44. return Ok();
  45. }
  46. }