From bbdd8338f40c00ba0920f6fa736859f26a389679 Mon Sep 17 00:00:00 2001 From: "Amir.H Khademi" Date: Sat, 27 Jan 2024 13:00:16 +0330 Subject: [PATCH] feat : add category and brand name in filters --- .version | 2 +- NetinaShop.Api/Controller/BrandController.cs | 19 +------- .../Controller/ProductCategoryController.cs | 10 ++-- NetinaShop.Api/Controller/SeedController.cs | 4 +- NetinaShop.Api/NetinaShop.Api.csproj | 4 +- .../Commands/ProductCategoryCommands.cs | 6 +-- .../CommandQueries/Queries/BrandQueries.cs | 4 ++ .../Queries/ProductCategoryQueries.cs | 2 +- .../Handlers/Brands/GetBrandsQueryHandler.cs | 47 +++++++++++++++++++ .../CreateProductCategoryCommandHandler.cs | 4 +- .../DeleteProductCategoryCommandHandler.cs | 4 +- .../GetProductCategoriesQueryHandler.cs | 22 +++++++-- ...r.cs => GetProductCategoryQueryHandler.cs} | 4 +- .../UpdateProductCategoryCommandHandler.cs | 4 +- 14 files changed, 92 insertions(+), 44 deletions(-) create mode 100644 NetinaShop.Domain/CommandQueries/Queries/BrandQueries.cs create mode 100644 NetinaShop.Repository/Handlers/Brands/GetBrandsQueryHandler.cs rename NetinaShop.Repository/Handlers/ProductCategories/{GetCategoryQueryHandler.cs => GetProductCategoryQueryHandler.cs} (79%) diff --git a/.version b/.version index 492f8f9..72b6c78 100644 --- a/.version +++ b/.version @@ -1 +1 @@ -0.0.0.6 \ No newline at end of file +0.0.0.7 \ No newline at end of file diff --git a/NetinaShop.Api/Controller/BrandController.cs b/NetinaShop.Api/Controller/BrandController.cs index 095bbe6..36c8051 100644 --- a/NetinaShop.Api/Controller/BrandController.cs +++ b/NetinaShop.Api/Controller/BrandController.cs @@ -32,24 +32,9 @@ public class BrandController : ICarterModule } // GET:Get All Entity - public async Task GetAllAsync([FromQuery] int? page, IRepositoryWrapper repositoryWrapper, - CancellationToken cancellationToken) + public async Task GetAllAsync([FromQuery] int? page, [FromQuery]string? brandName, IMediator mediator, CancellationToken cancellationToken) { - if (page != null) - { - return TypedResults.Ok(await repositoryWrapper.SetRepository().TableNoTracking - .OrderByDescending(b => b.CreatedAt).Skip(page.Value * 10).Take(10) - .Select(BrandMapper.ProjectToSDto) - .ToListAsync(cancellationToken)); - } - else - { - - return TypedResults.Ok(await repositoryWrapper.SetRepository().TableNoTracking - .OrderByDescending(b => b.CreatedAt) - .Select(BrandMapper.ProjectToSDto) - .ToListAsync(cancellationToken)); - } + return TypedResults.Ok(await mediator.Send(new GetBrandsQuery(Page: page, BrandName: brandName),cancellationToken)); } // GET:Get An Entity By Id diff --git a/NetinaShop.Api/Controller/ProductCategoryController.cs b/NetinaShop.Api/Controller/ProductCategoryController.cs index 626681f..b5c5407 100644 --- a/NetinaShop.Api/Controller/ProductCategoryController.cs +++ b/NetinaShop.Api/Controller/ProductCategoryController.cs @@ -30,22 +30,22 @@ public class ProductCategoryController : ICarterModule } // GET:Get All Entity - public async Task GetAllAsync([FromQuery]int? page, [FromQuery]bool? sortByMain,IMediator mediator, CancellationToken cancellationToken) - => TypedResults.Ok(await mediator.Send(new GetProductCategoriesQuery(page, sortByMain),cancellationToken)); + public async Task GetAllAsync([FromQuery]int? page, [FromQuery]bool? sortByMain , [FromQuery]string? categoryName,IMediator mediator, CancellationToken cancellationToken) + => TypedResults.Ok(await mediator.Send(new GetProductCategoriesQuery(Page: page,SortByMain: sortByMain , CategoryName: categoryName),cancellationToken)); // GET:Get An Entity By Id public async Task GetAsync(Guid id, IMediator mediator, CancellationToken cancellationToken) => TypedResults.Ok(await mediator.Send(new GetProductCategoryQuery(id),cancellationToken)); // POST:Create Entity - public async Task Post([FromBody] ProductCreateCategoryCommand request, IMediator mediator, CancellationToken cancellationToken) + public async Task Post([FromBody] CreateProductCategoryCommand request, IMediator mediator, CancellationToken cancellationToken) => TypedResults.Ok(await mediator.Send(request, cancellationToken)); // PUT:Update Entity - public async Task Put([FromBody] ProductUpdateCategoryCommand request, IMediator mediator, CancellationToken cancellationToken) + public async Task Put([FromBody] UpdateProductCategoryCommand request, IMediator mediator, CancellationToken cancellationToken) => TypedResults.Ok(await mediator.Send(request, cancellationToken)); // DELETE:Delete Entity public async Task Delete(Guid id, IMediator mediator, CancellationToken cancellationToken) - => TypedResults.Ok(await mediator.Send(new ProductDeleteCategoryCommand(id), cancellationToken)); + => TypedResults.Ok(await mediator.Send(new DeleteProductCategoryCommand(id), cancellationToken)); } \ No newline at end of file diff --git a/NetinaShop.Api/Controller/SeedController.cs b/NetinaShop.Api/Controller/SeedController.cs index 11f6d82..fc391d6 100644 --- a/NetinaShop.Api/Controller/SeedController.cs +++ b/NetinaShop.Api/Controller/SeedController.cs @@ -43,12 +43,12 @@ public class SeedController : ICarterModule if (key != "kKAYskyG8xPxKnJrHkuYxub4Ao2bnz7AOmNtwDT0RaqzaG7ZvbvaP29tCrC8wJ823RczJFXOIQT2bDOec4F38A==") throw new AppException("Key is not valid", ApiResultStatusCode.UnAuthorized); Dictionary categories = new Dictionary(); - var baseCat = await mediator.Send(new ProductCreateCategoryCommand("دسته بندی نشده", "محصولات دسته بندی نشده", default, + var baseCat = await mediator.Send(new CreateProductCategoryCommand("دسته بندی نشده", "محصولات دسته بندی نشده", default, new List())); categories.Add(0,baseCat.Id); foreach (var requestDto in request) { - var lDto = await mediator.Send(new ProductCreateCategoryCommand(requestDto.Name,requestDto.Description,default,new List()), cancellationToken); + var lDto = await mediator.Send(new CreateProductCategoryCommand(requestDto.Name,requestDto.Description,default,new List()), cancellationToken); categories.Add(requestDto.BaseCategoryId,lDto.Id); } diff --git a/NetinaShop.Api/NetinaShop.Api.csproj b/NetinaShop.Api/NetinaShop.Api.csproj index b74ce38..6b1cc04 100644 --- a/NetinaShop.Api/NetinaShop.Api.csproj +++ b/NetinaShop.Api/NetinaShop.Api.csproj @@ -6,8 +6,8 @@ enable true Linux - 0.0.0.6 - 0.0.0.6 + 0.0.0.7 + 0.0.0.7 diff --git a/NetinaShop.Domain/CommandQueries/Commands/ProductCategoryCommands.cs b/NetinaShop.Domain/CommandQueries/Commands/ProductCategoryCommands.cs index 4b068b0..1747a2b 100644 --- a/NetinaShop.Domain/CommandQueries/Commands/ProductCategoryCommands.cs +++ b/NetinaShop.Domain/CommandQueries/Commands/ProductCategoryCommands.cs @@ -1,16 +1,16 @@ namespace NetinaShop.Domain.CommandQueries.Commands; -public sealed record ProductCreateCategoryCommand( +public sealed record CreateProductCategoryCommand( string Name, string Description, Guid ParentId, List Files) : IRequest; -public sealed record ProductUpdateCategoryCommand( +public sealed record UpdateProductCategoryCommand( Guid Id, string Name, string Description, Guid ParentId, List Files) : IRequest; -public sealed record ProductDeleteCategoryCommand(Guid Id) : IRequest; \ No newline at end of file +public sealed record DeleteProductCategoryCommand(Guid Id) : IRequest; \ No newline at end of file diff --git a/NetinaShop.Domain/CommandQueries/Queries/BrandQueries.cs b/NetinaShop.Domain/CommandQueries/Queries/BrandQueries.cs new file mode 100644 index 0000000..d6700e2 --- /dev/null +++ b/NetinaShop.Domain/CommandQueries/Queries/BrandQueries.cs @@ -0,0 +1,4 @@ +namespace NetinaShop.Domain.CommandQueries.Queries; + +public record GetBrandQuery(Guid Id) : IRequest; +public record GetBrandsQuery(int? Page, string? BrandName) : IRequest>; \ No newline at end of file diff --git a/NetinaShop.Domain/CommandQueries/Queries/ProductCategoryQueries.cs b/NetinaShop.Domain/CommandQueries/Queries/ProductCategoryQueries.cs index bcc12a4..012a057 100644 --- a/NetinaShop.Domain/CommandQueries/Queries/ProductCategoryQueries.cs +++ b/NetinaShop.Domain/CommandQueries/Queries/ProductCategoryQueries.cs @@ -1,4 +1,4 @@ namespace NetinaShop.Domain.CommandQueries.Queries; public record GetProductCategoryQuery(Guid Id) : IRequest; -public record GetProductCategoriesQuery(int? Page , bool? SortByMain) : IRequest>; \ No newline at end of file +public record GetProductCategoriesQuery(int? Page , bool? SortByMain,string? CategoryName) : IRequest>; \ No newline at end of file diff --git a/NetinaShop.Repository/Handlers/Brands/GetBrandsQueryHandler.cs b/NetinaShop.Repository/Handlers/Brands/GetBrandsQueryHandler.cs new file mode 100644 index 0000000..2fa29ad --- /dev/null +++ b/NetinaShop.Repository/Handlers/Brands/GetBrandsQueryHandler.cs @@ -0,0 +1,47 @@ +using NetinaShop.Domain.Entities.Brands; + +namespace NetinaShop.Repository.Handlers.Brands; + +public class GetBrandsQueryHandler : IRequestHandler> +{ + private readonly IRepositoryWrapper _repositoryWrapper; + + public GetBrandsQueryHandler(IRepositoryWrapper repositoryWrapper) + { + _repositoryWrapper = repositoryWrapper; + } + public async Task> Handle(GetBrandsQuery request, CancellationToken cancellationToken) + { + IQueryable baseBrands; + List brands; + if (request.BrandName != null) + { + baseBrands = _repositoryWrapper.SetRepository() + .TableNoTracking + .Where(b => b.Name.Trim().Contains(request.BrandName.Trim())) + .OrderByDescending(b => b.CreatedAt); + } + else + { + + baseBrands = _repositoryWrapper.SetRepository().TableNoTracking + .OrderByDescending(b => b.CreatedAt); + } + + if (request.Page != null) + { + brands = await baseBrands.Skip(request.Page.Value * 10).Take(10) + .Select(BrandMapper.ProjectToSDto) + .ToListAsync(cancellationToken); + } + else + { + + brands = await baseBrands + .Select(BrandMapper.ProjectToSDto) + .ToListAsync(cancellationToken); + } + + return brands; + } +} \ No newline at end of file diff --git a/NetinaShop.Repository/Handlers/ProductCategories/CreateProductCategoryCommandHandler.cs b/NetinaShop.Repository/Handlers/ProductCategories/CreateProductCategoryCommandHandler.cs index 0295907..85fe110 100644 --- a/NetinaShop.Repository/Handlers/ProductCategories/CreateProductCategoryCommandHandler.cs +++ b/NetinaShop.Repository/Handlers/ProductCategories/CreateProductCategoryCommandHandler.cs @@ -2,7 +2,7 @@ namespace NetinaShop.Repository.Handlers.ProductCategories; -public class CreateProductCategoryCommandHandler : IRequestHandler +public class CreateProductCategoryCommandHandler : IRequestHandler { private readonly IRepositoryWrapper _repositoryWrapper; @@ -10,7 +10,7 @@ public class CreateProductCategoryCommandHandler : IRequestHandler Handle(ProductCreateCategoryCommand request, CancellationToken cancellationToken) + public async Task Handle(CreateProductCategoryCommand request, CancellationToken cancellationToken) { var ent = ProductCategory.Create(request.Name, request.Description); if (request.ParentId != default) diff --git a/NetinaShop.Repository/Handlers/ProductCategories/DeleteProductCategoryCommandHandler.cs b/NetinaShop.Repository/Handlers/ProductCategories/DeleteProductCategoryCommandHandler.cs index 1fd129c..ca1551c 100644 --- a/NetinaShop.Repository/Handlers/ProductCategories/DeleteProductCategoryCommandHandler.cs +++ b/NetinaShop.Repository/Handlers/ProductCategories/DeleteProductCategoryCommandHandler.cs @@ -2,7 +2,7 @@ namespace NetinaShop.Repository.Handlers.ProductCategories; -public class DeleteProductCategoryCommandHandler : IRequestHandler +public class DeleteProductCategoryCommandHandler : IRequestHandler { private readonly IRepositoryWrapper _repositoryWrapper; @@ -10,7 +10,7 @@ public class DeleteProductCategoryCommandHandler : IRequestHandler Handle(ProductDeleteCategoryCommand request, CancellationToken cancellationToken) + public async Task Handle(DeleteProductCategoryCommand request, CancellationToken cancellationToken) { var ent = await _repositoryWrapper.SetRepository().TableNoTracking diff --git a/NetinaShop.Repository/Handlers/ProductCategories/GetProductCategoriesQueryHandler.cs b/NetinaShop.Repository/Handlers/ProductCategories/GetProductCategoriesQueryHandler.cs index ba755da..7b1049a 100644 --- a/NetinaShop.Repository/Handlers/ProductCategories/GetProductCategoriesQueryHandler.cs +++ b/NetinaShop.Repository/Handlers/ProductCategories/GetProductCategoriesQueryHandler.cs @@ -12,20 +12,32 @@ public class GetProductCategoriesQueryHandler : IRequestHandler> Handle(GetProductCategoriesQuery request, CancellationToken cancellationToken) { + IQueryable basCategories; List groupCats; + if (request.CategoryName != null) + { + basCategories = _repositoryWrapper.SetRepository() + .TableNoTracking + .Where(c => c.Name.Trim().Contains(request.CategoryName.Trim())) + .OrderByDescending(c => c.CreatedAt); + } + else + { + + basCategories = _repositoryWrapper.SetRepository() + .TableNoTracking + .OrderByDescending(c=>c.CreatedAt); + } if (request.Page != null) { - groupCats = await _repositoryWrapper.SetRepository() - .TableNoTracking - .OrderByDescending(c => c.CreatedAt) + groupCats = await basCategories .Skip(request.Page.Value * 15).Take(15) .Select(CategoryMapper.ProjectToSDto) .ToListAsync(cancellationToken); } else { - groupCats = await _repositoryWrapper.SetRepository() - .TableNoTracking + groupCats = await basCategories .Select(CategoryMapper.ProjectToSDto) .ToListAsync(cancellationToken); } diff --git a/NetinaShop.Repository/Handlers/ProductCategories/GetCategoryQueryHandler.cs b/NetinaShop.Repository/Handlers/ProductCategories/GetProductCategoryQueryHandler.cs similarity index 79% rename from NetinaShop.Repository/Handlers/ProductCategories/GetCategoryQueryHandler.cs rename to NetinaShop.Repository/Handlers/ProductCategories/GetProductCategoryQueryHandler.cs index 5cfb1fa..a5b68de 100644 --- a/NetinaShop.Repository/Handlers/ProductCategories/GetCategoryQueryHandler.cs +++ b/NetinaShop.Repository/Handlers/ProductCategories/GetProductCategoryQueryHandler.cs @@ -2,11 +2,11 @@ namespace NetinaShop.Repository.Handlers.ProductCategories; -public class GetCategoryQueryHandler : IRequestHandler +public class GetProductCategoryQueryHandler : IRequestHandler { private readonly IRepositoryWrapper _repositoryWrapper; - public GetCategoryQueryHandler(IRepositoryWrapper repositoryWrapper) + public GetProductCategoryQueryHandler(IRepositoryWrapper repositoryWrapper) { _repositoryWrapper = repositoryWrapper; } diff --git a/NetinaShop.Repository/Handlers/ProductCategories/UpdateProductCategoryCommandHandler.cs b/NetinaShop.Repository/Handlers/ProductCategories/UpdateProductCategoryCommandHandler.cs index f92169c..d94f585 100644 --- a/NetinaShop.Repository/Handlers/ProductCategories/UpdateProductCategoryCommandHandler.cs +++ b/NetinaShop.Repository/Handlers/ProductCategories/UpdateProductCategoryCommandHandler.cs @@ -2,7 +2,7 @@ namespace NetinaShop.Repository.Handlers.ProductCategories; -public class UpdateProductCategoryCommandHandler : IRequestHandler +public class UpdateProductCategoryCommandHandler : IRequestHandler { private readonly IRepositoryWrapper _repositoryWrapper; @@ -10,7 +10,7 @@ public class UpdateProductCategoryCommandHandler : IRequestHandler Handle(ProductUpdateCategoryCommand request, CancellationToken cancellationToken) + public async Task Handle(UpdateProductCategoryCommand request, CancellationToken cancellationToken) { var ent = await _repositoryWrapper.SetRepository().TableNoTracking .FirstOrDefaultAsync(c => c.Id == request.Id, cancellationToken);