Skip to content

Commit d017898

Browse files
improved hateoas
1 parent 7ce49ce commit d017898

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

SampleWebApiAspNetCore/Controllers/v1/FoodsController.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
using SampleWebApiAspNetCore.Models;
99
using SampleWebApiAspNetCore.Repositories;
1010
using System.Text.Json;
11+
using System;
12+
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;
1113

1214
namespace SampleWebApiAspNetCore.Controllers.v1
1315
{
@@ -91,14 +93,15 @@ public ActionResult<FoodDto> AddFood(ApiVersion version, [FromBody] FoodCreateDt
9193
}
9294

9395
FoodEntity newFoodItem = _foodRepository.GetSingle(toAdd.Id);
96+
FoodDto foodDto = _mapper.Map<FoodDto>(newFoodItem);
9497

9598
return CreatedAtRoute(nameof(GetSingleFood),
9699
new { version = version.ToString(), id = newFoodItem.Id },
97-
_mapper.Map<FoodDto>(newFoodItem));
100+
_linkService.ExpandSingleFoodItem(foodDto, foodDto.Id, version));
98101
}
99102

100103
[HttpPatch("{id:int}", Name = nameof(PartiallyUpdateFood))]
101-
public ActionResult<FoodDto> PartiallyUpdateFood(int id, [FromBody] JsonPatchDocument<FoodUpdateDto> patchDoc)
104+
public ActionResult<FoodDto> PartiallyUpdateFood(ApiVersion version, int id, [FromBody] JsonPatchDocument<FoodUpdateDto> patchDoc)
102105
{
103106
if (patchDoc == null)
104107
{
@@ -130,7 +133,9 @@ public ActionResult<FoodDto> PartiallyUpdateFood(int id, [FromBody] JsonPatchDoc
130133
throw new Exception("Updating a fooditem failed on save.");
131134
}
132135

133-
return Ok(_mapper.Map<FoodDto>(updated));
136+
FoodDto foodDto = _mapper.Map<FoodDto>(updated);
137+
138+
return Ok(_linkService.ExpandSingleFoodItem(foodDto, foodDto.Id, version));
134139
}
135140

136141
[HttpDelete]
@@ -156,7 +161,7 @@ public ActionResult RemoveFood(int id)
156161

157162
[HttpPut]
158163
[Route("{id:int}", Name = nameof(UpdateFood))]
159-
public ActionResult<FoodDto> UpdateFood(int id, [FromBody] FoodUpdateDto foodUpdateDto)
164+
public ActionResult<FoodDto> UpdateFood(ApiVersion version, int id, [FromBody] FoodUpdateDto foodUpdateDto)
160165
{
161166
if (foodUpdateDto == null)
162167
{
@@ -179,7 +184,9 @@ public ActionResult<FoodDto> UpdateFood(int id, [FromBody] FoodUpdateDto foodUpd
179184
throw new Exception("Updating a fooditem failed on save.");
180185
}
181186

182-
return Ok(_mapper.Map<FoodDto>(existingFoodItem));
187+
FoodDto foodDto = _mapper.Map<FoodDto>(existingFoodItem);
188+
189+
return Ok(_linkService.ExpandSingleFoodItem(foodDto, foodDto.Id, version));
183190
}
184191

185192
[HttpGet("GetRandomMeal", Name = nameof(GetRandomMeal))]

0 commit comments

Comments
 (0)