From 4c4689f94c2c9c72b16e122a26d2f78c40e88924 Mon Sep 17 00:00:00 2001 From: DavidMakarov Date: Thu, 18 Apr 2024 19:47:00 +0400 Subject: [PATCH] fix GetFilteredList methods --- .../Implements/OrderStorage.cs | 15 +++--- .../Implements/OrderStorage.cs | 21 ++++---- .../Implements/OrderStorage.cs | 52 +++++++------------ .../Controllers/MainController.cs | 6 +-- 4 files changed, 44 insertions(+), 50 deletions(-) diff --git a/FlowerShop/FlowerShopDatabaseImplement/Implements/OrderStorage.cs b/FlowerShop/FlowerShopDatabaseImplement/Implements/OrderStorage.cs index 96629ec..b6c2941 100644 --- a/FlowerShop/FlowerShopDatabaseImplement/Implements/OrderStorage.cs +++ b/FlowerShop/FlowerShopDatabaseImplement/Implements/OrderStorage.cs @@ -22,12 +22,15 @@ namespace FlowerShopDatabaseImplement.Implements return new(); } using var context = new FlowerShopDatabase(); - if (model.Id.HasValue) - return context.Orders.Where(x => x.Id == model.Id).Select(x => AccessFlowerStorage(x.GetViewModel)).ToList(); - if (model.ClientId.HasValue) - return context.Orders.Where(x => x.ClientId == model.ClientId).Select(x => AccessFlowerStorage(x.GetViewModel)).ToList(); - return context.Orders.Where(x => x.DateCreate >= model.DateFrom).Where(x => x.DateCreate <= model.DateTo). - Select(x => AccessFlowerStorage(x.GetViewModel)).ToList(); + if (model.DateFrom.HasValue) + return context.Orders + .Where(x => (x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo) + && (!model.ClientId.HasValue || x.ClientId == model.ClientId)) + .Select(x => AccessFlowerStorage(x.GetViewModel)).ToList(); + return context.Orders + .Where(x => (model.Id.HasValue && x.Id == model.Id) + || (model.ClientId.HasValue && x.ClientId == model.ClientId)) + .Select(x => AccessFlowerStorage(x.GetViewModel)).ToList(); } public OrderViewModel? GetElement(OrderSearchModel model) { diff --git a/FlowerShop/FlowerShopFileImplement/Implements/OrderStorage.cs b/FlowerShop/FlowerShopFileImplement/Implements/OrderStorage.cs index 4ecf11f..ff602a5 100644 --- a/FlowerShop/FlowerShopFileImplement/Implements/OrderStorage.cs +++ b/FlowerShop/FlowerShopFileImplement/Implements/OrderStorage.cs @@ -19,15 +19,18 @@ namespace FlowerShopFileImplement.Implements } public List GetFilteredList(OrderSearchModel model) { - if (model.Id.HasValue) - return source.Orders.Where(x => x.Id == model.Id) - .Select(x => GetViewModel(x)) - .ToList(); - if (model.ClientId.HasValue) - return source.Orders.Where(x => x.ClientId == model.ClientId).Select(x => GetViewModel(x)).ToList(); - return source.Orders.Where(x => x.DateCreate >= model.DateFrom).Where(x => x.DateCreate <= model.DateTo). - Select(x => GetViewModel(x)).ToList(); - } + if (model.DateFrom.HasValue) + { + return source.Orders + .Where(x => (x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo) && (!model.ClientId.HasValue || x.ClientId == model.ClientId)) + .Select(GetViewModel) + .ToList(); + } + return source.Orders + .Where(x => (model.Id.HasValue && x.Id == model.Id) || (model.ClientId.HasValue && x.ClientId == model.ClientId)) + .Select(GetViewModel) + .ToList(); + } public OrderViewModel? GetElement(OrderSearchModel model) { if (!model.Id.HasValue) diff --git a/FlowerShop/FlowerShopListImplement/Implements/OrderStorage.cs b/FlowerShop/FlowerShopListImplement/Implements/OrderStorage.cs index 7e1abb6..86bbf74 100644 --- a/FlowerShop/FlowerShopListImplement/Implements/OrderStorage.cs +++ b/FlowerShop/FlowerShopListImplement/Implements/OrderStorage.cs @@ -3,6 +3,7 @@ using FlowerShopContracts.SearchModels; using FlowerShopContracts.StoragesContracts; using FlowerShopContracts.ViewModels; using FlowerShopListImplement.Models; +using System.Reflection; namespace FlowerShopListImplement.Implements { @@ -24,39 +25,26 @@ namespace FlowerShopListImplement.Implements } public List GetFilteredList(OrderSearchModel model) { - var result = new List(); - if (model == null || !model.Id.HasValue) - { - return result; - } - if (model.ClientId.HasValue) - { - foreach (var order in _source.Orders) - { - if (order.Id == model.Id && order.ClientId == model.ClientId) - { - result.Add(AttachNames(order.GetViewModel)); - } - } - return result; + if (model.DateFrom.HasValue) + { + return _source.Orders + .Where(x => (x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo) && (!model.ClientId.HasValue || x.ClientId == model.ClientId)) + .Select(x => AttachNames(x.GetViewModel)) + .ToList(); } - foreach (var order in _source.Orders) - { - if (order.Id == model.Id && order.DateCreate >= model.DateFrom && order.DateCreate <= model.DateTo) - { - result.Add(AttachNames(order.GetViewModel)); - } - } - return result; - } - public OrderViewModel? GetElement(OrderSearchModel model) - { - if (!model.Id.HasValue) - { - return null; - } - foreach (var order in _source.Orders) - { + return _source.Orders + .Where(x => (model.Id.HasValue && x.Id == model.Id) || (model.ClientId.HasValue && x.ClientId == model.ClientId)) + .Select(x => AttachNames(x.GetViewModel)) + .ToList(); + } + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + foreach (var order in _source.Orders) + { if (model.Id.HasValue && order.Id == model.Id) { return AttachNames(order.GetViewModel); diff --git a/FlowerShop/FlowerShopRestApi/Controllers/MainController.cs b/FlowerShop/FlowerShopRestApi/Controllers/MainController.cs index 459d3bd..cfe3c00 100644 --- a/FlowerShop/FlowerShopRestApi/Controllers/MainController.cs +++ b/FlowerShop/FlowerShopRestApi/Controllers/MainController.cs @@ -50,14 +50,14 @@ namespace FlowerShopRestApi.Controllers } } [HttpGet] - public List? GetOrders(int clientId) + public List? GetOrders(int? clientId) { try { - return _order.ReadList(new OrderSearchModel + return _order.ReadList(clientId != null ? new OrderSearchModel { ClientId = clientId - }); + } : null); } catch (Exception ex) {