diff --git a/ConfectionaryListImplement/ClientStorage.cs b/ConfectionaryListImplement/ClientStorage.cs index 9dcf91f..7540b52 100644 --- a/ConfectionaryListImplement/ClientStorage.cs +++ b/ConfectionaryListImplement/ClientStorage.cs @@ -2,11 +2,6 @@ using ConfectioneryContracts.SearchModels; using ConfectioneryContracts.StoragesContract; using ConfectioneryContracts.ViewModels; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace ConfectioneryListImplement { @@ -19,26 +14,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 +48,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 +76,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/ImplementerStorage.cs b/ConfectionaryListImplement/ImplementerStorage.cs index 99ae586..7dbf4ab 100644 --- a/ConfectionaryListImplement/ImplementerStorage.cs +++ b/ConfectionaryListImplement/ImplementerStorage.cs @@ -2,6 +2,7 @@ using ConfectioneryContracts.SearchModels; using ConfectioneryContracts.StoragesContract; using ConfectioneryContracts.ViewModels; +using System.Collections.Generic; namespace ConfectioneryListImplement { @@ -15,25 +16,30 @@ namespace ConfectioneryListImplement public ImplementerViewModel? Delete(ImplementerBindingModel model) { - var res = _source.Implementers.FirstOrDefault(x => x.Id == model.Id); - if (res != null) + for (int i = 0; i < _source.Implementers.Count; ++i) { - _source.Implementers.Remove(res); + if (_source.Implementers[i].Id == model.Id) + { + var element = _source.Implementers[i]; + _source.Implementers.RemoveAt(i); + return element.GetViewModel; + } } - return res?.GetViewModel; + return null; } public ImplementerViewModel? GetElement(ImplementerSearchModel model) { - if (model.Id.HasValue) - return _source.Implementers.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; - if (model.ImplementerFIO != null && model.Password != null) - return _source.Implementers - .FirstOrDefault(x => x.ImplementerFIO.Equals(model.ImplementerFIO) - && x.Password.Equals(model.Password)) - ?.GetViewModel; - if (model.ImplementerFIO != null) - return _source.Implementers.FirstOrDefault(x => x.ImplementerFIO.Equals(model.ImplementerFIO))?.GetViewModel; + foreach (var x in _source.Implementers) + { + if (model.Id.HasValue && x.Id == model.Id) + return x.GetViewModel; + if (model.ImplementerFIO != null && model.Password != null && + x.ImplementerFIO.Equals(model.ImplementerFIO) && x.Password.Equals(model.Password)) + return x.GetViewModel; + if (model.ImplementerFIO != null && x.ImplementerFIO.Equals(model.ImplementerFIO)) + return x.GetViewModel; + } return null; } @@ -48,19 +54,30 @@ namespace ConfectioneryListImplement var res = GetElement(model); return res != null ? new() { res } : new(); } - if (model.ImplementerFIO != null) // На случай если фио не будет уникальным (по заданию оно уникально) + // На случай если при расширении проекта фио не будет уникальным + // (по заданию оно уникально) + List result = new(); + if (model.ImplementerFIO != null) { - return _source.Implementers - .Where(x => x.ImplementerFIO.Equals(model.ImplementerFIO)) - .Select(x => x.GetViewModel) - .ToList(); + foreach (var implementer in _source.Implementers) + { + if (implementer.ImplementerFIO.Equals(model.ImplementerFIO)) + { + result.Add(implementer.GetViewModel); + } + } } - return new(); + return result; } public List GetFullList() { - return _source.Implementers.Select(x => x.GetViewModel).ToList(); + var result = new List(); + foreach (var implementer in _source.Implementers) + { + result.Add(implementer.GetViewModel); + } + return result; } public ImplementerViewModel? Insert(ImplementerBindingModel model) @@ -71,14 +88,20 @@ namespace ConfectioneryListImplement { _source.Implementers.Add(res); } - return res?.GetViewModel; + return res?.GetViewModel; } public ImplementerViewModel? Update(ImplementerBindingModel model) { - var res = _source.Implementers.FirstOrDefault(x => x.Id == model.Id); - res?.Update(model); - return res?.GetViewModel; + foreach (var implementer in _source.Implementers) + { + if (implementer.Id == model.Id) + { + implementer.Update(model); + return implementer.GetViewModel; + } + } + return null; } } } diff --git a/ConfectionaryListImplement/Order.cs b/ConfectionaryListImplement/Order.cs index 539716f..314778b 100644 --- a/ConfectionaryListImplement/Order.cs +++ b/ConfectionaryListImplement/Order.cs @@ -62,7 +62,6 @@ namespace ConfectioneryListImplement.Models public OrderViewModel GetViewModel => new() { PastryId = PastryId, - ClientFIO = DataListSingleton.GetInstance().Clients.FirstOrDefault(x => x.Id == ClientId)?.ClientFIO ?? string.Empty, ClientId = ClientId, ImplementerId = ImplementerId, ImplementerFIO = DataListSingleton.GetInstance().Implementers.FirstOrDefault(x => x.Id == ImplementerId)?.ImplementerFIO ?? string.Empty, diff --git a/ConfectionaryListImplement/OrderStorage.cs b/ConfectionaryListImplement/OrderStorage.cs index 43fd901..448ab68 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 { @@ -30,17 +31,6 @@ namespace ConfectioneryListImplement public OrderViewModel? GetElement(OrderSearchModel model) { - if (model.ImplementerId.HasValue && model.Statusses != null) - { - return _source.Orders - .FirstOrDefault(x => x.ImplementerId == model.ImplementerId && - model.Statusses.Contains(x.Status)) - ?.GetViewModel; - } - if (model.ImplementerId.HasValue) - { - return _source.Orders.FirstOrDefault(x => x.ImplementerId == model.ImplementerId)?.GetViewModel; - } if (!model.Id.HasValue) { return null; @@ -51,6 +41,15 @@ namespace ConfectioneryListImplement { return GetViewModel(order); } + else if (model.ImplementerId.HasValue && model.Statusses != null && + order.ImplementerId == model.ImplementerId && model.Statusses.Contains(order.Status)) + { + return GetViewModel(order); + } + else if (model.ImplementerId.HasValue && model.ImplementerId == order.ImplementerId) + { + return GetViewModel(order); + } } return null; } @@ -58,40 +57,32 @@ 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(); - } - if (!model.Id.HasValue && model.Statusses != null) - { - return _source.Orders - .Where(x => model.Statusses.Contains(x.Status)) - .Select(x => x.GetViewModel) - .ToList(); - } - if (model.ImplementerId.HasValue) - { - var res = _source.Orders.FirstOrDefault(x => x.ImplementerId == model.ImplementerId); - return res != null ? new() { res.GetViewModel } : new(); - } 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)); + } + else if (model.ImplementerId.HasValue && order.ImplementerId == model.ImplementerId) + { + result.Add(GetViewModel(order)); + } + else if (model.Statusses != null && model.Statusses.Contains(order.Status)) + { + result.Add(GetViewModel(order)); + } } - return new(); + return result; } public List GetFullList() @@ -147,6 +138,14 @@ namespace ConfectioneryListImplement break; } } + foreach (var client in _source.Clients) + { + if (client.Id == res.ClientId) + { + res.ClientFIO = client.ClientFIO; + break; + } + } return res; } }