11namespace NorthwindCRUD . Controllers
22{
33 using AutoMapper ;
4+ using Microsoft . AspNetCore . Authorization ;
45 using Microsoft . AspNetCore . Mvc ;
56 using NorthwindCRUD . Models . DbModels ;
6- using NorthwindCRUD . Models . InputModels ;
7+ using NorthwindCRUD . Models . Dtos ;
78 using NorthwindCRUD . Services ;
89
910 [ ApiController ]
1011 [ Route ( "[controller]" ) ]
11- public class CategoryController : ControllerBase
12+ public class CategoriesController : ControllerBase
1213 {
1314 private readonly CategoryService categoryService ;
15+ private readonly ProductService productService ;
1416 private readonly IMapper mapper ;
1517 private readonly ILogger logger ;
1618
17- public CategoryController ( CategoryService categoryService , IMapper mapper , ILogger logger )
19+ public CategoriesController ( CategoryService categoryService , ProductService productService , IMapper mapper , ILogger logger )
1820 {
1921 this . categoryService = categoryService ;
22+ this . productService = productService ;
2023 this . mapper = mapper ;
2124 this . logger = logger ;
2225 }
2326
2427 [ HttpGet ]
25- public ActionResult < CategoryInputModel [ ] > GetAll ( )
28+ public ActionResult < CategoryDto [ ] > GetAll ( )
2629 {
2730 try
2831 {
2932 var categories = this . categoryService . GetAll ( ) ;
30- return Ok ( this . mapper . Map < CategoryDb [ ] , CategoryInputModel [ ] > ( categories ) ) ;
33+ return base . Ok ( this . mapper . Map < CategoryDb [ ] , CategoryDto [ ] > ( categories ) ) ;
3134 }
3235 catch ( Exception error )
3336 {
@@ -38,15 +41,35 @@ public ActionResult<CategoryInputModel[]> GetAll()
3841 }
3942
4043 [ HttpGet ( "{id}" ) ]
41- public ActionResult < CategoryInputModel > GetById ( int id )
44+ public ActionResult < CategoryDto > GetById ( int id )
4245 {
4346 try
4447 {
4548 var category = this . categoryService . GetById ( id ) ;
49+ if ( category != null )
50+ {
51+ return base . Ok ( this . mapper . Map < CategoryDb , CategoryDto > ( category ) ) ;
52+ }
53+
54+ return NotFound ( ) ;
55+ }
56+ catch ( Exception error )
57+ {
58+ logger . LogError ( error . Message ) ;
59+ return StatusCode ( 500 ) ;
60+ }
61+ }
4662
63+
64+ [ HttpGet ( "{id}/Details" ) ]
65+ public ActionResult < CategoryDetailsDto > GetDetailsById ( int id )
66+ {
67+ try
68+ {
69+ var category = this . categoryService . GetById ( id ) ;
4770 if ( category != null )
4871 {
49- return Ok ( this . mapper . Map < CategoryDb , CategoryInputModel > ( category ) ) ;
72+ return Ok ( this . mapper . Map < CategoryDb , CategoryDetailsDto > ( category ) ) ;
5073 }
5174
5275 return NotFound ( ) ;
@@ -58,16 +81,32 @@ public ActionResult<CategoryInputModel> GetById(int id)
5881 }
5982 }
6083
84+ [ HttpGet ( "{id}/Products" ) ]
85+ public ActionResult < ProductDto [ ] > GetProductsByCategoryId ( int id )
86+ {
87+ try
88+ {
89+ var products = this . productService . GetAllByCategoryId ( id ) ;
90+ return Ok ( this . mapper . Map < ProductDb [ ] , ProductDto [ ] > ( products ) ) ;
91+ }
92+ catch ( Exception error )
93+ {
94+ logger . LogError ( error . Message ) ;
95+ return StatusCode ( 500 ) ;
96+ }
97+ }
98+
6199 [ HttpPost ]
62- public ActionResult < CategoryInputModel > Create ( CategoryInputModel model )
100+ [ Authorize ]
101+ public ActionResult < CategoryDetailsDto > Create ( CategoryDto model )
63102 {
64103 try
65104 {
66105 if ( ModelState . IsValid )
67106 {
68- var mappedModel = this . mapper . Map < CategoryInputModel , CategoryDb > ( model ) ;
107+ var mappedModel = this . mapper . Map < CategoryDto , CategoryDb > ( model ) ;
69108 var category = this . categoryService . Create ( mappedModel ) ;
70- return Ok ( this . mapper . Map < CategoryDb , CategoryInputModel > ( category ) ) ;
109+ return Ok ( this . mapper . Map < CategoryDb , CategoryDetailsDto > ( category ) ) ;
71110 }
72111
73112 return BadRequest ( ModelState ) ;
@@ -80,15 +119,22 @@ public ActionResult<CategoryInputModel> Create(CategoryInputModel model)
80119 }
81120
82121 [ HttpPut ]
83- public ActionResult < CategoryInputModel > Update ( CategoryInputModel model )
122+ [ Authorize ]
123+ public ActionResult < CategoryDto > Update ( CategoryDto model )
84124 {
85125 try
86126 {
87127 if ( ModelState . IsValid )
88128 {
89- var mappedModel = this . mapper . Map < CategoryInputModel , CategoryDb > ( model ) ;
129+ var mappedModel = this . mapper . Map < CategoryDto , CategoryDb > ( model ) ;
90130 var category = this . categoryService . Update ( mappedModel ) ;
91- return Ok ( this . mapper . Map < CategoryDb , CategoryInputModel > ( category ) ) ;
131+
132+ if ( category != null )
133+ {
134+ return base . Ok ( this . mapper . Map < CategoryDb , CategoryDto > ( category ) ) ;
135+ }
136+
137+ return NotFound ( ) ;
92138 }
93139
94140 return BadRequest ( ModelState ) ;
@@ -101,15 +147,15 @@ public ActionResult<CategoryInputModel> Update(CategoryInputModel model)
101147 }
102148
103149 [ HttpDelete ( "{id}" ) ]
104- public ActionResult < CategoryInputModel > Delete ( int id )
150+ [ Authorize ]
151+ public ActionResult < CategoryDto > Delete ( int id )
105152 {
106153 try
107154 {
108155 var category = this . categoryService . Delete ( id ) ;
109-
110156 if ( category != null )
111157 {
112- return Ok ( this . mapper . Map < CategoryDb , CategoryInputModel > ( category ) ) ;
158+ return base . Ok ( this . mapper . Map < CategoryDb , CategoryDto > ( category ) ) ;
113159 }
114160
115161 return NotFound ( ) ;
0 commit comments