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

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

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() : IRequest<List<ProductCategorySDto>>; public record GetProductCategoriesQuery(int? page) : IRequest<List<ProductCategorySDto>>;

View File

@ -2,7 +2,8 @@
public partial class Product 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 cost,
double packingCost, double packingCost,
bool hasExpressDelivery, bool hasExpressDelivery,

View File

@ -12,10 +12,23 @@ 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)
{ {
var groupCats = await _repositoryWrapper.SetRepository<ProductCategory>() List<ProductCategorySDto> groupCats;
.TableNoTracking if (request.page != null)
.Select(CategoryMapper.ProjectToSDto) {
.ToListAsync(cancellationToken); 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) 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();

View File

@ -12,7 +12,7 @@ public class CreateProductCommandHandler : IRequestHandler<CreateProductCommand,
public async Task<ProductLDto> Handle(CreateProductCommand request, CancellationToken cancellationToken) public async Task<ProductLDto> Handle(CreateProductCommand request, CancellationToken cancellationToken)
{ {
var ent = Product.Create(request.PersianName, request.EnglishName, request.Summery, request.ExpertCheck, 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); request.BrandId,request.CategoryId);
foreach (var specification in request.Specifications) 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>> 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) .Take(10)
.Select(ProductMapper.ProjectToSDto) .Select(ProductMapper.ProjectToSDto)
.ToListAsync(cancellationToken); .ToListAsync(cancellationToken);

View File

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

View File

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