From 6e474303da9402ec1779ecc79e715f8614dd073d Mon Sep 17 00:00:00 2001 From: Odumosu Matthew Babatunde <105985964+iamcymentho@users.noreply.github.com> Date: Wed, 25 Oct 2023 12:24:29 +0100 Subject: [PATCH] Create FoodService.cs Description: I introduced the FoodService class responsible for handling business logic related to food items. This class encapsulates the operations for retrieving food items, creating pagination metadata, and building links. It plays a pivotal role in keeping the controller clean and focused on handling HTTP requests while ensuring separation of concerns. --- .../Services/FoodService.cs | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 SampleWebApiAspNetCore/Services/FoodService.cs diff --git a/SampleWebApiAspNetCore/Services/FoodService.cs b/SampleWebApiAspNetCore/Services/FoodService.cs new file mode 100644 index 0000000..4ed0c79 --- /dev/null +++ b/SampleWebApiAspNetCore/Services/FoodService.cs @@ -0,0 +1,30 @@ +using Microsoft.AspNetCore.Mvc; +using SampleWebApiAspNetCore.Models; + +namespace SampleWebApiAspNetCore.Services +{ +public class FoodService : IFoodService +{ + private readonly IFoodRepository _foodRepository; + private readonly ILinkService _linkService; + + public FoodService(IFoodRepository foodRepository, ILinkService linkService) + { + _foodRepository = foodRepository; + _linkService = linkService; + } + + public FoodResultDto GetAllFoods(QueryParameters queryParameters, ApiVersion version) + { + var foodItems = _foodRepository.GetAll(queryParameters); + var allItemCount = _foodRepository.Count(); + var paginationMetadata = new PaginationMetadata(allItemCount, queryParameters); + + var links = _linkService.CreateLinksForCollection(queryParameters, allItemCount, version); + var toReturn = foodItems.Select(x => _linkService.ExpandSingleFoodItem(x, x.Id, version)); + + return new FoodResultDto(toReturn, links, paginationMetadata); + } +} + +}