refactor : add sort by main in category

release
Amir Hossein Khademi 2024-01-23 15:20:42 +03:30
parent f29a1a181e
commit e2ed3d100a
5 changed files with 13 additions and 11 deletions

View File

@ -1 +1 @@
0.0.0.3 0.0.0.4

View File

@ -30,8 +30,8 @@ public class ProductCategoryController : ICarterModule
} }
// GET:Get All Entity // GET:Get All Entity
public async Task<IResult> GetAllAsync([FromQuery]int? page,IMediator mediator, CancellationToken cancellationToken) public async Task<IResult> GetAllAsync([FromQuery]int? page, [FromQuery]bool? sortByMain,IMediator mediator, CancellationToken cancellationToken)
=> TypedResults.Ok(await mediator.Send(new GetProductCategoriesQuery(page),cancellationToken)); => TypedResults.Ok(await mediator.Send(new GetProductCategoriesQuery(page, sortByMain),cancellationToken));
// GET:Get An Entity By Id // GET:Get An Entity By Id
public async Task<IResult> GetAsync(Guid id, IMediator mediator, CancellationToken cancellationToken) public async Task<IResult> GetAsync(Guid id, IMediator mediator, CancellationToken cancellationToken)

View File

@ -6,8 +6,8 @@
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<InvariantGlobalization>true</InvariantGlobalization> <InvariantGlobalization>true</InvariantGlobalization>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS> <DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<AssemblyVersion>0.0.0.2</AssemblyVersion> <AssemblyVersion>0.0.0.4</AssemblyVersion>
<FileVersion>0.0.0.2</FileVersion> <FileVersion>0.0.0.4</FileVersion>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

@ -1,4 +1,4 @@
namespace NetinaShop.Domain.CommandQueries.Queries; namespace NetinaShop.Domain.CommandQueries.Queries;
public record GetProductCategoryQuery(Guid Id) : IRequest<ProductCategoryLDto>; public record GetProductCategoryQuery(Guid Id) : IRequest<ProductCategoryLDto>;
public record GetProductCategoriesQuery(int? page) : IRequest<List<ProductCategorySDto>>; public record GetProductCategoriesQuery(int? Page , bool? SortByMain) : IRequest<List<ProductCategorySDto>>;

View File

@ -13,12 +13,12 @@ public class GetProductCategoriesQueryHandler : IRequestHandler<GetProductCatego
public async Task<List<ProductCategorySDto>> Handle(GetProductCategoriesQuery request, CancellationToken cancellationToken) public async Task<List<ProductCategorySDto>> Handle(GetProductCategoriesQuery request, CancellationToken cancellationToken)
{ {
List<ProductCategorySDto> groupCats; List<ProductCategorySDto> groupCats;
if (request.page != null) if (request.Page != null)
{ {
groupCats = await _repositoryWrapper.SetRepository<ProductCategory>() groupCats = await _repositoryWrapper.SetRepository<ProductCategory>()
.TableNoTracking .TableNoTracking
.OrderByDescending(c=>c.CreatedAt) .OrderByDescending(c => c.CreatedAt)
.Skip(request.page.Value * 15).Take(15) .Skip(request.Page.Value * 15).Take(15)
.Select(CategoryMapper.ProjectToSDto) .Select(CategoryMapper.ProjectToSDto)
.ToListAsync(cancellationToken); .ToListAsync(cancellationToken);
} }
@ -30,11 +30,13 @@ public class GetProductCategoriesQueryHandler : IRequestHandler<GetProductCatego
.ToListAsync(cancellationToken); .ToListAsync(cancellationToken);
} }
if (request.SortByMain != null && !request.SortByMain.Value)
return groupCats;
foreach (var cat in groupCats) foreach (var cat in groupCats)
cat.Children = groupCats.Where(c => c.ParentId == cat.Id).ToList(); cat.Children = groupCats.Where(c => c.ParentId == cat.Id).ToList();
var mainCats = groupCats.Where(c => c.IsMain).ToList(); return groupCats.Where(c => c.IsMain).ToList();
return mainCats;
} }
} }