From 7c82bf10834ff91f7900bde71602e15010265709 Mon Sep 17 00:00:00 2001 From: HaiHungNguyenn Date: Mon, 19 Feb 2024 12:59:07 +0700 Subject: [PATCH] [PackageManagement][Hai] Update new method for pagin --- Domus.Api/Controllers/PackagesController.cs | 15 +++-- Domus.Common/Helpers/DataAccessHelper.cs | 2 +- .../Implementations/PackageService.cs | 56 ++++++++++++++++++- Domus.Service/Interfaces/IPackageService.cs | 1 + 4 files changed, 68 insertions(+), 6 deletions(-) diff --git a/Domus.Api/Controllers/PackagesController.cs b/Domus.Api/Controllers/PackagesController.cs index 13f7813..d304ee4 100644 --- a/Domus.Api/Controllers/PackagesController.cs +++ b/Domus.Api/Controllers/PackagesController.cs @@ -59,11 +59,18 @@ public async Task DeletePackage(Guid id) await _packageService.DeletePackage(id).ConfigureAwait(false)).ConfigureAwait(false); } - [HttpGet] - public async Task GetPackageByName(string name) + // [HttpGet] + // public async Task GetPackageByName(string name) + // { + // return await ExecuteServiceLogic(async () => + // await _packageService.GetPackageByName(name).ConfigureAwait(false)).ConfigureAwait(false); + // } + [HttpPost("search")] + public async Task SearchProducts(BaseSearchRequest request) { - return await ExecuteServiceLogic(async () => - await _packageService.GetPackageByName(name).ConfigureAwait(false)).ConfigureAwait(false); + return await ExecuteServiceLogic( + async () => await _packageService.SearchPackages(request).ConfigureAwait(false) + ).ConfigureAwait(false); } } diff --git a/Domus.Common/Helpers/DataAccessHelper.cs b/Domus.Common/Helpers/DataAccessHelper.cs index c9d9e1b..8d30970 100644 --- a/Domus.Common/Helpers/DataAccessHelper.cs +++ b/Domus.Common/Helpers/DataAccessHelper.cs @@ -17,7 +17,7 @@ private static IConfiguration Configuration } public static void InitConfiguration(IConfiguration configuration) - { + { _configuration = configuration; } diff --git a/Domus.Service/Implementations/PackageService.cs b/Domus.Service/Implementations/PackageService.cs index b40779f..ddd6637 100644 --- a/Domus.Service/Implementations/PackageService.cs +++ b/Domus.Service/Implementations/PackageService.cs @@ -1,4 +1,5 @@ using System.Diagnostics.CodeAnalysis; +using System.Linq.Expressions; using AutoMapper; using AutoMapper.QueryableExtensions; using Domus.Common.Helpers; @@ -10,7 +11,6 @@ using Domus.Service.Models; using Domus.Service.Models.Requests.Base; using Domus.Service.Models.Requests.OfferedPackages; -using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; namespace Domus.Service.Implementations; @@ -132,4 +132,58 @@ public async Task GetPackageByName(string name) Data = packages.ProjectTo(_mapper.ConfigurationProvider) }; } + public async Task SearchPackages(BaseSearchRequest request) + { + var packages = (await _packageRepository.FindAsync(pk => !pk.IsDeleted)).ToList(); + + foreach (var searchInfo in request.DisjunctionSearchInfos) + { + packages = packages + .Where(p => ReflectionHelper.GetStringValueByName(typeof(Package), searchInfo.FieldName, p) + .Contains(searchInfo.Keyword, StringComparison.OrdinalIgnoreCase)) + .ToList(); + } + + if (request.ConjunctionSearchInfos.Any()) + { + var initialSearchInfo = request.ConjunctionSearchInfos.First(); + Expression> conjunctionWhere = p => ReflectionHelper.GetStringValueByName(typeof(Package), initialSearchInfo.FieldName, p) + .Contains(initialSearchInfo.Keyword, StringComparison.OrdinalIgnoreCase); + + foreach (var (searchInfo, i) in request.ConjunctionSearchInfos.Select((value, i) => (value, i))) + { + if (i == 0) + continue; + + Expression> whereExpr = p => ReflectionHelper.GetStringValueByName(typeof(Package), searchInfo.FieldName, p) + .Contains(searchInfo.Keyword, StringComparison.OrdinalIgnoreCase); + conjunctionWhere = ExpressionHelper.CombineOrExpressions(conjunctionWhere, whereExpr); + } + + packages = packages.Where(conjunctionWhere.Compile()).ToList(); + } + + if (request.SortInfos.Any()) + { + request.SortInfos = request.SortInfos.OrderBy(si => si.Priority).ToList(); + var initialSortInfo = request.SortInfos.First(); + Expression> orderExpr = p => ReflectionHelper.GetValueByName(typeof(Package), initialSortInfo.FieldName, p); + + packages = initialSortInfo.Descending ? packages.OrderByDescending(orderExpr.Compile()).ToList() : packages.OrderBy(orderExpr.Compile()).ToList(); + + foreach (var (sortInfo, i) in request.SortInfos.Select((value, i) => (value, i))) + { + if (i == 0) + continue; + + orderExpr = p => ReflectionHelper.GetValueByName(typeof(Package), sortInfo.FieldName, p); + packages = sortInfo.Descending ? packages.OrderByDescending(orderExpr.Compile()).ToList() : packages.OrderBy(orderExpr.Compile()).ToList(); + } + } + + var paginatedResult = PaginationHelper.BuildPaginatedResult(_mapper, packages, request.PageSize, request.PageIndex); + + return new ServiceActionResult(true) { Data = paginatedResult }; + } + } \ No newline at end of file diff --git a/Domus.Service/Interfaces/IPackageService.cs b/Domus.Service/Interfaces/IPackageService.cs index 13a0557..29b7590 100644 --- a/Domus.Service/Interfaces/IPackageService.cs +++ b/Domus.Service/Interfaces/IPackageService.cs @@ -14,4 +14,5 @@ public interface IPackageService : IAutoRegisterable Task UpdatePackage(PackageRequest request, Guid packageId); Task DeletePackage(Guid packageId); Task GetPackageByName(string name); + Task SearchPackages(BaseSearchRequest request); } \ No newline at end of file