From e9764c6cd43fa7686aa63aee26b8503290d4a73d Mon Sep 17 00:00:00 2001 From: "Amir.H Khademi" Date: Tue, 6 Aug 2024 12:38:57 +0330 Subject: [PATCH] feat(AddFastPriceChange) --- Netina.Api/Controllers/ProductController.cs | 9 +++++- .../Commands/ProductCommands.cs | 1 + .../ChangeProductCostCommandHandler.cs | 31 +++++++++++++++++++ .../ChangeProductDisplayedCommandHandler.cs | 15 +++------ 4 files changed, 45 insertions(+), 11 deletions(-) create mode 100644 Netina.Repository/Handlers/Products/ChangeProductCostCommandHandler.cs diff --git a/Netina.Api/Controllers/ProductController.cs b/Netina.Api/Controllers/ProductController.cs index 3879b4e..45b2319 100644 --- a/Netina.Api/Controllers/ProductController.cs +++ b/Netina.Api/Controllers/ProductController.cs @@ -26,7 +26,11 @@ public class ProductController : ICarterModule .RequireAuthorization(builder => builder.AddAuthenticationSchemes("Bearer").RequireAuthenticatedUser().RequireClaim(CustomClaimType.Permission, ApplicationPermission.ManageProducts)) .HasApiVersion(1.0); - group.MapPut("{productId}", ChangeDisplayedAsync) + group.MapPut("{productId}/displayed", ChangeDisplayedAsync) + .RequireAuthorization(builder => builder.AddAuthenticationSchemes("Bearer").RequireAuthenticatedUser().RequireClaim(CustomClaimType.Permission, ApplicationPermission.ManageProducts)) + .HasApiVersion(1.0); + + group.MapPut("{productId}/cost", ChangeCostAsync) .RequireAuthorization(builder => builder.AddAuthenticationSchemes("Bearer").RequireAuthenticatedUser().RequireClaim(CustomClaimType.Permission, ApplicationPermission.ManageProducts)) .HasApiVersion(1.0); @@ -65,6 +69,9 @@ public class ProductController : ICarterModule public async Task ChangeDisplayedAsync(Guid productId, [FromQuery] bool beDisplayed, [FromServices] IMediator mediator, CancellationToken cancellationToken) => TypedResults.Ok(await mediator.Send(new ChangeProductDisplayedCommand(productId, beDisplayed), cancellationToken)); + public async Task ChangeCostAsync(Guid productId, [FromQuery] double cost, [FromServices] IMediator mediator, CancellationToken cancellationToken) + => TypedResults.Ok(await mediator.Send(new ChangeProductCostCommand(productId, cost), cancellationToken)); + // DELETE:Delete Entity public async Task Delete(Guid id, IMediator mediator, CancellationToken cancellationToken) => TypedResults.Ok(await mediator.Send(new DeleteProductCommand(id), cancellationToken)); diff --git a/Netina.Domain/CommandQueries/Commands/ProductCommands.cs b/Netina.Domain/CommandQueries/Commands/ProductCommands.cs index 7b090d5..b15f7a4 100644 --- a/Netina.Domain/CommandQueries/Commands/ProductCommands.cs +++ b/Netina.Domain/CommandQueries/Commands/ProductCommands.cs @@ -42,6 +42,7 @@ public sealed record UpdateProductCommand( List Files) : IRequest; public sealed record ChangeProductDisplayedCommand(Guid Id,bool BeDisplayed) : IRequest; +public sealed record ChangeProductCostCommand(Guid Id,double Cost) : IRequest; public sealed record DeleteProductCommand(Guid Id) : IRequest; diff --git a/Netina.Repository/Handlers/Products/ChangeProductCostCommandHandler.cs b/Netina.Repository/Handlers/Products/ChangeProductCostCommandHandler.cs new file mode 100644 index 0000000..60b23e5 --- /dev/null +++ b/Netina.Repository/Handlers/Products/ChangeProductCostCommandHandler.cs @@ -0,0 +1,31 @@ +using Microsoft.EntityFrameworkCore; + +namespace Netina.Repository.Handlers.Products; + +public class ChangeProductCostCommandHandler(IRepositoryWrapper repositoryWrapper) + : IRequestHandler +{ + public async Task Handle(ChangeProductCostCommand request, CancellationToken cancellationToken) + { + + var ent = await repositoryWrapper.SetRepository() + .TableNoTracking + .FirstOrDefaultAsync(p => p.Id == request.Id, cancellationToken); + if (ent == null) + throw new AppException("Product not found"); + var newEnt = Product.Create(ent.PersianName, ent.EnglishName, ent.Summery, ent.ExpertCheck, ent.Tags, + ent.Warranty, + ent.BeDisplayed, request.Cost, ent.PackingCost, ent.HasExpressDelivery, ent.Stock, ent.MaxOrderCount, + ent.BrandId, + ent.CategoryId); + newEnt.CreatedAt = ent.CreatedAt; + newEnt.CreatedBy = ent.CreatedBy; + newEnt.Id = ent.Id; + + repositoryWrapper.SetRepository() + .Update(newEnt); + + await repositoryWrapper.SaveChangesAsync(cancellationToken); + return true; + } +} \ No newline at end of file diff --git a/Netina.Repository/Handlers/Products/ChangeProductDisplayedCommandHandler.cs b/Netina.Repository/Handlers/Products/ChangeProductDisplayedCommandHandler.cs index 3a37075..d43ae23 100644 --- a/Netina.Repository/Handlers/Products/ChangeProductDisplayedCommandHandler.cs +++ b/Netina.Repository/Handlers/Products/ChangeProductDisplayedCommandHandler.cs @@ -2,17 +2,12 @@ namespace Netina.Repository.Handlers.Products; -public class ChangeProductDisplayedCommandHandler : IRequestHandler +public class ChangeProductDisplayedCommandHandler(IRepositoryWrapper repositoryWrapper) + : IRequestHandler { - private readonly IRepositoryWrapper _repositoryWrapper; - - public ChangeProductDisplayedCommandHandler(IRepositoryWrapper repositoryWrapper) - { - _repositoryWrapper = repositoryWrapper; - } public async Task Handle(ChangeProductDisplayedCommand request, CancellationToken cancellationToken) { - var ent = await _repositoryWrapper.SetRepository() + var ent = await repositoryWrapper.SetRepository() .TableNoTracking .FirstOrDefaultAsync(p => p.Id == request.Id, cancellationToken); if (ent == null) @@ -26,10 +21,10 @@ public class ChangeProductDisplayedCommandHandler : IRequestHandler() + repositoryWrapper.SetRepository() .Update(newEnt); - await _repositoryWrapper.SaveChangesAsync(cancellationToken); + await repositoryWrapper.SaveChangesAsync(cancellationToken); return true; } } \ No newline at end of file