From 8ea22859a1a31806819d69105775473839d39000 Mon Sep 17 00:00:00 2001 From: "Amir.H Khademi" Date: Tue, 23 Jan 2024 14:06:20 +0330 Subject: [PATCH] refactor : change cntroller and handlers add nullable page for controllers , change handlers --- .../Controller/BlogCategoryController.cs | 24 ++++++++++--- NetinaShop.Api/Controller/BrandController.cs | 36 +++++++++++++------ .../Controller/ProductCategoryController.cs | 4 +-- .../Commands/ProductCommands.cs | 6 ++-- .../Queries/ProductCategoryQueries.cs | 2 +- .../Entities/Products/Product.Aggregate.cs | 3 +- .../GetProductCategoriesQueryHandler.cs | 21 ++++++++--- .../Products/CreateProductCommandHandler.cs | 2 +- .../Products/GetProductsQueryHandler.cs | 6 ++-- .../Products/UpdateProductCommandHandler.cs | 5 ++- .../Program.cs | 6 ++-- 11 files changed, 79 insertions(+), 36 deletions(-) diff --git a/NetinaShop.Api/Controller/BlogCategoryController.cs b/NetinaShop.Api/Controller/BlogCategoryController.cs index 7ec1036..bf3cd7e 100644 --- a/NetinaShop.Api/Controller/BlogCategoryController.cs +++ b/NetinaShop.Api/Controller/BlogCategoryController.cs @@ -32,11 +32,25 @@ public class BlogCategoryController : ICarterModule } // GET:Get All Entity - public async Task GetAllAsync([FromQuery] int page, IRepositoryWrapper repositoryWrapper, CancellationToken cancellationToken) - => TypedResults.Ok(await repositoryWrapper.SetRepository().TableNoTracking - .OrderByDescending(b => b.CreatedAt).Skip(page * 10).Take(10) - .Select(BlogCategoryMapper.ProjectToSDto) - .ToListAsync(cancellationToken)); + public async Task GetAllAsync([FromQuery] int? page, IRepositoryWrapper repositoryWrapper, + CancellationToken cancellationToken) + { + if (page != null) + { + return TypedResults.Ok(await repositoryWrapper.SetRepository().TableNoTracking + .OrderByDescending(b => b.CreatedAt).Skip(page.Value * 10).Take(10) + .Select(BlogCategoryMapper.ProjectToSDto) + .ToListAsync(cancellationToken)); + } + else + { + + return TypedResults.Ok(await repositoryWrapper.SetRepository().TableNoTracking + .OrderByDescending(b => b.CreatedAt) + .Select(BlogCategoryMapper.ProjectToSDto) + .ToListAsync(cancellationToken)); + } + } // GET:Get An Entity By Id public async Task GetAsync(Guid id, IRepositoryWrapper repositoryWrapper, CancellationToken cancellationToken) diff --git a/NetinaShop.Api/Controller/BrandController.cs b/NetinaShop.Api/Controller/BrandController.cs index 68df95e..095bbe6 100644 --- a/NetinaShop.Api/Controller/BrandController.cs +++ b/NetinaShop.Api/Controller/BrandController.cs @@ -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 GetAllAsync([FromQuery] int page, IRepositoryWrapper repositoryWrapper, CancellationToken cancellationToken) - => TypedResults.Ok(await repositoryWrapper.SetRepository().TableNoTracking - .OrderByDescending(b => b.CreatedAt).Skip(page * 10).Take(10) - .Select(BrandMapper.ProjectToSDto) - .ToListAsync(cancellationToken)); + public async Task GetAllAsync([FromQuery] int? page, IRepositoryWrapper repositoryWrapper, + 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)); + } + } // GET:Get An Entity By Id public async Task GetAsync(Guid id, IRepositoryWrapper repositoryWrapper, CancellationToken cancellationToken) diff --git a/NetinaShop.Api/Controller/ProductCategoryController.cs b/NetinaShop.Api/Controller/ProductCategoryController.cs index f3729ad..d729352 100644 --- a/NetinaShop.Api/Controller/ProductCategoryController.cs +++ b/NetinaShop.Api/Controller/ProductCategoryController.cs @@ -30,8 +30,8 @@ public class ProductCategoryController : ICarterModule } // GET:Get All Entity - public async Task GetAllAsync(IMediator mediator, CancellationToken cancellationToken) - => TypedResults.Ok(await mediator.Send(new GetProductCategoriesQuery(),cancellationToken)); + public async Task 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 GetAsync(Guid id, IMediator mediator, CancellationToken cancellationToken) diff --git a/NetinaShop.Domain/CommandQueries/Commands/ProductCommands.cs b/NetinaShop.Domain/CommandQueries/Commands/ProductCommands.cs index b5bff90..0d2ae1e 100644 --- a/NetinaShop.Domain/CommandQueries/Commands/ProductCommands.cs +++ b/NetinaShop.Domain/CommandQueries/Commands/ProductCommands.cs @@ -10,8 +10,9 @@ string Warranty, bool BeDisplayed, double Cost, double PackingCost, +bool HasExpressDelivery, +int MaxOrderCount, Guid BrandId, -string BrandNames , Guid CategoryId, List Specifications, List Files):IRequest; @@ -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 Specifications, List Files) : IRequest; diff --git a/NetinaShop.Domain/CommandQueries/Queries/ProductCategoryQueries.cs b/NetinaShop.Domain/CommandQueries/Queries/ProductCategoryQueries.cs index cd6d677..2f1722f 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() : IRequest>; \ No newline at end of file +public record GetProductCategoriesQuery(int? page) : IRequest>; \ No newline at end of file diff --git a/NetinaShop.Domain/Entities/Products/Product.Aggregate.cs b/NetinaShop.Domain/Entities/Products/Product.Aggregate.cs index f38c71c..54ceb3c 100644 --- a/NetinaShop.Domain/Entities/Products/Product.Aggregate.cs +++ b/NetinaShop.Domain/Entities/Products/Product.Aggregate.cs @@ -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, diff --git a/NetinaShop.Repository/Handlers/ProductCategories/GetProductCategoriesQueryHandler.cs b/NetinaShop.Repository/Handlers/ProductCategories/GetProductCategoriesQueryHandler.cs index eee02eb..cd55a3e 100644 --- a/NetinaShop.Repository/Handlers/ProductCategories/GetProductCategoriesQueryHandler.cs +++ b/NetinaShop.Repository/Handlers/ProductCategories/GetProductCategoriesQueryHandler.cs @@ -12,10 +12,23 @@ public class GetProductCategoriesQueryHandler : IRequestHandler> Handle(GetProductCategoriesQuery request, CancellationToken cancellationToken) { - var groupCats = await _repositoryWrapper.SetRepository() - .TableNoTracking - .Select(CategoryMapper.ProjectToSDto) - .ToListAsync(cancellationToken); + List groupCats; + if (request.page != null) + { + groupCats = await _repositoryWrapper.SetRepository() + .TableNoTracking + .OrderByDescending(c=>c.CreatedAt) + .Skip(request.page.Value * 15).Take(15) + .Select(CategoryMapper.ProjectToSDto) + .ToListAsync(cancellationToken); + } + else + { + groupCats = await _repositoryWrapper.SetRepository() + .TableNoTracking + .Select(CategoryMapper.ProjectToSDto) + .ToListAsync(cancellationToken); + } foreach (var cat in groupCats) cat.Children = groupCats.Where(c => c.ParentId == cat.Id).ToList(); diff --git a/NetinaShop.Repository/Handlers/Products/CreateProductCommandHandler.cs b/NetinaShop.Repository/Handlers/Products/CreateProductCommandHandler.cs index cad934c..fb963fd 100644 --- a/NetinaShop.Repository/Handlers/Products/CreateProductCommandHandler.cs +++ b/NetinaShop.Repository/Handlers/Products/CreateProductCommandHandler.cs @@ -12,7 +12,7 @@ public class CreateProductCommandHandler : IRequestHandler 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) diff --git a/NetinaShop.Repository/Handlers/Products/GetProductsQueryHandler.cs b/NetinaShop.Repository/Handlers/Products/GetProductsQueryHandler.cs index fcc065f..6600329 100644 --- a/NetinaShop.Repository/Handlers/Products/GetProductsQueryHandler.cs +++ b/NetinaShop.Repository/Handlers/Products/GetProductsQueryHandler.cs @@ -1,6 +1,4 @@ -using NetinaShop.Domain.Entities.Discounts; - -namespace NetinaShop.Repository.Handlers.Products; +namespace NetinaShop.Repository.Handlers.Products; public class GetProductsQueryHandler : IRequestHandler> { @@ -33,7 +31,7 @@ public class GetProductsQueryHandler : IRequestHandler(), new List()); 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(), new List()); products.Add(product);