From 27e8f545fe1b7d69ef5f6a634e103178175ea990 Mon Sep 17 00:00:00 2001 From: "Amir.H Khademi" Date: Thu, 16 May 2024 13:52:54 +0330 Subject: [PATCH] fix : order homepage informations --- Netina.Api/Controllers/BlogController.cs | 19 ++++- Netina.Api/Controllers/DashboardController.cs | 8 ++ .../Abstracts/IDashboardService.cs | 6 +- Netina.Core/BaseServices/DashboardService.cs | 78 +++++++++++-------- .../GetTorobProductsQueryHandler.cs | 43 ++++++++-- .../CalculateProductDiscountCommandHandler.cs | 10 +-- Netina.Core/Netina.Core.csproj | 3 + .../Dtos/DashboardDtos/OrderDashboardDto.cs | 9 +++ Netina.Domain/Dtos/SmallDtos/ProductSDto.cs | 2 +- Netina.Domain/Netina.Domain.csproj | 1 - 10 files changed, 126 insertions(+), 53 deletions(-) create mode 100644 Netina.Domain/Dtos/DashboardDtos/OrderDashboardDto.cs diff --git a/Netina.Api/Controllers/BlogController.cs b/Netina.Api/Controllers/BlogController.cs index b515bf5..c6aa858 100644 --- a/Netina.Api/Controllers/BlogController.cs +++ b/Netina.Api/Controllers/BlogController.cs @@ -1,4 +1,6 @@ -using Netina.Domain.Entities.Blogs; +using Marten.Events; +using Netina.Domain.Entities.Blogs; +using Netina.Domain.Entities.Products; namespace Netina.Api.Controllers; @@ -60,6 +62,7 @@ public class BlogController : ICarterModule { ent.AddFile(file.Name, file.FileLocation, file.FileName, file.IsHeader, file.IsPrimary, file.FileType); } + repositoryWrapper.SetRepository().Add(ent); await repositoryWrapper.SaveChangesAsync(cancellationToken); return TypedResults.Ok(ent.AdaptToSDto()); @@ -76,6 +79,20 @@ public class BlogController : ICarterModule newEnt.Id = ent.Id; newEnt.CreatedAt = ent.CreatedAt; newEnt.CreatedBy = ent.CreatedBy; + + var dbFiles = await repositoryWrapper.SetRepository().TableNoTracking + .Where(s => s.BlogId == newEnt.Id).ToListAsync(cancellationToken); + foreach (var dbFile in dbFiles) + { + if (dto.Files.FirstOrDefault(s => s.Id == dbFile.Id) == null) + { + repositoryWrapper.SetRepository().Delete(dbFile); + await repositoryWrapper.SaveChangesAsync(cancellationToken); + } + } + foreach (var file in dto.Files.Where(f => f.Id == default)) + newEnt.AddFile(file.Name, file.FileLocation, file.FileName, file.IsHeader, file.IsPrimary, file.FileType); + repositoryWrapper.SetRepository().Update(newEnt); await repositoryWrapper.SaveChangesAsync(cancellationToken); return TypedResults.Ok(); diff --git a/Netina.Api/Controllers/DashboardController.cs b/Netina.Api/Controllers/DashboardController.cs index 6f7fba2..c4196e9 100644 --- a/Netina.Api/Controllers/DashboardController.cs +++ b/Netina.Api/Controllers/DashboardController.cs @@ -12,8 +12,16 @@ public class DashboardController : ICarterModule .WithDisplayName("Get Home Dashboard") .HasApiVersion(1.0); + group.MapGet("orders", GetOrdersDashboardAsync) + .WithDisplayName("Get Orders Dashboard") + .HasApiVersion(1.0); + + } + private async Task GetOrdersDashboardAsync([FromServices] IDashboardService dashboardService, CancellationToken cancellationToken) + => TypedResults.Ok(await dashboardService.GetOrdersDashboardAsyncTask(cancellationToken)); + private async Task GetHomeDashboardAsync([FromServices] IDashboardService dashboardService, CancellationToken cancellationToken) => TypedResults.Ok(await dashboardService.GetHomeDashboardAsyncTask(cancellationToken)); } \ No newline at end of file diff --git a/Netina.Core/BaseServices/Abstracts/IDashboardService.cs b/Netina.Core/BaseServices/Abstracts/IDashboardService.cs index 12dfb0e..9ff10a5 100644 --- a/Netina.Core/BaseServices/Abstracts/IDashboardService.cs +++ b/Netina.Core/BaseServices/Abstracts/IDashboardService.cs @@ -1,9 +1,7 @@ -using Netina.Common.Models; -using Netina.Domain.Dtos.DashboardDtos; - -namespace Netina.Core.BaseServices.Abstracts; +namespace Netina.Core.BaseServices.Abstracts; public interface IDashboardService : IScopedDependency { public Task GetHomeDashboardAsyncTask(CancellationToken cancellationToken = default); + public Task GetOrdersDashboardAsyncTask(CancellationToken cancellationToken = default); } \ No newline at end of file diff --git a/Netina.Core/BaseServices/DashboardService.cs b/Netina.Core/BaseServices/DashboardService.cs index f561bc9..606b9a9 100644 --- a/Netina.Core/BaseServices/DashboardService.cs +++ b/Netina.Core/BaseServices/DashboardService.cs @@ -1,12 +1,4 @@ -using Netina.Core.BaseServices.Abstracts; -using Netina.Domain.Dtos.DashboardDtos; -using Netina.Domain.Entities.Blogs; -using Netina.Domain.Entities.Brands; -using Netina.Domain.Entities.Orders; -using Netina.Domain.Entities.Products; -using Netina.Domain.Entities.Users; -using Netina.Domain.Enums; -using Netina.Repository.Repositories.Base.Contracts; +using MD.PersianDateTime.Standard; namespace Netina.Core.BaseServices; @@ -22,30 +14,54 @@ public class DashboardService : IDashboardService } public async Task GetHomeDashboardAsyncTask(CancellationToken cancellationToken = default) { - var response = new HomeDashboardDto(); - response.BlogsCount = await _repositoryWrapper.SetRepository() - .TableNoTracking - .CountAsync(cancellationToken); + var response = new HomeDashboardDto + { + BlogsCount = await _repositoryWrapper.SetRepository() + .TableNoTracking + .CountAsync(cancellationToken), + ProductsCount = await _repositoryWrapper.SetRepository() + .TableNoTracking + .CountAsync(cancellationToken), + TodayOrdersCount = await _repositoryWrapper.SetRepository() + .TableNoTracking + .Where(o => o.OrderAt.Date == DateTime.Today.Date) + .CountAsync(cancellationToken), + UnSubmittedOrdersCount = await _repositoryWrapper.SetRepository() + .TableNoTracking + .Where(o => o.OrderStatus == OrderStatus.Paid || o.OrderStatus == OrderStatus.Submitted) + .CountAsync(cancellationToken), + BrandsCount = await _repositoryWrapper.SetRepository() + .TableNoTracking + .CountAsync(cancellationToken), + SubscribersCount = await _userManager.Users.CountAsync(cancellationToken) + }; - response.ProductsCount = await _repositoryWrapper.SetRepository() - .TableNoTracking - .CountAsync(cancellationToken); + return response; + } - response.TodayOrdersCount = await _repositoryWrapper.SetRepository() - .TableNoTracking - .Where(o => o.OrderAt.Date == DateTime.Today.Date) - .CountAsync(cancellationToken); - - response.UnSubmittedOrdersCount = await _repositoryWrapper.SetRepository() - .TableNoTracking - .Where(o => o.OrderStatus == OrderStatus.Paid || o.OrderStatus == OrderStatus.Submitted) - .CountAsync(cancellationToken); - - response.BrandsCount = await _repositoryWrapper.SetRepository() - .TableNoTracking - .CountAsync(cancellationToken); - - response.SubscribersCount = await _userManager.Users.CountAsync(cancellationToken); + public async Task GetOrdersDashboardAsyncTask(CancellationToken cancellationToken = default) + { + DateTime startOfThisMonth = new PersianDateTime(PersianDateTime.Today.Year, PersianDateTime.Today.Month, 1).ToDateTime(); + DateTime endOfThisMonth = startOfThisMonth.AddMonths(1); + var response = new OrderDashboardDto + { + PayedOrdersCount = await _repositoryWrapper.SetRepository() + .TableNoTracking + .Where(o=>o.IsPayed && o.OrderStatus==OrderStatus.Paid) + .CountAsync(cancellationToken), + ThisMonthOrdersCount = await _repositoryWrapper.SetRepository() + .TableNoTracking + .Where(s => s.OrderAt.Date >= startOfThisMonth.Date && s.OrderAt.Date < endOfThisMonth.Date) + .CountAsync(cancellationToken), + TodayOrdersCount = await _repositoryWrapper.SetRepository() + .TableNoTracking + .Where(o => o.OrderAt.Date == DateTime.Now.Date) + .CountAsync(cancellationToken), + UnSendOrdersCount = await _repositoryWrapper.SetRepository() + .TableNoTracking + .Where(o => o.IsPayed && o.OrderStatus == OrderStatus.Processing) + .CountAsync(cancellationToken) + }; return response; } diff --git a/Netina.Core/CoreServices/SearchServices/GetTorobProductsQueryHandler.cs b/Netina.Core/CoreServices/SearchServices/GetTorobProductsQueryHandler.cs index cd87833..6e084a1 100644 --- a/Netina.Core/CoreServices/SearchServices/GetTorobProductsQueryHandler.cs +++ b/Netina.Core/CoreServices/SearchServices/GetTorobProductsQueryHandler.cs @@ -5,27 +5,58 @@ namespace Netina.Core.CoreServices.SearchServices; public class GetTorobProductsQueryHandler : IRequestHandler> { private readonly IRepositoryWrapper _repositoryWrapper; + private readonly IMediator _mediator; private readonly SiteSettings _siteSetting; - public GetTorobProductsQueryHandler(IRepositoryWrapper repositoryWrapper , IOptionsSnapshot optionsSnapshot) + public GetTorobProductsQueryHandler(IRepositoryWrapper repositoryWrapper, IMediator mediator, IOptionsSnapshot optionsSnapshot) { _repositoryWrapper = repositoryWrapper; + _mediator = mediator; _siteSetting = optionsSnapshot.Value; } public async Task> Handle(GetTorobProductsQuery request, CancellationToken cancellationToken) { - var products = await _repositoryWrapper.SetRepository() + var productsSDto = await _repositoryWrapper.SetRepository() .TableNoTracking .OrderByDescending(p => p.ModifiedAt == DateTime.MinValue ? p.CreatedAt : p.ModifiedAt) - .Select(ProductMapper.ProjectToTorobResponseDto) + .Select(p => new ProductSDto + { + Id = p.Id, + BeDisplayed = p.BeDisplayed, + IsEnable = p.IsEnable, + Cost = p.Cost, + CategoryId = p.CategoryId, + BrandId = p.BrandId, + EnglishName = p.EnglishName, + PersianName = p.PersianName, + Slug = p.Slug, + + }) .Skip(request.Page * 100) .Take(100) .ToListAsync(cancellationToken); - products.ForEach(p => + + foreach (var productSDto in productsSDto) + await _mediator.Send(new CalculateProductDiscountCommand(productSDto), cancellationToken); + var products = new List(); + foreach (var product in productsSDto) { - p.page_url = $"{_siteSetting.WebSiteUrl}/products/{p.product_id}"; - }); + var torobProduct = new TorobProductResponseDto + { + availibility = product.IsEnable, + page_url = $"{_siteSetting.WebSiteUrl}/products/{product.Id}", + product_id = product.Id.ToString() + }; + if (product.Cost > product.CostWithDiscount) + { + torobProduct.old_price = product.Cost; + torobProduct.price = product.CostWithDiscount; + } + else + torobProduct.price = product.Cost; + products.Add(torobProduct); + } return products; } diff --git a/Netina.Core/EntityServices/DiscountHandlers/CalculateProductDiscountCommandHandler.cs b/Netina.Core/EntityServices/DiscountHandlers/CalculateProductDiscountCommandHandler.cs index cf091ab..f1b6eef 100644 --- a/Netina.Core/EntityServices/DiscountHandlers/CalculateProductDiscountCommandHandler.cs +++ b/Netina.Core/EntityServices/DiscountHandlers/CalculateProductDiscountCommandHandler.cs @@ -1,12 +1,4 @@ -using Netina.Common.Models.Exception; -using Netina.Domain.CommandQueries.Commands; -using Netina.Domain.Dtos.LargDtos; -using Netina.Domain.Dtos.SmallDtos; -using Netina.Domain.Entities.Discounts; -using Netina.Domain.Enums; -using Netina.Repository.Repositories.Base.Contracts; - -namespace Netina.Core.EntityServices.DiscountHandlers; +namespace Netina.Core.EntityServices.DiscountHandlers; public class CalculateProductDiscountCommandHandler : IRequestHandler { diff --git a/Netina.Core/Netina.Core.csproj b/Netina.Core/Netina.Core.csproj index 1236c52..015f381 100644 --- a/Netina.Core/Netina.Core.csproj +++ b/Netina.Core/Netina.Core.csproj @@ -55,9 +55,12 @@ + + + diff --git a/Netina.Domain/Dtos/DashboardDtos/OrderDashboardDto.cs b/Netina.Domain/Dtos/DashboardDtos/OrderDashboardDto.cs new file mode 100644 index 0000000..060f41a --- /dev/null +++ b/Netina.Domain/Dtos/DashboardDtos/OrderDashboardDto.cs @@ -0,0 +1,9 @@ +namespace Netina.Domain.Dtos.DashboardDtos; + +public class OrderDashboardDto +{ + public int TodayOrdersCount { get; set; } + public int ThisMonthOrdersCount { get; set; } + public int PayedOrdersCount { get; set; } + public int UnSendOrdersCount { get; set; } +} \ No newline at end of file diff --git a/Netina.Domain/Dtos/SmallDtos/ProductSDto.cs b/Netina.Domain/Dtos/SmallDtos/ProductSDto.cs index d52554c..1a7e5be 100644 --- a/Netina.Domain/Dtos/SmallDtos/ProductSDto.cs +++ b/Netina.Domain/Dtos/SmallDtos/ProductSDto.cs @@ -24,7 +24,7 @@ public class ProductSDto : BaseDto public int ReviewCount { get; set; } public int Viewed { get; set; } public string MainImage { get; set; } = string.Empty; - public Guid CategoryId { get; internal set; } + public Guid CategoryId { get; set; } public Guid BrandId { get; set; } public string BrandName { get; set; } = string.Empty; diff --git a/Netina.Domain/Netina.Domain.csproj b/Netina.Domain/Netina.Domain.csproj index 13192c2..fb82ead 100644 --- a/Netina.Domain/Netina.Domain.csproj +++ b/Netina.Domain/Netina.Domain.csproj @@ -37,7 +37,6 @@ -