@@ -23,7 +23,7 @@ public class FoodsController : ControllerBase
23
23
private readonly IMapper _mapper ;
24
24
25
25
public FoodsController (
26
- IUrlHelper urlHelper ,
26
+ IUrlHelper urlHelper ,
27
27
IFoodRepository foodRepository ,
28
28
IMapper mapper )
29
29
{
@@ -33,9 +33,9 @@ public FoodsController(
33
33
}
34
34
35
35
[ HttpGet ( Name = nameof ( GetAllFoods ) ) ]
36
- public ActionResult GetAllFoods ( [ FromQuery ] QueryParameters queryParameters )
36
+ public ActionResult GetAllFoods ( ApiVersion version , [ FromQuery ] QueryParameters queryParameters )
37
37
{
38
- List < FoodItem > foodItems = _foodRepository . GetAll ( queryParameters ) . ToList ( ) ;
38
+ List < FoodEntity > foodItems = _foodRepository . GetAll ( queryParameters ) . ToList ( ) ;
39
39
40
40
var allItemCount = _foodRepository . Count ( ) ;
41
41
@@ -50,9 +50,9 @@ public ActionResult GetAllFoods([FromQuery] QueryParameters queryParameters)
50
50
Response . Headers . Add ( "X-Pagination" ,
51
51
Newtonsoft . Json . JsonConvert . SerializeObject ( paginationMetadata ) ) ;
52
52
53
- var links = CreateLinksForCollection ( queryParameters , allItemCount ) ;
53
+ var links = CreateLinksForCollection ( queryParameters , allItemCount , version ) ;
54
54
55
- var toReturn = foodItems . Select ( x => ExpandSingleFoodItem ( x ) ) ;
55
+ var toReturn = foodItems . Select ( x => ExpandSingleFoodItem ( x , version ) ) ;
56
56
57
57
return Ok ( new
58
58
{
@@ -63,27 +63,27 @@ public ActionResult GetAllFoods([FromQuery] QueryParameters queryParameters)
63
63
64
64
[ HttpGet ]
65
65
[ Route ( "{id:int}" , Name = nameof ( GetSingleFood ) ) ]
66
- public ActionResult GetSingleFood ( int id )
66
+ public ActionResult GetSingleFood ( ApiVersion version , int id )
67
67
{
68
- FoodItem foodItem = _foodRepository . GetSingle ( id ) ;
68
+ FoodEntity foodItem = _foodRepository . GetSingle ( id ) ;
69
69
70
70
if ( foodItem == null )
71
71
{
72
72
return NotFound ( ) ;
73
73
}
74
74
75
- return Ok ( ExpandSingleFoodItem ( foodItem ) ) ;
75
+ return Ok ( ExpandSingleFoodItem ( foodItem , version ) ) ;
76
76
}
77
77
78
78
[ HttpPost ( Name = nameof ( AddFood ) ) ]
79
- public ActionResult < FoodItemDto > AddFood ( [ FromBody ] FoodCreateDto foodCreateDto )
79
+ public ActionResult < FoodDto > AddFood ( ApiVersion version , [ FromBody ] FoodCreateDto foodCreateDto )
80
80
{
81
81
if ( foodCreateDto == null )
82
82
{
83
83
return BadRequest ( ) ;
84
84
}
85
85
86
- FoodItem toAdd = _mapper . Map < FoodItem > ( foodCreateDto ) ;
86
+ FoodEntity toAdd = _mapper . Map < FoodEntity > ( foodCreateDto ) ;
87
87
88
88
_foodRepository . Add ( toAdd ) ;
89
89
@@ -92,21 +92,21 @@ public ActionResult<FoodItemDto> AddFood([FromBody] FoodCreateDto foodCreateDto)
92
92
throw new Exception ( "Creating a fooditem failed on save." ) ;
93
93
}
94
94
95
- FoodItem newFoodItem = _foodRepository . GetSingle ( toAdd . Id ) ;
95
+ FoodEntity newFoodItem = _foodRepository . GetSingle ( toAdd . Id ) ;
96
96
97
- return CreatedAtRoute ( nameof ( GetSingleFood ) , new { id = newFoodItem . Id } ,
98
- _mapper . Map < FoodItemDto > ( newFoodItem ) ) ;
97
+ return CreatedAtRoute ( nameof ( GetSingleFood ) , new { version = version . ToString ( ) , id = newFoodItem . Id } ,
98
+ _mapper . Map < FoodDto > ( newFoodItem ) ) ;
99
99
}
100
100
101
101
[ HttpPatch ( "{id:int}" , Name = nameof ( PartiallyUpdateFood ) ) ]
102
- public ActionResult < FoodItemDto > PartiallyUpdateFood ( int id , [ FromBody ] JsonPatchDocument < FoodUpdateDto > patchDoc )
102
+ public ActionResult < FoodDto > PartiallyUpdateFood ( int id , [ FromBody ] JsonPatchDocument < FoodUpdateDto > patchDoc )
103
103
{
104
104
if ( patchDoc == null )
105
105
{
106
106
return BadRequest ( ) ;
107
107
}
108
108
109
- FoodItem existingEntity = _foodRepository . GetSingle ( id ) ;
109
+ FoodEntity existingEntity = _foodRepository . GetSingle ( id ) ;
110
110
111
111
if ( existingEntity == null )
112
112
{
@@ -124,21 +124,21 @@ public ActionResult<FoodItemDto> PartiallyUpdateFood(int id, [FromBody] JsonPatc
124
124
}
125
125
126
126
_mapper . Map ( foodUpdateDto , existingEntity ) ;
127
- FoodItem updated = _foodRepository . Update ( id , existingEntity ) ;
127
+ FoodEntity updated = _foodRepository . Update ( id , existingEntity ) ;
128
128
129
129
if ( ! _foodRepository . Save ( ) )
130
130
{
131
131
throw new Exception ( "Updating a fooditem failed on save." ) ;
132
132
}
133
133
134
- return Ok ( _mapper . Map < FoodItemDto > ( updated ) ) ;
134
+ return Ok ( _mapper . Map < FoodDto > ( updated ) ) ;
135
135
}
136
136
137
137
[ HttpDelete ]
138
138
[ Route ( "{id:int}" , Name = nameof ( RemoveFood ) ) ]
139
139
public ActionResult RemoveFood ( int id )
140
140
{
141
- FoodItem foodItem = _foodRepository . GetSingle ( id ) ;
141
+ FoodEntity foodItem = _foodRepository . GetSingle ( id ) ;
142
142
143
143
if ( foodItem == null )
144
144
{
@@ -157,7 +157,7 @@ public ActionResult RemoveFood(int id)
157
157
158
158
[ HttpPut ]
159
159
[ Route ( "{id:int}" , Name = nameof ( UpdateFood ) ) ]
160
- public ActionResult < FoodItemDto > UpdateFood ( int id , [ FromBody ] FoodUpdateDto foodUpdateDto )
160
+ public ActionResult < FoodDto > UpdateFood ( int id , [ FromBody ] FoodUpdateDto foodUpdateDto )
161
161
{
162
162
if ( foodUpdateDto == null )
163
163
{
@@ -180,16 +180,16 @@ public ActionResult<FoodItemDto> UpdateFood(int id, [FromBody]FoodUpdateDto food
180
180
throw new Exception ( "Updating a fooditem failed on save." ) ;
181
181
}
182
182
183
- return Ok ( _mapper . Map < FoodItemDto > ( existingFoodItem ) ) ;
183
+ return Ok ( _mapper . Map < FoodDto > ( existingFoodItem ) ) ;
184
184
}
185
185
186
186
[ HttpGet ( "GetRandomMeal" , Name = nameof ( GetRandomMeal ) ) ]
187
187
public ActionResult GetRandomMeal ( )
188
188
{
189
- ICollection < FoodItem > foodItems = _foodRepository . GetRandomMeal ( ) ;
189
+ ICollection < FoodEntity > foodItems = _foodRepository . GetRandomMeal ( ) ;
190
190
191
- IEnumerable < FoodItemDto > dtos = foodItems
192
- . Select ( x => _mapper . Map < FoodItemDto > ( x ) ) ;
191
+ IEnumerable < FoodDto > dtos = foodItems
192
+ . Select ( x => _mapper . Map < FoodDto > ( x ) ) ;
193
193
194
194
var links = new List < LinkDto > ( ) ;
195
195
@@ -203,7 +203,7 @@ public ActionResult GetRandomMeal()
203
203
} ) ;
204
204
}
205
205
206
- private List < LinkDto > CreateLinksForCollection ( QueryParameters queryParameters , int totalCount )
206
+ private List < LinkDto > CreateLinksForCollection ( QueryParameters queryParameters , int totalCount , ApiVersion version )
207
207
{
208
208
var links = new List < LinkDto > ( ) ;
209
209
@@ -249,46 +249,54 @@ private List<LinkDto> CreateLinksForCollection(QueryParameters queryParameters,
249
249
} ) , "previous" , "GET" ) ) ;
250
250
}
251
251
252
+ var posturl = _urlHelper . Link ( nameof ( AddFood ) , new { version = version . ToString ( ) } ) ;
253
+
252
254
links . Add (
253
- new LinkDto ( _urlHelper . Link ( nameof ( AddFood ) , null ) ,
255
+ new LinkDto ( posturl ,
254
256
"create_food" ,
255
257
"POST" ) ) ;
256
258
257
259
return links ;
258
260
}
259
261
260
- private dynamic ExpandSingleFoodItem ( FoodItem foodItem )
262
+ private dynamic ExpandSingleFoodItem ( FoodEntity foodItem , ApiVersion version )
261
263
{
262
- var links = GetLinks ( foodItem . Id ) ;
263
- FoodItemDto item = _mapper . Map < FoodItemDto > ( foodItem ) ;
264
+ var links = GetLinks ( foodItem . Id , version ) ;
265
+ FoodDto item = _mapper . Map < FoodDto > ( foodItem ) ;
264
266
265
267
var resourceToReturn = item . ToDynamic ( ) as IDictionary < string , object > ;
266
268
resourceToReturn . Add ( "links" , links ) ;
267
269
268
270
return resourceToReturn ;
269
271
}
270
272
271
- private IEnumerable < LinkDto > GetLinks ( int id )
273
+ private IEnumerable < LinkDto > GetLinks ( int id , ApiVersion version )
272
274
{
273
275
var links = new List < LinkDto > ( ) ;
274
276
277
+ var getLink = _urlHelper . Link ( nameof ( GetSingleFood ) , new { version = version . ToString ( ) , id = id } ) ;
278
+
275
279
links . Add (
276
- new LinkDto ( _urlHelper . Link ( nameof ( GetSingleFood ) , new { id = id } ) ,
277
- "self" ,
278
- "GET" ) ) ;
280
+ new LinkDto ( getLink , "self" , "GET" ) ) ;
281
+
282
+ var deleteLink = _urlHelper . Link ( nameof ( RemoveFood ) , new { version = version . ToString ( ) , id = id } ) ;
279
283
280
284
links . Add (
281
- new LinkDto ( _urlHelper . Link ( nameof ( RemoveFood ) , new { id = id } ) ,
285
+ new LinkDto ( deleteLink ,
282
286
"delete_food" ,
283
287
"DELETE" ) ) ;
284
288
289
+ var createLink = _urlHelper . Link ( nameof ( AddFood ) , new { version = version . ToString ( ) } ) ;
290
+
285
291
links . Add (
286
- new LinkDto ( _urlHelper . Link ( nameof ( AddFood ) , null ) ,
292
+ new LinkDto ( createLink ,
287
293
"create_food" ,
288
294
"POST" ) ) ;
289
295
296
+ var updateLink = _urlHelper . Link ( nameof ( UpdateFood ) , new { version = version . ToString ( ) , id = id } ) ;
297
+
290
298
links . Add (
291
- new LinkDto ( _urlHelper . Link ( nameof ( UpdateFood ) , new { id = id } ) ,
299
+ new LinkDto ( updateLink ,
292
300
"update_food" ,
293
301
"PUT" ) ) ;
294
302
0 commit comments