Skip to content

Commit 4609181

Browse files
better return value handling
1 parent e29353e commit 4609181

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

SampleWebApiAspNetCore/Controllers/v1/FoodsController.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public FoodsController(IUrlHelper urlHelper, IFoodRepository foodRepository)
2828
}
2929

3030
[HttpGet(Name = nameof(GetAllFoods))]
31-
public ActionResult<object> GetAllFoods([FromQuery] QueryParameters queryParameters)
31+
public ActionResult GetAllFoods([FromQuery] QueryParameters queryParameters)
3232
{
3333
List<FoodItem> foodItems = _foodRepository.GetAll(queryParameters).ToList();
3434

@@ -58,7 +58,7 @@ public ActionResult<object> GetAllFoods([FromQuery] QueryParameters queryParamet
5858

5959
[HttpGet]
6060
[Route("{id:int}", Name = nameof(GetSingleFood))]
61-
public ActionResult<OkObjectResult> GetSingleFood(int id)
61+
public ActionResult GetSingleFood(int id)
6262
{
6363
FoodItem foodItem = _foodRepository.GetSingle(id);
6464

@@ -71,7 +71,7 @@ public ActionResult<OkObjectResult> GetSingleFood(int id)
7171
}
7272

7373
[HttpPost(Name = nameof(AddFood))]
74-
public ActionResult<CreatedAtRouteResult> AddFood([FromBody] FoodCreateDto foodCreateDto)
74+
public ActionResult<FoodItemDto> AddFood([FromBody] FoodCreateDto foodCreateDto)
7575
{
7676
if (foodCreateDto == null)
7777
{
@@ -99,7 +99,7 @@ public ActionResult<CreatedAtRouteResult> AddFood([FromBody] FoodCreateDto foodC
9999
}
100100

101101
[HttpPatch("{id:int}", Name = nameof(PartiallyUpdateFood))]
102-
public ActionResult<OkObjectResult> PartiallyUpdateFood(int id, [FromBody] JsonPatchDocument<FoodUpdateDto> patchDoc)
102+
public ActionResult<FoodItemDto> PartiallyUpdateFood(int id, [FromBody] JsonPatchDocument<FoodUpdateDto> patchDoc)
103103
{
104104
if (patchDoc == null)
105105
{
@@ -136,7 +136,7 @@ public ActionResult<OkObjectResult> PartiallyUpdateFood(int id, [FromBody] JsonP
136136

137137
[HttpDelete]
138138
[Route("{id:int}", Name = nameof(RemoveFood))]
139-
public ActionResult<NoContentResult> RemoveFood(int id)
139+
public ActionResult RemoveFood(int id)
140140
{
141141
FoodItem foodItem = _foodRepository.GetSingle(id);
142142

@@ -157,7 +157,7 @@ public ActionResult<NoContentResult> RemoveFood(int id)
157157

158158
[HttpPut]
159159
[Route("{id:int}", Name = nameof(UpdateFood))]
160-
public ActionResult<OkObjectResult> UpdateFood(int id, [FromBody]FoodUpdateDto foodUpdateDto)
160+
public ActionResult<FoodItemDto> UpdateFood(int id, [FromBody]FoodUpdateDto foodUpdateDto)
161161
{
162162
if (foodUpdateDto == null)
163163
{
@@ -189,7 +189,7 @@ public ActionResult<OkObjectResult> UpdateFood(int id, [FromBody]FoodUpdateDto f
189189
}
190190

191191
[HttpGet("GetRandomMeal", Name = nameof(GetRandomMeal))]
192-
public ActionResult<object> GetRandomMeal()
192+
public ActionResult GetRandomMeal()
193193
{
194194
ICollection<FoodItem> foodItems = _foodRepository.GetRandomMeal();
195195

@@ -255,6 +255,11 @@ private List<LinkDto> CreateLinksForCollection(QueryParameters queryParameters,
255255
}), "previous", "GET"));
256256
}
257257

258+
links.Add(
259+
new LinkDto(_urlHelper.Link(nameof(AddFood), null),
260+
"create_food",
261+
"POST"));
262+
258263
return links;
259264
}
260265

SampleWebApiAspNetCore/Controllers/v2/FoodsController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace SampleWebApiAspNetCore.v2.Controllers
77
public class FoodsController : ControllerBase
88
{
99
[HttpGet]
10-
public ActionResult<OkObjectResult> Get()
10+
public ActionResult Get()
1111
{
1212
return Ok("2.0");
1313
}

0 commit comments

Comments
 (0)