From 1b0c774583f15b2db22c625b1a0f551c13576406 Mon Sep 17 00:00:00 2001 From: malik masis Date: Wed, 24 Aug 2022 16:10:39 +0300 Subject: [PATCH 1/5] Combined the actions --- .../Orders/IOrderRepository.cs | 12 ++----- .../Orders/EfCoreOrderRepository.cs | 31 ++----------------- 2 files changed, 4 insertions(+), 39 deletions(-) diff --git a/services/ordering/src/EShopOnAbp.OrderingService.Domain/Orders/IOrderRepository.cs b/services/ordering/src/EShopOnAbp.OrderingService.Domain/Orders/IOrderRepository.cs index 0e953790..97e3fd77 100644 --- a/services/ordering/src/EShopOnAbp.OrderingService.Domain/Orders/IOrderRepository.cs +++ b/services/ordering/src/EShopOnAbp.OrderingService.Domain/Orders/IOrderRepository.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Threading; using System.Threading.Tasks; using Volo.Abp.Domain.Repositories; @@ -20,16 +21,7 @@ public interface IOrderRepository : IRepository bool includeDetails = true, CancellationToken cancellationToken = default); - Task> GetTopSelling( - ISpecification spec, - bool includeDetails = true, - CancellationToken cancellationToken = default); - - Task> GetPercentOfTotalPayment( - ISpecification spec, - CancellationToken cancellationToken = default); - - Task> GetCountOfTotalOrderStatus( + Task> GetDashboardAsync( ISpecification spec, bool includeDetails = true, CancellationToken cancellationToken = default); diff --git a/services/ordering/src/EShopOnAbp.OrderingService.EntityFrameworkCore/Orders/EfCoreOrderRepository.cs b/services/ordering/src/EShopOnAbp.OrderingService.EntityFrameworkCore/Orders/EfCoreOrderRepository.cs index b717a372..f84e01d2 100644 --- a/services/ordering/src/EShopOnAbp.OrderingService.EntityFrameworkCore/Orders/EfCoreOrderRepository.cs +++ b/services/ordering/src/EShopOnAbp.OrderingService.EntityFrameworkCore/Orders/EfCoreOrderRepository.cs @@ -45,35 +45,8 @@ public class EfCoreOrderRepository : EfCoreRepository o.OrderDate) .ToListAsync(GetCancellationToken(cancellationToken)); } - - public async Task> GetTopSelling( - ISpecification spec, - bool includeDetails = true, - CancellationToken cancellationToken = default) - { - return (await (await GetDbSetAsync()) - .IncludeDetails(includeDetails) - .Where(spec.ToExpression()) - .SelectMany(oi => oi.OrderItems) - .GroupBy(p => p.ProductCode) - .OrderByDescending(o => o.Sum(p => p.Units)) - .Select(o => o.ToList()) - .ToListAsync()) - .SelectMany(t => t) - .Take(OrderConstants.Top10) - .ToList(); - } - - public async Task> GetPercentOfTotalPayment( - ISpecification spec, - CancellationToken cancellationToken = default) - { - return await (await GetDbSetAsync()) - .Where(spec.ToExpression()) - .ToListAsync(GetCancellationToken(cancellationToken)); - } - - public async Task> GetCountOfTotalOrderStatus( + + public async Task> GetDashboardAsync( ISpecification spec, bool includeDetails = true, CancellationToken cancellationToken = default) From c8c50a4fa084650739bc6071640345b0443da5bb Mon Sep 17 00:00:00 2001 From: malik masis Date: Wed, 24 Aug 2022 16:10:57 +0300 Subject: [PATCH 2/5] Fixed grouping --- .../Orders/OrderAppService.cs | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/services/ordering/src/EShopOnAbp.OrderingService.Application/Orders/OrderAppService.cs b/services/ordering/src/EShopOnAbp.OrderingService.Application/Orders/OrderAppService.cs index 63b84cb2..91e46d3e 100644 --- a/services/ordering/src/EShopOnAbp.OrderingService.Application/Orders/OrderAppService.cs +++ b/services/ordering/src/EShopOnAbp.OrderingService.Application/Orders/OrderAppService.cs @@ -76,21 +76,21 @@ public class OrderAppService : ApplicationService, IOrderAppService private async Task> GetTopSellingAsync(string filter) { ISpecification specification = SpecificationFactory.Create(filter); - var orderItems = await _orderRepository.GetTopSelling(specification, true); - return ObjectMapper.Map, List>(orderItems); + var orderItems = await _orderRepository.GetDashboardAsync(specification); + return CreateTopSellingDtoMapping(orderItems); } private async Task> GetPercentOfTotalPaymentAsync(string filter) { ISpecification specification = SpecificationFactory.Create(filter); - var orders = await _orderRepository.GetPercentOfTotalPayment(specification); + var orders = await _orderRepository.GetDashboardAsync(specification, false); return CreatePaymentDtoMapping(orders); } private async Task> GetCountOfTotalOrderStatusAsync(string filter) { ISpecification specification = SpecificationFactory.Create(filter); - var orders = await _orderRepository.GetCountOfTotalOrderStatus(specification, true); + var orders = await _orderRepository.GetDashboardAsync(specification); return CreateOrderStatusDtoMapping(orders); } @@ -200,11 +200,24 @@ public class OrderAppService : ApplicationService, IOrderAppService return payments; } + private List CreateTopSellingDtoMapping(List orders) + { + var orderItems = orders.Select(p => p.OrderItems).SelectMany(p => p).ToList(); + + var topSetlling = orderItems + .GroupBy(p => new { p.ProductName, p.PictureUrl }) + .Select(p => new TopSellingDto { Units = p.Count(), ProductName = p.Key.ProductName, PictureUrl = p.Key.PictureUrl }) + .OrderByDescending(p => p.Units) + .Take(OrderConstants.Top10).ToList(); + + return topSetlling; + } + private List CreateOrderStatusDtoMapping(List orders) { var orderStatus = orders .GroupBy(p => p.OrderStatus) - .Select(p => new OrderStatusDto { CountOfStatusOrder = p.Count(), OrderStatus = p.Key.ToString(), }) + .Select(p => new OrderStatusDto { CountOfStatusOrder = p.Count(), OrderStatus = p.Key.ToString() }) .OrderBy(p => p.CountOfStatusOrder) .ToList(); From f3be62f7b5091539fbf12e2b3fe6413b52aebc36 Mon Sep 17 00:00:00 2001 From: malik masis Date: Wed, 24 Aug 2022 16:12:44 +0300 Subject: [PATCH 3/5] combined the actions --- .../Orders/IOrderRepository.cs | 12 ++----- .../Orders/EfCoreOrderRepository.cs | 31 ++----------------- 2 files changed, 4 insertions(+), 39 deletions(-) diff --git a/services/ordering/src/EShopOnAbp.OrderingService.Domain/Orders/IOrderRepository.cs b/services/ordering/src/EShopOnAbp.OrderingService.Domain/Orders/IOrderRepository.cs index 0e953790..97e3fd77 100644 --- a/services/ordering/src/EShopOnAbp.OrderingService.Domain/Orders/IOrderRepository.cs +++ b/services/ordering/src/EShopOnAbp.OrderingService.Domain/Orders/IOrderRepository.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Threading; using System.Threading.Tasks; using Volo.Abp.Domain.Repositories; @@ -20,16 +21,7 @@ public interface IOrderRepository : IRepository bool includeDetails = true, CancellationToken cancellationToken = default); - Task> GetTopSelling( - ISpecification spec, - bool includeDetails = true, - CancellationToken cancellationToken = default); - - Task> GetPercentOfTotalPayment( - ISpecification spec, - CancellationToken cancellationToken = default); - - Task> GetCountOfTotalOrderStatus( + Task> GetDashboardAsync( ISpecification spec, bool includeDetails = true, CancellationToken cancellationToken = default); diff --git a/services/ordering/src/EShopOnAbp.OrderingService.EntityFrameworkCore/Orders/EfCoreOrderRepository.cs b/services/ordering/src/EShopOnAbp.OrderingService.EntityFrameworkCore/Orders/EfCoreOrderRepository.cs index b717a372..f84e01d2 100644 --- a/services/ordering/src/EShopOnAbp.OrderingService.EntityFrameworkCore/Orders/EfCoreOrderRepository.cs +++ b/services/ordering/src/EShopOnAbp.OrderingService.EntityFrameworkCore/Orders/EfCoreOrderRepository.cs @@ -45,35 +45,8 @@ public class EfCoreOrderRepository : EfCoreRepository o.OrderDate) .ToListAsync(GetCancellationToken(cancellationToken)); } - - public async Task> GetTopSelling( - ISpecification spec, - bool includeDetails = true, - CancellationToken cancellationToken = default) - { - return (await (await GetDbSetAsync()) - .IncludeDetails(includeDetails) - .Where(spec.ToExpression()) - .SelectMany(oi => oi.OrderItems) - .GroupBy(p => p.ProductCode) - .OrderByDescending(o => o.Sum(p => p.Units)) - .Select(o => o.ToList()) - .ToListAsync()) - .SelectMany(t => t) - .Take(OrderConstants.Top10) - .ToList(); - } - - public async Task> GetPercentOfTotalPayment( - ISpecification spec, - CancellationToken cancellationToken = default) - { - return await (await GetDbSetAsync()) - .Where(spec.ToExpression()) - .ToListAsync(GetCancellationToken(cancellationToken)); - } - - public async Task> GetCountOfTotalOrderStatus( + + public async Task> GetDashboardAsync( ISpecification spec, bool includeDetails = true, CancellationToken cancellationToken = default) From f4f348f98a262b2826ead0c723d0922e1f95ee71 Mon Sep 17 00:00:00 2001 From: malik masis Date: Wed, 24 Aug 2022 16:12:51 +0300 Subject: [PATCH 4/5] Fixed grouping --- .../Orders/OrderAppService.cs | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/services/ordering/src/EShopOnAbp.OrderingService.Application/Orders/OrderAppService.cs b/services/ordering/src/EShopOnAbp.OrderingService.Application/Orders/OrderAppService.cs index 63b84cb2..91e46d3e 100644 --- a/services/ordering/src/EShopOnAbp.OrderingService.Application/Orders/OrderAppService.cs +++ b/services/ordering/src/EShopOnAbp.OrderingService.Application/Orders/OrderAppService.cs @@ -76,21 +76,21 @@ public class OrderAppService : ApplicationService, IOrderAppService private async Task> GetTopSellingAsync(string filter) { ISpecification specification = SpecificationFactory.Create(filter); - var orderItems = await _orderRepository.GetTopSelling(specification, true); - return ObjectMapper.Map, List>(orderItems); + var orderItems = await _orderRepository.GetDashboardAsync(specification); + return CreateTopSellingDtoMapping(orderItems); } private async Task> GetPercentOfTotalPaymentAsync(string filter) { ISpecification specification = SpecificationFactory.Create(filter); - var orders = await _orderRepository.GetPercentOfTotalPayment(specification); + var orders = await _orderRepository.GetDashboardAsync(specification, false); return CreatePaymentDtoMapping(orders); } private async Task> GetCountOfTotalOrderStatusAsync(string filter) { ISpecification specification = SpecificationFactory.Create(filter); - var orders = await _orderRepository.GetCountOfTotalOrderStatus(specification, true); + var orders = await _orderRepository.GetDashboardAsync(specification); return CreateOrderStatusDtoMapping(orders); } @@ -200,11 +200,24 @@ public class OrderAppService : ApplicationService, IOrderAppService return payments; } + private List CreateTopSellingDtoMapping(List orders) + { + var orderItems = orders.Select(p => p.OrderItems).SelectMany(p => p).ToList(); + + var topSetlling = orderItems + .GroupBy(p => new { p.ProductName, p.PictureUrl }) + .Select(p => new TopSellingDto { Units = p.Count(), ProductName = p.Key.ProductName, PictureUrl = p.Key.PictureUrl }) + .OrderByDescending(p => p.Units) + .Take(OrderConstants.Top10).ToList(); + + return topSetlling; + } + private List CreateOrderStatusDtoMapping(List orders) { var orderStatus = orders .GroupBy(p => p.OrderStatus) - .Select(p => new OrderStatusDto { CountOfStatusOrder = p.Count(), OrderStatus = p.Key.ToString(), }) + .Select(p => new OrderStatusDto { CountOfStatusOrder = p.Count(), OrderStatus = p.Key.ToString() }) .OrderBy(p => p.CountOfStatusOrder) .ToList(); From c37f83a459a022b1b701e3cded164d36ec637dfb Mon Sep 17 00:00:00 2001 From: malik masis Date: Fri, 2 Sep 2022 17:11:32 +0300 Subject: [PATCH 5/5] Updated grouping by ProductId --- .../Orders/OrderAppService.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/ordering/src/EShopOnAbp.OrderingService.Application/Orders/OrderAppService.cs b/services/ordering/src/EShopOnAbp.OrderingService.Application/Orders/OrderAppService.cs index 91e46d3e..93bcc00c 100644 --- a/services/ordering/src/EShopOnAbp.OrderingService.Application/Orders/OrderAppService.cs +++ b/services/ordering/src/EShopOnAbp.OrderingService.Application/Orders/OrderAppService.cs @@ -205,8 +205,8 @@ public class OrderAppService : ApplicationService, IOrderAppService var orderItems = orders.Select(p => p.OrderItems).SelectMany(p => p).ToList(); var topSetlling = orderItems - .GroupBy(p => new { p.ProductName, p.PictureUrl }) - .Select(p => new TopSellingDto { Units = p.Count(), ProductName = p.Key.ProductName, PictureUrl = p.Key.PictureUrl }) + .GroupBy(p => p.ProductId) + .Select(p => new TopSellingDto { Units = p.Count(), ProductName = p.First().ProductName , PictureUrl = p.First().PictureUrl }) .OrderByDescending(p => p.Units) .Take(OrderConstants.Top10).ToList();