refactor : change cntroller and handlers

add nullable page for controllers , change handlers
release
Amir Hossein Khademi 2024-01-23 14:06:20 +03:30
parent 4ebcf7538a
commit 8ea22859a1
11 changed files with 79 additions and 36 deletions

View File

@ -32,11 +32,25 @@ public class BlogCategoryController : ICarterModule
}
// GET:Get All Entity
public async Task<IResult> GetAllAsync([FromQuery] int page, IRepositoryWrapper repositoryWrapper, CancellationToken cancellationToken)
=> TypedResults.Ok(await repositoryWrapper.SetRepository<BlogCategory>().TableNoTracking
.OrderByDescending(b => b.CreatedAt).Skip(page * 10).Take(10)
.Select(BlogCategoryMapper.ProjectToSDto)
.ToListAsync(cancellationToken));
public async Task<IResult> GetAllAsync([FromQuery] int? page, IRepositoryWrapper repositoryWrapper,
CancellationToken cancellationToken)
{
if (page != null)
{
return TypedResults.Ok(await repositoryWrapper.SetRepository<BlogCategory>().TableNoTracking
.OrderByDescending(b => b.CreatedAt).Skip(page.Value * 10).Take(10)
.Select(BlogCategoryMapper.ProjectToSDto)
.ToListAsync(cancellationToken));
}
else
{
return TypedResults.Ok(await repositoryWrapper.SetRepository<BlogCategory>().TableNoTracking
.OrderByDescending(b => b.CreatedAt)
.Select(BlogCategoryMapper.ProjectToSDto)
.ToListAsync(cancellationToken));
}
}
// GET:Get An Entity By Id
public async Task<IResult> GetAsync(Guid id, IRepositoryWrapper repositoryWrapper, CancellationToken cancellationToken)

View File

@ -8,8 +8,7 @@ public class BrandController : ICarterModule
public virtual void AddRoutes(IEndpointRouteBuilder app)
{
var group = app.NewVersionedApi("Brand")
.MapGroup($"api/brand")
.RequireAuthorization(builder => builder.AddAuthenticationSchemes("Bearer").RequireAuthenticatedUser());
.MapGroup($"api/brand");
group.MapGet("", GetAllAsync)
.WithDisplayName("GetAllBrands")
@ -20,21 +19,38 @@ public class BrandController : ICarterModule
.HasApiVersion(1.0);
group.MapPost("", Post)
.HasApiVersion(1.0);
.HasApiVersion(1.0)
.RequireAuthorization(builder => builder.AddAuthenticationSchemes("Bearer").RequireAuthenticatedUser());
group.MapPut("", Put)
.HasApiVersion(1.0);
.HasApiVersion(1.0)
.RequireAuthorization(builder => builder.AddAuthenticationSchemes("Bearer").RequireAuthenticatedUser());
group.MapDelete("{id}", Delete)
.HasApiVersion(1.0);
.HasApiVersion(1.0)
.RequireAuthorization(builder => builder.AddAuthenticationSchemes("Bearer").RequireAuthenticatedUser());
}
// GET:Get All Entity
public async Task<IResult> GetAllAsync([FromQuery] int page, IRepositoryWrapper repositoryWrapper, CancellationToken cancellationToken)
=> TypedResults.Ok(await repositoryWrapper.SetRepository<Brand>().TableNoTracking
.OrderByDescending(b => b.CreatedAt).Skip(page * 10).Take(10)
.Select(BrandMapper.ProjectToSDto)
.ToListAsync(cancellationToken));
public async Task<IResult> GetAllAsync([FromQuery] int? page, IRepositoryWrapper repositoryWrapper,
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));
}
}
// GET:Get An Entity By Id
public async Task<IResult> GetAsync(Guid id, IRepositoryWrapper repositoryWrapper, CancellationToken cancellationToken)

View File

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

View File

@ -10,8 +10,9 @@ string Warranty,
bool BeDisplayed,
double Cost,
double PackingCost,
bool HasExpressDelivery,
int MaxOrderCount,
Guid BrandId,
string BrandNames ,
Guid CategoryId,
List<SpecificationSDto> Specifications,
List<StorageFileSDto> Files):IRequest<ProductLDto>;
@ -27,8 +28,9 @@ public sealed record UpdateProductCommand(
bool BeDisplayed,
double Cost,
double PackingCost,
bool HasExpressDelivery,
int MaxOrderCount,
Guid BrandId,
string BrandNames,
Guid CategoryId,
List<SpecificationSDto> Specifications,
List<StorageFileSDto> Files) : IRequest<bool>;

View File

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

View File

@ -2,7 +2,8 @@
public partial class Product
{
public static Product Create(string persianName, string englishName, string summery, string expertCheck, string tags, string warranty, bool beDisplayed,
public static Product Create(string persianName, string englishName, string summery, string expertCheck, string tags, string warranty,
bool beDisplayed,
double cost,
double packingCost,
bool hasExpressDelivery,

View File

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

View File

@ -12,7 +12,7 @@ public class CreateProductCommandHandler : IRequestHandler<CreateProductCommand,
public async Task<ProductLDto> Handle(CreateProductCommand request, CancellationToken cancellationToken)
{
var ent = Product.Create(request.PersianName, request.EnglishName, request.Summery, request.ExpertCheck,
request.Tags, request.Warranty,request.BeDisplayed,request.Cost,request.PackingCost,0,0,0,
request.Tags, request.Warranty,request.BeDisplayed,request.Cost,request.PackingCost,request.HasExpressDelivery,request.MaxOrderCount,
request.BrandId,request.CategoryId);
foreach (var specification in request.Specifications)

View File

@ -1,6 +1,4 @@
using NetinaShop.Domain.Entities.Discounts;
namespace NetinaShop.Repository.Handlers.Products;
namespace NetinaShop.Repository.Handlers.Products;
public class GetProductsQueryHandler : IRequestHandler<GetProductsQuery, List<ProductSDto>>
{
@ -33,7 +31,7 @@ public class GetProductsQueryHandler : IRequestHandler<GetProductsQuery, List<Pr
};
}
var productSDtos = await products.Skip(request.Page * 10)
var productSDtos = await products.Skip(request.Page * 20)
.Take(10)
.Select(ProductMapper.ProjectToSDto)
.ToListAsync(cancellationToken);

View File

@ -21,9 +21,8 @@ public class UpdateProductCommandHandler : IRequestHandler<UpdateProductCommand,
request.BeDisplayed,
request.Cost,
request.PackingCost,
ent.ReviewCount,
ent.Rate,
ent.Viewed,
request.HasExpressDelivery,
request.MaxOrderCount,
request.BrandId,
request.CategoryId);
newEnt.Id = ent.Id;

View File

@ -106,12 +106,12 @@ try
if (price != null && double.TryParse(price.meta_value, out double dPrice))
product = new CreateProductCommand(wordPressPostDto.post_title, string.Empty, wordPressPostDto.post_content,
wordPressPostDto.post_excerpt, string.Empty, string.Empty, true, dPrice, 0,
brandId, string.Empty, categoryId,
false,10, brandId, categoryId,
new List<SpecificationSDto>(), new List<StorageFileSDto>());
else
product = new CreateProductCommand(wordPressPostDto.post_title, string.Empty, wordPressPostDto.post_content,
wordPressPostDto.post_excerpt, string.Empty, string.Empty, true, 0, 0,
brandId, string.Empty, categoryId,
wordPressPostDto.post_excerpt, string.Empty, string.Empty, true, 0, 0,false,10,
brandId,categoryId,
new List<SpecificationSDto>(), new List<StorageFileSDto>());
products.Add(product);