From 209f1fa73bccefe0e29f5264af7e543ba37697cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Mon, 13 Mar 2023 16:43:23 +0400 Subject: [PATCH 1/3] =?UTF-8?q?=D0=A3=D0=B4=D0=B0=D0=BB=D0=B5=D0=BD=D1=8B?= =?UTF-8?q?=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=D1=8B=20LINQ=20=D0=B2=20ListI?= =?UTF-8?q?mplement?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ConfectionaryListImplement/ClientStorage.cs | 86 +++++++++++---------- ConfectionaryListImplement/OrderStorage.cs | 27 +++---- 2 files changed, 59 insertions(+), 54 deletions(-) diff --git a/ConfectionaryListImplement/ClientStorage.cs b/ConfectionaryListImplement/ClientStorage.cs index 9dcf91f..0d441ec 100644 --- a/ConfectionaryListImplement/ClientStorage.cs +++ b/ConfectionaryListImplement/ClientStorage.cs @@ -5,6 +5,7 @@ using ConfectioneryContracts.ViewModels; using System; using System.Collections.Generic; using System.Linq; +using System.Security.Cryptography.X509Certificates; using System.Text; using System.Threading.Tasks; @@ -19,26 +20,31 @@ namespace ConfectioneryListImplement } public ClientViewModel? Delete(ClientBindingModel model) - { - var res = _source.Clients.FirstOrDefault(x => x.Id == model.Id); - if (res != null) - { - _source.Clients.Remove(res); - } - return res?.GetViewModel; - } + { + for (int i = 0; i < _source.Clients.Count; ++i) + { + if (_source.Clients[i].Id == model.Id) + { + var element = _source.Clients[i]; + _source.Clients.RemoveAt(i); + return element.GetViewModel; + } + } + return null; + } - public ClientViewModel? GetElement(ClientSearchModel model) + public ClientViewModel? GetElement(ClientSearchModel model) { - if (model.Id.HasValue) - return _source.Clients.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; - if (model.Email != null && model.Password != null) - return _source.Clients - .FirstOrDefault(x => x.Email.Equals(model.Email) - && x.Password.Equals(model.Password)) - ?.GetViewModel; - if (model.Email != null) - return _source.Clients.FirstOrDefault(x => x.Email.Equals(model.Email))?.GetViewModel; + foreach (var client in _source.Clients) + { + if (model.Id.HasValue && model.Id == client.Id) + return client.GetViewModel; + if (model.Email != null && model.Password != null && + client.Email.Equals(model.Email) && client.Password.Equals(model.Password)) + return client.GetViewModel; + if (model.Email != null && client.Email.Equals(model.Email)) + return client.GetViewModel; + } return null; } @@ -48,25 +54,21 @@ namespace ConfectioneryListImplement { return new(); } - if (model.Id.HasValue) - { - var res = GetElement(model); - return res != null ? new() { res } : new(); - } - if (model.Email != null) - { - return _source.Clients - .Where(x => x.Email.Contains(model.Email)) - .Select(x => x.GetViewModel) - .ToList(); - } - return new(); + // У нас нет каких либо специфических условий для выборки списка клиентов, + // поэтому получаем лишь один элемент по модели + var res = GetElement(model); + return res != null ? new() { res } : new(); } public List GetFullList() { - return _source.Clients.Select(x => x.GetViewModel).ToList(); - } + var result = new List(); + foreach (var client in _source.Clients) + { + result.Add(client.GetViewModel); + } + return result; + } public ClientViewModel? Insert(ClientBindingModel model) { @@ -80,10 +82,16 @@ namespace ConfectioneryListImplement } public ClientViewModel? Update(ClientBindingModel model) - { - var res = _source.Clients.FirstOrDefault(x => x.Id == model.Id); - res?.Update(model); - return res?.GetViewModel; - } - } + { + foreach (var client in _source.Clients) + { + if (client.Id == model.Id) + { + client.Update(model); + return client.GetViewModel; + } + } + return null; + } + } } diff --git a/ConfectionaryListImplement/OrderStorage.cs b/ConfectionaryListImplement/OrderStorage.cs index 8b0558a..605c158 100644 --- a/ConfectionaryListImplement/OrderStorage.cs +++ b/ConfectionaryListImplement/OrderStorage.cs @@ -3,6 +3,7 @@ using ConfectioneryContracts.SearchModels; using ConfectioneryContracts.StoragesContract; using ConfectioneryContracts.ViewModels; using ConfectioneryListImplement.Models; +using System.Security.Cryptography.X509Certificates; namespace ConfectioneryListImplement { @@ -47,28 +48,24 @@ namespace ConfectioneryListImplement public List GetFilteredList(OrderSearchModel model) { var result = new List(); - if (!model.Id.HasValue && model.DateFrom.HasValue && model.DateTo.HasValue) // если не ищем по айдишнику, значит ищем по диапазону дат - { - return _source.Orders - .Where(x => model.DateFrom <= x.DateCreate.Date && x.DateCreate <= model.DateTo) - .Select(x => x.GetViewModel) - .ToList(); - } - if (!model.Id.HasValue && model.ClientId.HasValue) - { - return _source.Orders - .Where(x => x.ClientId == model.ClientId) - .Select(x => x.GetViewModel) - .ToList(); - } foreach (var order in _source.Orders) { if (order.Id == model.Id) { return new() { GetViewModel(order) }; + } + // если не ищем по айдишнику, значит ищем по диапазону дат + else if (model.DateFrom.HasValue && model.DateTo.HasValue && + model.DateFrom <= order.DateCreate.Date && order.DateCreate.Date <= model.DateTo) + { + result.Add(GetViewModel(order)); + } + else if (model.ClientId.HasValue && order.ClientId == model.ClientId) + { + result.Add(GetViewModel(order)); } } - return new(); + return result; } public List GetFullList() From a157403c04978fdf359aa0c1406f84b339e44d97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Mon, 13 Mar 2023 17:23:49 +0400 Subject: [PATCH 2/3] =?UTF-8?q?=D1=83=D0=B4=D0=B0=D0=BB=D0=B5=D0=BD=20LINQ?= =?UTF-8?q?=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=20=D0=BF=D0=BE=D0=BB=D1=83?= =?UTF-8?q?=D1=87=D0=B5=D0=BD=D0=B8=D1=8F=20=D1=84=D0=B8=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ConfectionaryListImplement/ClientStorage.cs | 6 ------ ConfectionaryListImplement/Order.cs | 1 - ConfectionaryListImplement/OrderStorage.cs | 8 ++++++++ 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/ConfectionaryListImplement/ClientStorage.cs b/ConfectionaryListImplement/ClientStorage.cs index 0d441ec..7540b52 100644 --- a/ConfectionaryListImplement/ClientStorage.cs +++ b/ConfectionaryListImplement/ClientStorage.cs @@ -2,12 +2,6 @@ using ConfectioneryContracts.SearchModels; using ConfectioneryContracts.StoragesContract; using ConfectioneryContracts.ViewModels; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Security.Cryptography.X509Certificates; -using System.Text; -using System.Threading.Tasks; namespace ConfectioneryListImplement { diff --git a/ConfectionaryListImplement/Order.cs b/ConfectionaryListImplement/Order.cs index 9954ae8..d6bfc12 100644 --- a/ConfectionaryListImplement/Order.cs +++ b/ConfectionaryListImplement/Order.cs @@ -58,7 +58,6 @@ namespace ConfectioneryListImplement.Models public OrderViewModel GetViewModel => new() { PastryId = PastryId, - ClientFIO = DataListSingleton.GetInstance().Clients.FirstOrDefault(x => x.Id == ClientId)?.ClientFIO ?? string.Empty, ClientId = ClientId, Count = Count, Sum = Sum, diff --git a/ConfectionaryListImplement/OrderStorage.cs b/ConfectionaryListImplement/OrderStorage.cs index 605c158..6c794aa 100644 --- a/ConfectionaryListImplement/OrderStorage.cs +++ b/ConfectionaryListImplement/OrderStorage.cs @@ -121,6 +121,14 @@ namespace ConfectioneryListImplement break; } } + foreach (var client in _source.Clients) + { + if (client.Id == res.ClientId) + { + res.ClientFIO = client.ClientFIO; + break; + } + } return res; } } From d9d36e1eec22dd1a278fe4cf79e8294a4e14ae32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Mon, 13 Mar 2023 17:32:16 +0400 Subject: [PATCH 3/3] =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ConfectioneryDatabaseImplement/Order.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ConfectioneryDatabaseImplement/Order.cs b/ConfectioneryDatabaseImplement/Order.cs index f13c47d..c9b44a6 100644 --- a/ConfectioneryDatabaseImplement/Order.cs +++ b/ConfectioneryDatabaseImplement/Order.cs @@ -89,7 +89,7 @@ namespace ConfectioneryDatabaseImplement.Models return new() { PastryName = context.Pastries.FirstOrDefault(x => x.Id == PastryId)?.PastryName ?? string.Empty, - ClientFIO = Client?.ClientFIO ?? context.Clients.FirstOrDefault(x => x.Id == ClientId)?.ClientFIO ?? string.Empty, + ClientFIO = Client?.ClientFIO ?? string.Empty, ImplementerFIO = Implementer?.ImplementerFIO ?? string.Empty, PastryId = PastryId, Count = Count,