From d13a95c9b6416ab65c4a894d7724d7e11cce30b7 Mon Sep 17 00:00:00 2001 From: "Amir.H Khademi" Date: Fri, 16 Feb 2024 18:07:00 +0330 Subject: [PATCH] feat : complete calculate discount , add ver 0.6.12.20 --- .version | 2 +- .../CalculateProductDiscountCommandHandler.cs | 105 +++++++++++++++--- .../Commands/ProductCommands.cs | 2 +- .../Dtos/LargDtos/ProductLDto.cs | 4 + .../Products/GetProductQueryHandler.cs | 17 +-- 5 files changed, 100 insertions(+), 30 deletions(-) diff --git a/.version b/.version index fe4aaa4..1d384f2 100644 --- a/.version +++ b/.version @@ -1 +1 @@ -0.5.11.19 \ No newline at end of file +0.6.12.20 \ No newline at end of file diff --git a/NetinaShop.Core/EntityServices/DiscountHandlers/CalculateProductDiscountCommandHandler.cs b/NetinaShop.Core/EntityServices/DiscountHandlers/CalculateProductDiscountCommandHandler.cs index 02f138a..1ef439d 100644 --- a/NetinaShop.Core/EntityServices/DiscountHandlers/CalculateProductDiscountCommandHandler.cs +++ b/NetinaShop.Core/EntityServices/DiscountHandlers/CalculateProductDiscountCommandHandler.cs @@ -1,8 +1,9 @@ -using NetinaShop.Domain.Dtos.SmallDtos; +using NetinaShop.Domain.Dtos.LargDtos; +using NetinaShop.Domain.Dtos.SmallDtos; namespace NetinaShop.Core.EntityServices.DiscountHandlers; -public class CalculateProductDiscountCommandHandler : IRequestHandler +public class CalculateProductDiscountCommandHandler : IRequestHandler { private readonly IRepositoryWrapper _repositoryWrapper; @@ -10,11 +11,27 @@ public class CalculateProductDiscountCommandHandler : IRequestHandler Handle(CalculateProductDiscountCommand request, CancellationToken cancellationToken) + + + public async Task Handle(CalculateProductDiscountCommand request, CancellationToken cancellationToken) + { + if (request.Product is ProductSDto product) + { + await CalculateAsync(product, cancellationToken); + }else if (request.Product is ProductLDto productLDto) + { + await CalculateAsync(productLDto, cancellationToken); + } + else + throw new AppException("Model is wrong , ProductSDto or ProductLDto"); + return true; + } + + public async Task CalculateAsync(ProductSDto request, CancellationToken cancellationToken) { double discountPrice = 0; - request.ProductSDto.CostWithDiscount = request.ProductSDto.Cost; - double totalPrice = request.ProductSDto.Cost; + request.CostWithDiscount = request.Cost; + double totalPrice = request.Cost; var allDiscount = await _repositoryWrapper.SetRepository() .TableNoTracking @@ -25,23 +42,23 @@ public class CalculateProductDiscountCommandHandler : IRequestHandler() .TableNoTracking - .FirstOrDefaultAsync(d => d.CategoryId == request.ProductSDto.CategoryId && d.HasCode == false && d.ExpireDate.Date >= DateTime.Today.Date, cancellationToken); + .FirstOrDefaultAsync(d => d.CategoryId == request.CategoryId && d.HasCode == false && d.ExpireDate.Date >= DateTime.Today.Date, cancellationToken); if (categoryDiscount != null && !categoryDiscount.IsExpired()) { discountPrice = categoryDiscount.AmountType == DiscountAmountType.Amount ? totalPrice - categoryDiscount.DiscountAmount : totalPrice - totalPrice / 100 * categoryDiscount.DiscountPercent; - request.ProductSDto.HasDiscount = true; + request.HasDiscount = true; } var productDiscount = await _repositoryWrapper.SetRepository() .TableNoTracking - .Where(d => d.HasCode == false && d.ProductId == request.ProductSDto.Id && d.ExpireDate.Date >= DateTime.Today.Date) + .Where(d => d.HasCode == false && d.ProductId == request.Id && d.ExpireDate.Date >= DateTime.Today.Date) .OrderByDescending(d => d.CreatedAt) .FirstOrDefaultAsync(cancellationToken); @@ -50,17 +67,75 @@ public class CalculateProductDiscountCommandHandler : IRequestHandler CalculateAsync(ProductLDto request, CancellationToken cancellationToken) + { + double discountPrice = 0; + request.CostWithDiscount = request.Cost; + double totalPrice = request.Cost; + + var allDiscount = await _repositoryWrapper.SetRepository() + .TableNoTracking + .FirstOrDefaultAsync(d => d.Type == DiscountType.All && d.HasCode == false && d.ExpireDate.Date >= DateTime.Today.Date, cancellationToken); + + if (allDiscount != null && !allDiscount.IsExpired()) + { + + discountPrice = allDiscount.AmountType == DiscountAmountType.Amount ? totalPrice - allDiscount.DiscountAmount : + totalPrice - totalPrice / 100 * allDiscount.DiscountPercent; + request.HasDiscount = true; + } + + var categoryDiscount = await _repositoryWrapper.SetRepository() + .TableNoTracking + .FirstOrDefaultAsync(d => d.CategoryId == request.CategoryId && d.HasCode == false && d.ExpireDate.Date >= DateTime.Today.Date, cancellationToken); + + if (categoryDiscount != null && !categoryDiscount.IsExpired()) + { + discountPrice = categoryDiscount.AmountType == DiscountAmountType.Amount + ? totalPrice - categoryDiscount.DiscountAmount : totalPrice - totalPrice / 100 * categoryDiscount.DiscountPercent; + request.HasDiscount = true; + } + + var productDiscount = await _repositoryWrapper.SetRepository() + .TableNoTracking + .Where(d => d.HasCode == false && d.ProductId == request.Id && d.ExpireDate.Date >= DateTime.Today.Date) + .OrderByDescending(d => d.CreatedAt) + .FirstOrDefaultAsync(cancellationToken); + + if (productDiscount != null && !productDiscount.IsExpired()) + { + discountPrice = productDiscount.AmountType == DiscountAmountType.Amount ? totalPrice - productDiscount.DiscountAmount + : totalPrice - totalPrice / 100 * productDiscount.DiscountPercent; + + request.HasDiscount = true; + if (productDiscount.IsSpecialOffer) + { + request.IsSpecial = true; + request.SpecialOffer = productDiscount.AdaptToSDto(); + } + } + + if (request.HasDiscount) + { + request.CostWithDiscount = discountPrice; + request.DiscountPercent = request.Cost == 0 ? 0 : 100 - 100 * request.CostWithDiscount / request.Cost; + } + + return request; } } \ No newline at end of file diff --git a/NetinaShop.Domain/CommandQueries/Commands/ProductCommands.cs b/NetinaShop.Domain/CommandQueries/Commands/ProductCommands.cs index 7fce16e..c3b363f 100644 --- a/NetinaShop.Domain/CommandQueries/Commands/ProductCommands.cs +++ b/NetinaShop.Domain/CommandQueries/Commands/ProductCommands.cs @@ -43,4 +43,4 @@ public sealed record UpdateProductCommand( public sealed record DeleteProductCommand(Guid Id) : IRequest; -public sealed record CalculateProductDiscountCommand(ProductSDto ProductSDto) : IRequest; \ No newline at end of file +public sealed record CalculateProductDiscountCommand(object Product) : IRequest; \ No newline at end of file diff --git a/NetinaShop.Domain/Dtos/LargDtos/ProductLDto.cs b/NetinaShop.Domain/Dtos/LargDtos/ProductLDto.cs index 79301a4..fc669c7 100644 --- a/NetinaShop.Domain/Dtos/LargDtos/ProductLDto.cs +++ b/NetinaShop.Domain/Dtos/LargDtos/ProductLDto.cs @@ -8,7 +8,11 @@ public class ProductLDto : BaseDto public string ExpertCheck { get; set; } = string.Empty; public string Tags { get; set; } = string.Empty; public string Warranty { get; set; } = string.Empty; + public double CostWithDiscount { get; set; } + public double DiscountPercent { get; set; } public bool BeDisplayed { get; set; } + public bool IsSpecial { get; set; } + public bool HasDiscount { get; set; } public bool HasExpressDelivery { get; set; } public double Cost { get; set; } public double PackingCost { get; set; } diff --git a/NetinaShop.Repository/Handlers/Products/GetProductQueryHandler.cs b/NetinaShop.Repository/Handlers/Products/GetProductQueryHandler.cs index 677984d..cf90c90 100644 --- a/NetinaShop.Repository/Handlers/Products/GetProductQueryHandler.cs +++ b/NetinaShop.Repository/Handlers/Products/GetProductQueryHandler.cs @@ -5,10 +5,12 @@ namespace NetinaShop.Repository.Handlers.Products; public class GetProductQueryHandler : IRequestHandler { private readonly IRepositoryWrapper _repositoryWrapper; + private readonly IMediator _mediator; - public GetProductQueryHandler(IRepositoryWrapper repositoryWrapper) + public GetProductQueryHandler(IRepositoryWrapper repositoryWrapper,IMediator mediator) { _repositoryWrapper = repositoryWrapper; + _mediator = mediator; } public async Task Handle(GetProductQuery request, CancellationToken cancellationToken) { @@ -20,18 +22,7 @@ public class GetProductQueryHandler : IRequestHandler() - .TableNoTracking - .Where(pd => pd.IsSpecialOffer && pd.ProductId == ent.Id) - .Select(DiscountMapper.ProjectToSDto) - .FirstOrDefaultAsync(cancellationToken); - - ent.SpecialOffer = specialOffer; - - if (ent.SpecialOffer == null || ent.SpecialOffer.ExpireDate.Date < DateTime.Today.Date) - ent.IsSpecialOffer = false; - else - ent.IsSpecialOffer = true; + await _mediator.Send(new CalculateProductDiscountCommand(ent), cancellationToken); var response = new GetProductResponseDto {