From 81a96d2fd44508bd05dc93ea6c6b6bc45c88235d Mon Sep 17 00:00:00 2001 From: Galip Tolga Erdem Date: Thu, 6 Jan 2022 17:12:14 +0300 Subject: [PATCH] updated specification implementation --- .../Orders/OrderAppService.cs | 9 +++------ .../Orders/Specifications/Last30DaysSpecification.cs | 6 ++++-- .../Orders/Specifications/MonthsAgoSpecification.cs | 4 ++-- .../Orders/Specifications/SpecificationFactory.cs | 8 +++++++- .../Orders/EfCoreOrderRepository.cs | 1 + 5 files changed, 17 insertions(+), 11 deletions(-) diff --git a/services/ordering/src/EShopOnAbp.OrderingService.Application/Orders/OrderAppService.cs b/services/ordering/src/EShopOnAbp.OrderingService.Application/Orders/OrderAppService.cs index 9d73a302..676173d8 100644 --- a/services/ordering/src/EShopOnAbp.OrderingService.Application/Orders/OrderAppService.cs +++ b/services/ordering/src/EShopOnAbp.OrderingService.Application/Orders/OrderAppService.cs @@ -32,13 +32,10 @@ public class OrderAppService : ApplicationService, IOrderAppService public async Task> GetMyOrdersAsync(GetMyOrdersInput input) { - ISpecification specification = null; - if (!input.Filter.IsNullOrEmpty()) - { - specification = SpecificationFactory.Create(input.Filter); - } - + ISpecification specification = SpecificationFactory.Create(input.Filter); + var orders = await _orderRepository.GetOrdersByUserId(CurrentUser.GetId(), specification, true); + return CreateOrderDtoMapping(orders); } diff --git a/services/ordering/src/EShopOnAbp.OrderingService.Domain/Orders/Specifications/Last30DaysSpecification.cs b/services/ordering/src/EShopOnAbp.OrderingService.Domain/Orders/Specifications/Last30DaysSpecification.cs index 6c90e62a..6f284d44 100644 --- a/services/ordering/src/EShopOnAbp.OrderingService.Domain/Orders/Specifications/Last30DaysSpecification.cs +++ b/services/ordering/src/EShopOnAbp.OrderingService.Domain/Orders/Specifications/Last30DaysSpecification.cs @@ -8,7 +8,9 @@ public class Last30DaysSpecification : Specification { public override Expression> ToExpression() { - var daysAgo30 = DateTime.Now.AddDays(-30); - return query => query.OrderDate >= daysAgo30 && query.OrderDate <= DateTime.Now; + var daysAgo30 = DateTime.UtcNow.Subtract(TimeSpan.FromDays(30)); + return query => query.OrderDate >= daysAgo30 + ; + // && query.OrderDate <= DateTime.UtcNow; } } \ No newline at end of file diff --git a/services/ordering/src/EShopOnAbp.OrderingService.Domain/Orders/Specifications/MonthsAgoSpecification.cs b/services/ordering/src/EShopOnAbp.OrderingService.Domain/Orders/Specifications/MonthsAgoSpecification.cs index fc085fcd..3bcb10e7 100644 --- a/services/ordering/src/EShopOnAbp.OrderingService.Domain/Orders/Specifications/MonthsAgoSpecification.cs +++ b/services/ordering/src/EShopOnAbp.OrderingService.Domain/Orders/Specifications/MonthsAgoSpecification.cs @@ -15,7 +15,7 @@ public class MonthsAgoSpecification : Specification public override Expression> ToExpression() { - var monthsAgo = DateTime.Now.AddMonths(NumberOfMonths); - return query => query.OrderDate >= monthsAgo && query.OrderDate <= DateTime.Now; + var monthsAgo = DateTime.UtcNow.AddMonths(-NumberOfMonths); + return query => query.OrderDate >= monthsAgo; } } \ No newline at end of file diff --git a/services/ordering/src/EShopOnAbp.OrderingService.Domain/Orders/Specifications/SpecificationFactory.cs b/services/ordering/src/EShopOnAbp.OrderingService.Domain/Orders/Specifications/SpecificationFactory.cs index 68f927f6..4bb9e0fb 100644 --- a/services/ordering/src/EShopOnAbp.OrderingService.Domain/Orders/Specifications/SpecificationFactory.cs +++ b/services/ordering/src/EShopOnAbp.OrderingService.Domain/Orders/Specifications/SpecificationFactory.cs @@ -1,4 +1,5 @@ -using Volo.Abp.Specifications; +using System; +using Volo.Abp.Specifications; namespace EShopOnAbp.OrderingService.Orders.Specifications; @@ -6,6 +7,11 @@ public static class SpecificationFactory { public static ISpecification Create(string filter) { + if (filter.IsNullOrEmpty()) + { + return new Last30DaysSpecification(); + } + if (filter.StartsWith("y")) { var year = int.Parse(filter.Split('y')[1]); diff --git a/services/ordering/src/EShopOnAbp.OrderingService.EntityFrameworkCore/Orders/EfCoreOrderRepository.cs b/services/ordering/src/EShopOnAbp.OrderingService.EntityFrameworkCore/Orders/EfCoreOrderRepository.cs index bc7cb8c3..e71f3acb 100644 --- a/services/ordering/src/EShopOnAbp.OrderingService.EntityFrameworkCore/Orders/EfCoreOrderRepository.cs +++ b/services/ordering/src/EShopOnAbp.OrderingService.EntityFrameworkCore/Orders/EfCoreOrderRepository.cs @@ -38,6 +38,7 @@ public class EfCoreOrderRepository : EfCoreRepositoryq.Buyer.Id == userId) .Where(spec.ToExpression()) + .OrderByDescending(o=>o.OrderDate) .ToListAsync(GetCancellationToken(cancellationToken)); }