1- using Microsoft . AspNetCore . Mvc ;
1+ using Microsoft . AspNetCore . Http ;
2+ using Microsoft . AspNetCore . Mvc ;
23using Store . ApplicationCore . DTOs ;
34using Store . ApplicationCore . Exceptions ;
45using Store . ApplicationCore . Interfaces ;
56using System . Collections . Generic ;
7+ using System . Net . Mime ;
68
79namespace Store . WebApi . Controllers
810{
911 [ Route ( "api/[controller]" ) ]
1012 [ ApiController ]
13+ [ Consumes ( MediaTypeNames . Application . Json ) ]
14+ [ Produces ( "application/json" ) ]
1115 public class ProductsController : Controller
1216 {
1317 private readonly IProductRepository productRepository ;
@@ -17,13 +21,26 @@ public ProductsController(IProductRepository productRepository)
1721 this . productRepository = productRepository ;
1822 }
1923
24+ /// <summary>
25+ /// Get all products
26+ /// </summary>
27+ /// <response code="200">Returns the products</response>
2028 [ HttpGet ]
21- public ActionResult < List < ProductResponse > > GetProducts ( )
29+ [ ProducesResponseType ( StatusCodes . Status200OK , Type = typeof ( List < ProductResponse > ) ) ]
30+ public ActionResult GetProducts ( )
2231 {
2332 return Ok ( this . productRepository . GetProducts ( ) ) ;
2433 }
2534
35+ /// <summary>
36+ /// Get a product by id
37+ /// </summary>
38+ /// <param name="id">Product id</param>
39+ /// <response code="200">Returns the existing product</response>
40+ /// <response code="404">If the product doesn't exist</response>
2641 [ HttpGet ( "{id}" ) ]
42+ [ ProducesResponseType ( StatusCodes . Status200OK , Type = typeof ( SingleProductResponse ) ) ]
43+ [ ProducesResponseType ( StatusCodes . Status404NotFound ) ]
2744 public ActionResult GetProductById ( int id )
2845 {
2946 try
@@ -37,14 +54,33 @@ public ActionResult GetProductById(int id)
3754 }
3855 }
3956
57+ /// <summary>
58+ /// Create a product
59+ /// </summary>
60+ /// <param name="request">Product data</param>
61+ /// <response code="201">Returns the created product</response>
62+ /// <response code="400">If the data doesn't pass the validations</response>
4063 [ HttpPost ]
64+ [ ProducesResponseType ( StatusCodes . Status201Created , Type = typeof ( SingleProductResponse ) ) ]
65+ [ ProducesResponseType ( StatusCodes . Status400BadRequest ) ]
4166 public ActionResult Create ( CreateProductRequest request )
4267 {
4368 var product = this . productRepository . CreateProduct ( request ) ;
44- return Ok ( product ) ;
69+ return StatusCode ( 201 , product ) ;
4570 }
4671
72+ /// <summary>
73+ /// Update a product by id
74+ /// </summary>
75+ /// <param name="id">Product id</param>
76+ /// <param name="request">Product data</param>
77+ /// <response code="200">Returns the updated product</response>
78+ /// <response code="400">If the data doesn't pass the validations</response>
79+ /// <response code="404">If the product doesn't exist</response>
4780 [ HttpPut ( "{id}" ) ]
81+ [ ProducesResponseType ( StatusCodes . Status200OK , Type = typeof ( SingleProductResponse ) ) ]
82+ [ ProducesResponseType ( StatusCodes . Status400BadRequest ) ]
83+ [ ProducesResponseType ( StatusCodes . Status404NotFound ) ]
4884 public ActionResult Update ( int id , UpdateProductRequest request )
4985 {
5086 try
@@ -58,7 +94,15 @@ public ActionResult Update(int id, UpdateProductRequest request)
5894 }
5995 }
6096
97+ /// <summary>
98+ /// Delete a product by id
99+ /// </summary>
100+ /// <param name="id">Product id</param>
101+ /// <response code="204">If the product was deleted</response>
102+ /// <response code="404">If the product doesn't exist</response>
61103 [ HttpDelete ( "{id}" ) ]
104+ [ ProducesResponseType ( StatusCodes . Status204NoContent ) ]
105+ [ ProducesResponseType ( StatusCodes . Status404NotFound ) ]
62106 public ActionResult Delete ( int id )
63107 {
64108 try
0 commit comments