1- namespace ASPNETCoreExample . Controllers {
1+ namespace ASPNETCoreExample . Controllers
2+ {
23 using System . Collections . Generic ;
34 using System . Linq ;
45
5- using Dtos ;
6+ using ASPNETCoreExample . Dtos ;
7+ using ASPNETCoreExample . Models ;
8+ using ASPNETCoreExample . Repositories ;
69
710 using Microsoft . AspNetCore . JsonPatch ;
811 using Microsoft . AspNetCore . Mvc ;
912 using Microsoft . Extensions . Logging ;
1013
11- using Models ;
12-
13- using Repositories ;
14-
15- /// <summary>
16- /// Controller of <see cref="IFoodRepository"/>.
17- /// </summary>
1814 [ Route ( "api/[controller]" ) ]
19- public class FoodController : Controller {
15+ public class FoodController : Controller
16+ {
2017 private readonly IFoodRepository foodRepository ;
2118
2219 private readonly ILogger logger ;
@@ -26,7 +23,8 @@ public class FoodController : Controller {
2623 /// </summary>
2724 /// <param name="foodRepository">Repository of <see cref="FoodItem"/>.</param>
2825 /// <param name="logger">Logger of this class.</param>
29- public FoodController ( IFoodRepository foodRepository , ILogger < FoodController > logger ) {
26+ public FoodController ( IFoodRepository foodRepository , ILogger < FoodController > logger )
27+ {
3028 this . foodRepository = foodRepository ;
3129 this . logger = logger ;
3230 }
@@ -38,7 +36,8 @@ public FoodController(IFoodRepository foodRepository, ILogger<FoodController> lo
3836 /// <response code="200">Returns all items of food.</response>
3937 [ HttpGet ]
4038 [ ProducesResponseType ( typeof ( IEnumerable < FoodDto > ) , 200 ) ]
41- public IActionResult GetAllFoodItems ( ) {
39+ public IActionResult GetAllFoodItems ( )
40+ {
4241 IEnumerable < FoodItem > foodItems = this . foodRepository . GetAll ( ) ;
4342 IEnumerable < FoodDto > mappedItems = foodItems . Select ( AutoMapper . Mapper . Map < FoodDto > ) ;
4443
@@ -55,10 +54,12 @@ public IActionResult GetAllFoodItems() {
5554 [ HttpGet ( "{id:int}" , Name = "GetSingleFoodItem" ) ]
5655 [ ProducesResponseType ( typeof ( FoodDto ) , 404 ) ]
5756 [ ProducesResponseType ( typeof ( FoodDto ) , 200 ) ]
58- public IActionResult GetSingleFoodItem ( int id ) {
57+ public IActionResult GetSingleFoodItem ( int id )
58+ {
5959 FoodItem foodItem = this . foodRepository . GetSingle ( id ) ;
6060
61- if ( foodItem == null ) {
61+ if ( foodItem == null )
62+ {
6263 this . logger . LogWarning ( "Item with {ID} not found" , id ) ;
6364 return this . NotFound ( ) ;
6465 }
@@ -76,23 +77,21 @@ public IActionResult GetSingleFoodItem(int id) {
7677 [ HttpPost ]
7778 [ ProducesResponseType ( typeof ( FoodDto ) , 400 ) ]
7879 [ ProducesResponseType ( typeof ( FoodDto ) , 201 ) ]
79- public IActionResult AddNewFoodItem ( [ FromBody ] FoodDto foodDto ) {
80- if ( foodDto == null ) {
80+ public IActionResult AddNewFoodItem ( [ FromBody ] FoodDto foodDto )
81+ {
82+ if ( foodDto == null )
83+ {
8184 return this . BadRequest ( ) ;
8285 }
8386
84- if ( ! this . ModelState . IsValid ) {
87+ if ( ! this . ModelState . IsValid )
88+ {
8589 return this . BadRequest ( this . ModelState ) ;
8690 }
8791
8892 FoodItem foodItem = this . foodRepository . Add ( AutoMapper . Mapper . Map < FoodItem > ( foodDto ) ) ;
8993
90- return this . CreatedAtRoute (
91- "GetSingleFoodItem" ,
92- new {
93- id = foodItem . Id
94- } ,
95- AutoMapper . Mapper . Map < FoodDto > ( foodItem ) ) ;
94+ return this . CreatedAtRoute ( "GetSingleFoodItem" , new { id = foodItem . Id } , AutoMapper . Mapper . Map < FoodDto > ( foodItem ) ) ;
9695 }
9796
9897 /// <summary>
@@ -108,17 +107,21 @@ public IActionResult AddNewFoodItem([FromBody] FoodDto foodDto) {
108107 [ ProducesResponseType ( typeof ( FoodDto ) , 404 ) ]
109108 [ ProducesResponseType ( typeof ( FoodDto ) , 400 ) ]
110109 [ ProducesResponseType ( typeof ( FoodDto ) , 200 ) ]
111- public IActionResult UpdateFoodItem ( int id , [ FromBody ] FoodDto foodDto ) {
110+ public IActionResult UpdateFoodItem ( int id , [ FromBody ] FoodDto foodDto )
111+ {
112112 var foodItemToCheck = this . foodRepository . GetSingle ( id ) ;
113- if ( foodItemToCheck == null ) {
113+ if ( foodItemToCheck == null )
114+ {
114115 return this . NotFound ( ) ;
115116 }
116117
117- if ( id != foodDto . Id ) {
118+ if ( id != foodDto . Id )
119+ {
118120 return this . BadRequest ( "Ids do not match!" ) ;
119121 }
120122
121- if ( ! this . ModelState . IsValid ) {
123+ if ( ! this . ModelState . IsValid )
124+ {
122125 return this . BadRequest ( this . ModelState ) ;
123126 }
124127
@@ -139,20 +142,24 @@ public IActionResult UpdateFoodItem(int id, [FromBody] FoodDto foodDto) {
139142 [ ProducesResponseType ( typeof ( FoodDto ) , 400 ) ]
140143 [ ProducesResponseType ( typeof ( FoodDto ) , 404 ) ]
141144 [ ProducesResponseType ( typeof ( FoodDto ) , 200 ) ]
142- public IActionResult PartialUpdate ( int id , [ FromBody ] JsonPatchDocument < FoodDto > foodDtoPatchDoc ) {
143- if ( foodDtoPatchDoc == null ) {
145+ public IActionResult PartialUpdate ( int id , [ FromBody ] JsonPatchDocument < FoodDto > foodDtoPatchDoc )
146+ {
147+ if ( foodDtoPatchDoc == null )
148+ {
144149 return this . BadRequest ( ) ;
145150 }
146151
147152 var foodItemExistingEntity = this . foodRepository . GetSingle ( id ) ;
148- if ( foodItemExistingEntity == null ) {
153+ if ( foodItemExistingEntity == null )
154+ {
149155 return this . NotFound ( ) ;
150156 }
151157
152158 FoodDto foodDto = AutoMapper . Mapper . Map < FoodDto > ( foodItemExistingEntity ) ;
153159 foodDtoPatchDoc . ApplyTo ( foodDto , this . ModelState ) ;
154160
155- if ( ! this . ModelState . IsValid ) {
161+ if ( ! this . ModelState . IsValid )
162+ {
156163 return this . BadRequest ( this . ModelState ) ;
157164 }
158165
@@ -170,9 +177,11 @@ public IActionResult PartialUpdate(int id, [FromBody] JsonPatchDocument<FoodDto>
170177 [ HttpDelete ( "{id:int}" ) ]
171178 [ ProducesResponseType ( typeof ( FoodDto ) , 404 ) ]
172179 [ ProducesResponseType ( typeof ( FoodDto ) , 204 ) ]
173- public IActionResult Remove ( int id ) {
180+ public IActionResult Remove ( int id )
181+ {
174182 var foodItem = this . foodRepository . GetSingle ( id ) ;
175- if ( foodItem == null ) {
183+ if ( foodItem == null )
184+ {
176185 return this . NotFound ( ) ;
177186 }
178187
0 commit comments