From 8e4c186d5ce43136dbf94b07063633ee328725b1 Mon Sep 17 00:00:00 2001 From: Odumosu Matthew Babatunde <105985964+iamcymentho@users.noreply.github.com> Date: Wed, 25 Oct 2023 12:34:33 +0100 Subject: [PATCH] Update FoodsController.cs Description: Refactors the GetAllFoods controller method to achieve a cleaner and more maintainable codebase. Business logic for retrieving food items, creating pagination metadata, and building links has been moved to the FoodService class, adhering to the Single Responsibility Principle. This separation of concerns enhances code organization and readability. The Response.Headers.Add("X-Pagination", ...) line is retained in the controller, as it directly modifies the response headers. This change helps maintain a clear distinction between controller and service responsibilities. Issue: [Include the issue number or reference, if applicable] This commit addresses the issue [Insert Issue Number or Reference] and aligns with best practices for structuring .NET Web API applications. The separation of concerns ensures a more modular and testable codebase. --- .../Controllers/v1/FoodsController.cs | 30 +++++-------------- 1 file changed, 8 insertions(+), 22 deletions(-) diff --git a/SampleWebApiAspNetCore/Controllers/v1/FoodsController.cs b/SampleWebApiAspNetCore/Controllers/v1/FoodsController.cs index 892e327..3bd84dc 100644 --- a/SampleWebApiAspNetCore/Controllers/v1/FoodsController.cs +++ b/SampleWebApiAspNetCore/Controllers/v1/FoodsController.cs @@ -19,13 +19,16 @@ public class FoodsController : ControllerBase private readonly IFoodRepository _foodRepository; private readonly IMapper _mapper; private readonly ILinkService _linkService; + private readonly IFoodService _foodService public FoodsController( IFoodRepository foodRepository, + IFoodService foodService, IMapper mapper, ILinkService linkService) { _foodRepository = foodRepository; + _foodService = foodService; _mapper = mapper; _linkService = linkService; } @@ -33,30 +36,13 @@ public FoodsController( [HttpGet(Name = nameof(GetAllFoods))] public ActionResult GetAllFoods(ApiVersion version, [FromQuery] QueryParameters queryParameters) { - List foodItems = _foodRepository.GetAll(queryParameters).ToList(); - - var allItemCount = _foodRepository.Count(); - - var paginationMetadata = new - { - totalCount = allItemCount, - pageSize = queryParameters.PageCount, - currentPage = queryParameters.Page, - totalPages = queryParameters.GetTotalPages(allItemCount) - }; - - Response.Headers.Add("X-Pagination", JsonSerializer.Serialize(paginationMetadata)); - - var links = _linkService.CreateLinksForCollection(queryParameters, allItemCount, version); - var toReturn = foodItems.Select(x => _linkService.ExpandSingleFoodItem(x, x.Id, version)); - - return Ok(new - { - value = toReturn, - links = links - }); + var result = _foodService.GetAllFoods(queryParameters, version); + // Add the X-Pagination header to the response + Response.Headers.Add("X-Pagination", JsonSerializer.Serialize(result.PaginationMetadata)); + return Ok(result); } + [HttpGet] [Route("{id:int}", Name = nameof(GetSingleFood))] public ActionResult GetSingleFood(ApiVersion version, int id)