feat : add category and brand name in filters

release
Amir Hossein Khademi 2024-01-27 13:00:16 +03:30
parent 04bb1c25dd
commit bbdd8338f4
14 changed files with 92 additions and 44 deletions

View File

@ -1 +1 @@
0.0.0.6
0.0.0.7

View File

@ -32,24 +32,9 @@ public class BrandController : ICarterModule
}
// GET:Get All Entity
public async Task<IResult> GetAllAsync([FromQuery] int? page, IRepositoryWrapper repositoryWrapper,
CancellationToken cancellationToken)
public async Task<IResult> GetAllAsync([FromQuery] int? page, [FromQuery]string? brandName, IMediator mediator, CancellationToken cancellationToken)
{
if (page != null)
{
return TypedResults.Ok(await repositoryWrapper.SetRepository<Brand>().TableNoTracking
.OrderByDescending(b => b.CreatedAt).Skip(page.Value * 10).Take(10)
.Select(BrandMapper.ProjectToSDto)
.ToListAsync(cancellationToken));
}
else
{
return TypedResults.Ok(await repositoryWrapper.SetRepository<Brand>().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

View File

@ -30,22 +30,22 @@ public class ProductCategoryController : ICarterModule
}
// GET:Get All Entity
public async Task<IResult> GetAllAsync([FromQuery]int? page, [FromQuery]bool? sortByMain,IMediator mediator, CancellationToken cancellationToken)
=> TypedResults.Ok(await mediator.Send(new GetProductCategoriesQuery(page, sortByMain),cancellationToken));
public async Task<IResult> 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<IResult> GetAsync(Guid id, IMediator mediator, CancellationToken cancellationToken)
=> TypedResults.Ok(await mediator.Send(new GetProductCategoryQuery(id),cancellationToken));
// POST:Create Entity
public async Task<IResult> Post([FromBody] ProductCreateCategoryCommand request, IMediator mediator, CancellationToken cancellationToken)
public async Task<IResult> Post([FromBody] CreateProductCategoryCommand request, IMediator mediator, CancellationToken cancellationToken)
=> TypedResults.Ok(await mediator.Send(request, cancellationToken));
// PUT:Update Entity
public async Task<IResult> Put([FromBody] ProductUpdateCategoryCommand request, IMediator mediator, CancellationToken cancellationToken)
public async Task<IResult> Put([FromBody] UpdateProductCategoryCommand request, IMediator mediator, CancellationToken cancellationToken)
=> TypedResults.Ok(await mediator.Send(request, cancellationToken));
// DELETE:Delete Entity
public async Task<IResult> 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));
}

View File

@ -43,12 +43,12 @@ public class SeedController : ICarterModule
if (key != "kKAYskyG8xPxKnJrHkuYxub4Ao2bnz7AOmNtwDT0RaqzaG7ZvbvaP29tCrC8wJ823RczJFXOIQT2bDOec4F38A==")
throw new AppException("Key is not valid", ApiResultStatusCode.UnAuthorized);
Dictionary<int,Guid> categories = new Dictionary<int, Guid>();
var baseCat = await mediator.Send(new ProductCreateCategoryCommand("دسته بندی نشده", "محصولات دسته بندی نشده", default,
var baseCat = await mediator.Send(new CreateProductCategoryCommand("دسته بندی نشده", "محصولات دسته بندی نشده", default,
new List<StorageFileSDto>()));
categories.Add(0,baseCat.Id);
foreach (var requestDto in request)
{
var lDto = await mediator.Send(new ProductCreateCategoryCommand(requestDto.Name,requestDto.Description,default,new List<StorageFileSDto>()), cancellationToken);
var lDto = await mediator.Send(new CreateProductCategoryCommand(requestDto.Name,requestDto.Description,default,new List<StorageFileSDto>()), cancellationToken);
categories.Add(requestDto.BaseCategoryId,lDto.Id);
}

View File

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

View File

@ -1,16 +1,16 @@
namespace NetinaShop.Domain.CommandQueries.Commands;
public sealed record ProductCreateCategoryCommand(
public sealed record CreateProductCategoryCommand(
string Name,
string Description,
Guid ParentId,
List<StorageFileSDto> Files) : IRequest<ProductCategoryLDto>;
public sealed record ProductUpdateCategoryCommand(
public sealed record UpdateProductCategoryCommand(
Guid Id,
string Name,
string Description,
Guid ParentId,
List<StorageFileSDto> Files) : IRequest<bool>;
public sealed record ProductDeleteCategoryCommand(Guid Id) : IRequest<bool>;
public sealed record DeleteProductCategoryCommand(Guid Id) : IRequest<bool>;

View File

@ -0,0 +1,4 @@
namespace NetinaShop.Domain.CommandQueries.Queries;
public record GetBrandQuery(Guid Id) : IRequest<BrandLDto>;
public record GetBrandsQuery(int? Page, string? BrandName) : IRequest<List<BrandSDto>>;

View File

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

View File

@ -0,0 +1,47 @@
using NetinaShop.Domain.Entities.Brands;
namespace NetinaShop.Repository.Handlers.Brands;
public class GetBrandsQueryHandler : IRequestHandler<GetBrandsQuery, List<BrandSDto>>
{
private readonly IRepositoryWrapper _repositoryWrapper;
public GetBrandsQueryHandler(IRepositoryWrapper repositoryWrapper)
{
_repositoryWrapper = repositoryWrapper;
}
public async Task<List<BrandSDto>> Handle(GetBrandsQuery request, CancellationToken cancellationToken)
{
IQueryable<Brand> baseBrands;
List<BrandSDto> brands;
if (request.BrandName != null)
{
baseBrands = _repositoryWrapper.SetRepository<Brand>()
.TableNoTracking
.Where(b => b.Name.Trim().Contains(request.BrandName.Trim()))
.OrderByDescending(b => b.CreatedAt);
}
else
{
baseBrands = _repositoryWrapper.SetRepository<Brand>().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;
}
}

View File

@ -2,7 +2,7 @@
namespace NetinaShop.Repository.Handlers.ProductCategories;
public class CreateProductCategoryCommandHandler : IRequestHandler<ProductCreateCategoryCommand,ProductCategoryLDto>
public class CreateProductCategoryCommandHandler : IRequestHandler<CreateProductCategoryCommand,ProductCategoryLDto>
{
private readonly IRepositoryWrapper _repositoryWrapper;
@ -10,7 +10,7 @@ public class CreateProductCategoryCommandHandler : IRequestHandler<ProductCreate
{
_repositoryWrapper = repositoryWrapper;
}
public async Task<ProductCategoryLDto> Handle(ProductCreateCategoryCommand request, CancellationToken cancellationToken)
public async Task<ProductCategoryLDto> Handle(CreateProductCategoryCommand request, CancellationToken cancellationToken)
{
var ent = ProductCategory.Create(request.Name, request.Description);
if (request.ParentId != default)

View File

@ -2,7 +2,7 @@
namespace NetinaShop.Repository.Handlers.ProductCategories;
public class DeleteProductCategoryCommandHandler : IRequestHandler<ProductDeleteCategoryCommand, bool>
public class DeleteProductCategoryCommandHandler : IRequestHandler<DeleteProductCategoryCommand, bool>
{
private readonly IRepositoryWrapper _repositoryWrapper;
@ -10,7 +10,7 @@ public class DeleteProductCategoryCommandHandler : IRequestHandler<ProductDelete
{
_repositoryWrapper = repositoryWrapper;
}
public async Task<bool> Handle(ProductDeleteCategoryCommand request, CancellationToken cancellationToken)
public async Task<bool> Handle(DeleteProductCategoryCommand request, CancellationToken cancellationToken)
{
var ent = await _repositoryWrapper.SetRepository<ProductCategory>().TableNoTracking

View File

@ -12,20 +12,32 @@ public class GetProductCategoriesQueryHandler : IRequestHandler<GetProductCatego
}
public async Task<List<ProductCategorySDto>> Handle(GetProductCategoriesQuery request, CancellationToken cancellationToken)
{
IQueryable<ProductCategory> basCategories;
List<ProductCategorySDto> groupCats;
if (request.CategoryName != null)
{
basCategories = _repositoryWrapper.SetRepository<ProductCategory>()
.TableNoTracking
.Where(c => c.Name.Trim().Contains(request.CategoryName.Trim()))
.OrderByDescending(c => c.CreatedAt);
}
else
{
basCategories = _repositoryWrapper.SetRepository<ProductCategory>()
.TableNoTracking
.OrderByDescending(c=>c.CreatedAt);
}
if (request.Page != null)
{
groupCats = await _repositoryWrapper.SetRepository<ProductCategory>()
.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<ProductCategory>()
.TableNoTracking
groupCats = await basCategories
.Select(CategoryMapper.ProjectToSDto)
.ToListAsync(cancellationToken);
}

View File

@ -2,11 +2,11 @@
namespace NetinaShop.Repository.Handlers.ProductCategories;
public class GetCategoryQueryHandler : IRequestHandler<GetProductCategoryQuery, ProductCategoryLDto>
public class GetProductCategoryQueryHandler : IRequestHandler<GetProductCategoryQuery, ProductCategoryLDto>
{
private readonly IRepositoryWrapper _repositoryWrapper;
public GetCategoryQueryHandler(IRepositoryWrapper repositoryWrapper)
public GetProductCategoryQueryHandler(IRepositoryWrapper repositoryWrapper)
{
_repositoryWrapper = repositoryWrapper;
}

View File

@ -2,7 +2,7 @@
namespace NetinaShop.Repository.Handlers.ProductCategories;
public class UpdateProductCategoryCommandHandler : IRequestHandler<ProductUpdateCategoryCommand, bool>
public class UpdateProductCategoryCommandHandler : IRequestHandler<UpdateProductCategoryCommand, bool>
{
private readonly IRepositoryWrapper _repositoryWrapper;
@ -10,7 +10,7 @@ public class UpdateProductCategoryCommandHandler : IRequestHandler<ProductUpdate
{
_repositoryWrapper = repositoryWrapper;
}
public async Task<bool> Handle(ProductUpdateCategoryCommand request, CancellationToken cancellationToken)
public async Task<bool> Handle(UpdateProductCategoryCommand request, CancellationToken cancellationToken)
{
var ent = await _repositoryWrapper.SetRepository<ProductCategory>().TableNoTracking
.FirstOrDefaultAsync(c => c.Id == request.Id, cancellationToken);