From db6fd2a64753aa1140e07f07d88fbb4f3e429fa1 Mon Sep 17 00:00:00 2001 From: ekallin Date: Sun, 25 Feb 2024 19:43:57 +0400 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D1=8B=20=D0=B1=D0=B8=D0=B1=D0=BB=D0=B8=D0=BE=D1=82=D0=B5?= =?UTF-8?q?=D0=BA=D0=B8=20SushiBarBusinessLogic=20=D0=B8=20SushiBarListImp?= =?UTF-8?q?lements?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SushiBar/SushiBar.csproj | 8 ++ SushiBar/SushiBar.sln | 12 ++ SushiBarBusinessLogic/ComponentLogic.cs | 113 ++++++++++++++++ SushiBarBusinessLogic/OrderLogic.cs | 124 ++++++++++++++++++ .../SushiBarBusinessLogic.csproj | 21 +++ SushiBarBusinessLogic/SushiLogic.cs | 109 +++++++++++++++ SushiBarContracts/SushiBarContracts.csproj | 8 ++ SushiBarDataModels/SushiBarDataModels.csproj | 8 ++ .../Components/Component.cs | 41 ++++++ .../Components/ComponentStorage.cs | 101 ++++++++++++++ SushiBarListImplements/DataListSingleton.cs | 29 ++++ SushiBarListImplements/Orders/Order.cs | 62 +++++++++ SushiBarListImplements/Orders/OrderStorage.cs | 121 +++++++++++++++++ .../SushiBarListImplements.csproj | 14 ++ SushiBarListImplements/Sushis/Sushi.cs | 49 +++++++ SushiBarListImplements/Sushis/SushiStorage.cs | 108 +++++++++++++++ 16 files changed, 928 insertions(+) create mode 100644 SushiBarBusinessLogic/ComponentLogic.cs create mode 100644 SushiBarBusinessLogic/OrderLogic.cs create mode 100644 SushiBarBusinessLogic/SushiBarBusinessLogic.csproj create mode 100644 SushiBarBusinessLogic/SushiLogic.cs create mode 100644 SushiBarListImplements/Components/Component.cs create mode 100644 SushiBarListImplements/Components/ComponentStorage.cs create mode 100644 SushiBarListImplements/DataListSingleton.cs create mode 100644 SushiBarListImplements/Orders/Order.cs create mode 100644 SushiBarListImplements/Orders/OrderStorage.cs create mode 100644 SushiBarListImplements/SushiBarListImplements.csproj create mode 100644 SushiBarListImplements/Sushis/Sushi.cs create mode 100644 SushiBarListImplements/Sushis/SushiStorage.cs diff --git a/SushiBar/SushiBar.csproj b/SushiBar/SushiBar.csproj index 663fdb8..0adb268 100644 --- a/SushiBar/SushiBar.csproj +++ b/SushiBar/SushiBar.csproj @@ -8,4 +8,12 @@ enable + + + + + + + + \ No newline at end of file diff --git a/SushiBar/SushiBar.sln b/SushiBar/SushiBar.sln index 6400d68..443b087 100644 --- a/SushiBar/SushiBar.sln +++ b/SushiBar/SushiBar.sln @@ -9,6 +9,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SushiBarDataModels", "..\Su EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SushiBarContracts", "..\SushiBarContracts\SushiBarContracts.csproj", "{0C8B928F-ACD2-4196-8F2B-5190914911D8}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SushiBarBusinessLogic", "..\SushiBarBusinessLogic\SushiBarBusinessLogic.csproj", "{B4061C5A-A7C4-4F49-A1AC-083877203CD8}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SushiBarListImplements", "..\SushiBarListImplements\SushiBarListImplements.csproj", "{BAA05DBA-C77F-4CF6-9998-4C150FA5797C}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -27,6 +31,14 @@ Global {0C8B928F-ACD2-4196-8F2B-5190914911D8}.Debug|Any CPU.Build.0 = Debug|Any CPU {0C8B928F-ACD2-4196-8F2B-5190914911D8}.Release|Any CPU.ActiveCfg = Release|Any CPU {0C8B928F-ACD2-4196-8F2B-5190914911D8}.Release|Any CPU.Build.0 = Release|Any CPU + {B4061C5A-A7C4-4F49-A1AC-083877203CD8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B4061C5A-A7C4-4F49-A1AC-083877203CD8}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B4061C5A-A7C4-4F49-A1AC-083877203CD8}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B4061C5A-A7C4-4F49-A1AC-083877203CD8}.Release|Any CPU.Build.0 = Release|Any CPU + {BAA05DBA-C77F-4CF6-9998-4C150FA5797C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BAA05DBA-C77F-4CF6-9998-4C150FA5797C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BAA05DBA-C77F-4CF6-9998-4C150FA5797C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BAA05DBA-C77F-4CF6-9998-4C150FA5797C}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/SushiBarBusinessLogic/ComponentLogic.cs b/SushiBarBusinessLogic/ComponentLogic.cs new file mode 100644 index 0000000..f50e47d --- /dev/null +++ b/SushiBarBusinessLogic/ComponentLogic.cs @@ -0,0 +1,113 @@ +using SushiBarContracts.BindingModel; +using SushiBarContracts.BusinessLogicsContracts; +using SushiBarContracts.SearchModel; +using SushiBarContracts.StoragesContracts; +using SushiBarContracts.ViewModels; +using Microsoft.Extensions.Logging; + + +namespace SushiBarBusinessLogic.BusinessLogic +{ + public class ComponentLogic : IComponentLogic + { + private readonly ILogger _logger; + private readonly IComponentStorage _componentStorage; + + public ComponentLogic(ILogger logger, IComponentStorage componentStorage) + { + _logger = logger; + _componentStorage = componentStorage; + } + + public List? ReadList(ComponentSearchModel? model) + { + _logger.LogInformation("ReadList. ComponentName:{ComponentName}. Id:{ Id}", model?.ComponentName, model?.Id); + var list = model == null ? _componentStorage.GetFullList() : _componentStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + public ComponentViewModel? ReadElement(ComponentSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. ComponentName:{ComponentName}.Id:{ Id}", model.ComponentName, model.Id); + var element = _componentStorage.GetElement(model); + if (element == null) + { + _logger.LogWarning("ReadElement element not found"); + return null; + } + _logger.LogInformation("ReadElement find. Id:{Id}", element.Id); + return element; + } + public bool Create(ComponentBindingModel model) + { + CheckModel(model); + if (_componentStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + public bool Update(ComponentBindingModel model) + { + CheckModel(model); + if (_componentStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + public bool Delete(ComponentBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_componentStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + private void CheckModel(ComponentBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.ComponentName)) + { + throw new ArgumentNullException("Нет названия компонента", + nameof(model.ComponentName)); + } + if (model.Cost <= 0) + { + throw new ArgumentNullException("Цена компонента должна быть больше 0", nameof(model.Cost)); + } + _logger.LogInformation("Component. ComponentName:{ComponentName}. Cost:{ Cost}. Id: { Id}", + model.ComponentName, model.Cost, model.Id); + + var element = _componentStorage.GetElement(new ComponentSearchModel { + ComponentName = model.ComponentName + }); + + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Компонент с таким названием уже есть"); + } + } + } +} diff --git a/SushiBarBusinessLogic/OrderLogic.cs b/SushiBarBusinessLogic/OrderLogic.cs new file mode 100644 index 0000000..ac24cdd --- /dev/null +++ b/SushiBarBusinessLogic/OrderLogic.cs @@ -0,0 +1,124 @@ +using SushiBarContracts.BindingModel; +using SushiBarContracts.BusinessLogicsContracts; +using SushiBarContracts.SearchModel; +using SushiBarContracts.StoragesContracts; +using SushiBarContracts.ViewModels; +using Microsoft.Extensions.Logging; +using SushiBarDataModels.Enums; + +namespace SushiBarBusinessLogic.BusinessLogic +{ + public class OrderLogic : IOrderLogic + { + private readonly ILogger _logger; + private readonly IOrderStorage _orderStorage; + + public OrderLogic(ILogger logger, IOrderStorage orderStorage) + { + _logger = logger; + _orderStorage = orderStorage; + } + + public List? ReadList(OrderSearchModel? model) + { + _logger.LogInformation("ReadList. Id:{ Id}", model?.Id); + var list = model == null ? _orderStorage.GetFullList() : _orderStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + + public bool CreateOrder(OrderBindingModel model) + { + CheckModel(model); + if (model.Status != OrderStatus.Неизвестен) return false; + model.Status = OrderStatus.Принят; + if (_orderStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool TakeOrderInWork(OrderBindingModel model) + { + return ChangeStatus(model, OrderStatus.Выполняется); + } + + public bool FinishOrder(OrderBindingModel model) + { + return ChangeStatus(model, OrderStatus.Готов); + } + + public bool DeliveryOrder(OrderBindingModel model) + { + return ChangeStatus(model, OrderStatus.Выдан); + } + + private bool ChangeStatus(OrderBindingModel model, OrderStatus orderStatus) + { + CheckModel(model, false); + var order = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id }); + if (order == null) + { + _logger.LogWarning("Change status operation failed. Order not found"); + return false; + } + if (order.Status + 1 != orderStatus) + { + _logger.LogWarning("Change status operation failed. Incorrect new status: {orderStatus}. Current status: {Status}", + orderStatus, order.Status); + return false; + } + + model.SushiId = order.SushiId; + model.Count = order.Count; + model.Sum = order.Sum; + model.DateCreate = order.DateCreate; + model.Status = orderStatus; + + if (model.Status == OrderStatus.Готов) + { + model.DateImplement = DateTime.Now; + } + else + { + model.DateImplement = order.DateImplement; + } + if (_orderStorage.Update(model) == null) + { + _logger.LogWarning("Change status operation failed"); + return false; + } + return true; + } + + private void CheckModel(OrderBindingModel model, bool withParams = true) + { + if (model == null) + throw new ArgumentNullException(nameof(model)); + + if (!withParams) return; + + if (model.SushiId <= 0) + throw new ArgumentNullException("Неверный идентификатор суши", nameof(model.SushiId)); + + if (model.Count <= 0) + throw new ArgumentNullException("Количество суш в заказе не может быть меньше нуля или равно нулю", nameof(model.Count)); + + if (model.Sum <= 0) + throw new ArgumentNullException("Стоимость заказа не может быть меньше нуля", nameof(model.Sum)); + + if (model.DateImplement.HasValue && model.DateImplement < model.DateCreate) + throw new ArithmeticException("Заказ должен быть выдан позже, чем был создан"); + + _logger.LogInformation("Sushi. SushiId:{SushiId}. Count:{ Count}. Sum:{ Sum}. Id: { Id}", + model.SushiId, model.Count, model.Sum, model.Id); + } + } +} diff --git a/SushiBarBusinessLogic/SushiBarBusinessLogic.csproj b/SushiBarBusinessLogic/SushiBarBusinessLogic.csproj new file mode 100644 index 0000000..033d6c7 --- /dev/null +++ b/SushiBarBusinessLogic/SushiBarBusinessLogic.csproj @@ -0,0 +1,21 @@ + + + + net8.0 + enable + enable + + + + + + + + + + + + + + + diff --git a/SushiBarBusinessLogic/SushiLogic.cs b/SushiBarBusinessLogic/SushiLogic.cs new file mode 100644 index 0000000..9ffbe64 --- /dev/null +++ b/SushiBarBusinessLogic/SushiLogic.cs @@ -0,0 +1,109 @@ +using SushiBarContracts.BindingModel; +using SushiBarContracts.BusinessLogicsContracts; +using SushiBarContracts.SearchModel; +using SushiBarContracts.StoragesContracts; +using SushiBarContracts.ViewModels; +using Microsoft.Extensions.Logging; + +namespace SushiBarBusinessLogic.BusinessLogic +{ + public class SushiLogic : ISushiLogic + { + private readonly ILogger _logger; + private readonly ISushiStorage _sushiStorage; + + public SushiLogic(ILogger logger, ISushiStorage sushiStorage) + { + _logger = logger; + _sushiStorage = sushiStorage; + } + + public List? ReadList(SushiSearchModel? model) + { + _logger.LogInformation("ReadList. SushiName:{SushiName}. Id:{ Id}", model?.SushiName, model?.Id); + var list = model == null ? _sushiStorage.GetFullList() : _sushiStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + public SushiViewModel? ReadElement(SushiSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. SushiName:{SushiName}.Id:{ Id}", model.SushiName, model.Id); + var element = _sushiStorage.GetElement(model); + if (element == null) + { + _logger.LogWarning("ReadElement element not found"); + return null; + } + _logger.LogInformation("ReadElement find. Id:{Id}", element.Id); + return element; + } + public bool Create(SushiBindingModel model) + { + CheckModel(model); + if (_sushiStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + public bool Update(SushiBindingModel model) + { + CheckModel(model); + if (_sushiStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + public bool Delete(SushiBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_sushiStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + private void CheckModel(SushiBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.SushiName)) + { + throw new ArgumentNullException("Нет названия компонента", nameof(model.SushiName)); + } + if (model.Price <= 0) + { + throw new ArgumentNullException("Цена продукта должна быть больше 0", nameof(model.Price)); + } + _logger.LogInformation("Sushi. SushiName:{SushiName}. Price:{ Price}. Id: { Id}", + model.SushiName, model.Price, model.Id); + + var element = _sushiStorage.GetElement(new SushiSearchModel{SushiName = model.SushiName}); + + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Компонент с таким названием уже есть"); + } + } + } +} diff --git a/SushiBarContracts/SushiBarContracts.csproj b/SushiBarContracts/SushiBarContracts.csproj index 24184cb..47af33f 100644 --- a/SushiBarContracts/SushiBarContracts.csproj +++ b/SushiBarContracts/SushiBarContracts.csproj @@ -6,6 +6,14 @@ enable + + + + + + + + diff --git a/SushiBarDataModels/SushiBarDataModels.csproj b/SushiBarDataModels/SushiBarDataModels.csproj index fa71b7a..1409f86 100644 --- a/SushiBarDataModels/SushiBarDataModels.csproj +++ b/SushiBarDataModels/SushiBarDataModels.csproj @@ -6,4 +6,12 @@ enable + + + + + + + + diff --git a/SushiBarListImplements/Components/Component.cs b/SushiBarListImplements/Components/Component.cs new file mode 100644 index 0000000..e03724d --- /dev/null +++ b/SushiBarListImplements/Components/Component.cs @@ -0,0 +1,41 @@ +using SushiBarContracts.BindingModel; +using SushiBarContracts.ViewModels; +using SushiBarDataModels.Models; + +namespace SushiBarListImplements.Components +{ + public class Component : IComponentModel + { + public int Id { get; private set; } + public string ComponentName { get; private set; } = string.Empty; + public double Cost { get; set; } + public static Component? Create(ComponentBindingModel? model) + { + if (model == null) + { + return null; + } + return new Component() + { + Id = model.Id, + ComponentName = model.ComponentName, + Cost = model.Cost + }; + } + public void Update(ComponentBindingModel? model) + { + if (model == null) + { + return; + } + ComponentName = model.ComponentName; + Cost = model.Cost; + } + public ComponentViewModel GetViewModel => new() + { + Id = Id, + ComponentName = ComponentName, + Cost = Cost + }; + } +} diff --git a/SushiBarListImplements/Components/ComponentStorage.cs b/SushiBarListImplements/Components/ComponentStorage.cs new file mode 100644 index 0000000..9562488 --- /dev/null +++ b/SushiBarListImplements/Components/ComponentStorage.cs @@ -0,0 +1,101 @@ +using SushiBarContracts.BindingModel; +using SushiBarContracts.SearchModel; +using SushiBarContracts.StoragesContracts; +using SushiBarContracts.ViewModels; +using SushiBarListImplement; + +namespace SushiBarListImplements.Components +{ + public class ComponentStorage : IComponentStorage + { + private readonly DataListSingleton _source; + public ComponentStorage() + { + _source = DataListSingleton.GetInstance(); + } + public List GetFullList() + { + var result = new List(); + foreach (var component in _source.Components) + { + result.Add(component.GetViewModel); + } + return result; + } + public List GetFilteredList(ComponentSearchModel model) + { + var result = new List(); + if (string.IsNullOrEmpty(model.ComponentName)) + { + return result; + } + foreach (var component in _source.Components) + { + if (component.ComponentName.Contains(model.ComponentName)) + { + result.Add(component.GetViewModel); + } + } + return result; + } + public ComponentViewModel? GetElement(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue) + { + return null; + } + foreach (var component in _source.Components) + { + if (!string.IsNullOrEmpty(model.ComponentName) && component.ComponentName == model.ComponentName || + model.Id.HasValue && component.Id == model.Id) + { + return component.GetViewModel; + } + } + return null; + } + public ComponentViewModel? Insert(ComponentBindingModel model) + { + model.Id = 1; + foreach (var component in _source.Components) + { + if (model.Id <= component.Id) + { + model.Id = component.Id + 1; + } + } + var newComponent = Component.Create(model); + if (newComponent == null) + { + return null; + } + _source.Components.Add(newComponent); + return newComponent.GetViewModel; + } + public ComponentViewModel? Update(ComponentBindingModel model) + { + foreach (var component in _source.Components) + { + if (component.Id == model.Id) + { + component.Update(model); + return component.GetViewModel; + } + } + return null; + } + public ComponentViewModel? Delete(ComponentBindingModel model) + { + for (int i = 0; i < _source.Components.Count; ++i) + { + if (_source.Components[i].Id == model.Id) + { + var element = _source.Components[i]; + _source.Components.RemoveAt(i); + return element.GetViewModel; + } + } + return null; + } + } +} diff --git a/SushiBarListImplements/DataListSingleton.cs b/SushiBarListImplements/DataListSingleton.cs new file mode 100644 index 0000000..7ec7d73 --- /dev/null +++ b/SushiBarListImplements/DataListSingleton.cs @@ -0,0 +1,29 @@ +using SushiBarListImplements.Components; +using SushiBarListImplements.Orders; +using SushiBarListImplements.Sushis; + +namespace SushiBarListImplement +{ + public class DataListSingleton + { + private static DataListSingleton? _instance; + public List Components { get; set; } + public List Orders { get; set; } + public List Sushis { get; set; } + private DataListSingleton() + { + Components = new List(); + Orders = new List(); + Sushis = new List(); + } + public static DataListSingleton GetInstance() + { + if (_instance == null) + { + _instance = new DataListSingleton(); + } + return _instance; + } + + } +} diff --git a/SushiBarListImplements/Orders/Order.cs b/SushiBarListImplements/Orders/Order.cs new file mode 100644 index 0000000..50617a1 --- /dev/null +++ b/SushiBarListImplements/Orders/Order.cs @@ -0,0 +1,62 @@ +using SushiBarContracts.BindingModel; +using SushiBarContracts.ViewModels; +using SushiBarDataModels.Models; +using SushiBarDataModels; +using SushiBarDataModels.Enums; + +namespace SushiBarListImplements.Orders +{ + public class Order : IOrderModel + { + public int Id { get; private set; } + public int SushiId { get; private set; } + public int Count { get; private set; } + public double Sum { get; private set; } + public OrderStatus Status { get; private set; } + public DateTime DateCreate { get; private set; } + public DateTime? DateImplement { get; private set; } + + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + SushiId = model.SushiId, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement + }; + } + + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + SushiId = model.SushiId; + Count = model.Count; + Sum = model.Sum; + Status = model.Status; + DateCreate = model.DateCreate; + DateImplement = model.DateImplement; + } + + public OrderViewModel GetViewModel => new() + { + Id = Id, + SushiId = SushiId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement + }; + } +} diff --git a/SushiBarListImplements/Orders/OrderStorage.cs b/SushiBarListImplements/Orders/OrderStorage.cs new file mode 100644 index 0000000..2e046a7 --- /dev/null +++ b/SushiBarListImplements/Orders/OrderStorage.cs @@ -0,0 +1,121 @@ +using SushiBarContracts.BindingModel; +using SushiBarContracts.SearchModel; +using SushiBarContracts.StoragesContracts; +using SushiBarContracts.ViewModels; +using SushiBarListImplement; + +namespace SushiBarListImplements.Orders +{ + public class OrderStorage : IOrderStorage + { + private DataListSingleton _source; + + public OrderStorage() + { + _source = DataListSingleton.GetInstance(); + } + + public List GetFullList() + { + var result = new List(); + foreach (var order in _source.Orders) + { + result.Add(AddSushiName(order.GetViewModel)); + } + return result; + } + + public List GetFilteredList(OrderSearchModel model) + { + var result = new List(); + if (model == null || !model.Id.HasValue) + { + return result; + } + foreach (var order in _source.Orders) + { + if (order.Id == model.Id) + { + result.Add(order.GetViewModel); + } + } + return result; + } + + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + foreach (var order in _source.Orders) + { + if (order.Id == model.Id) + { + return AddSushiName(order.GetViewModel); + } + } + return null; + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + model.Id = 1; + foreach (var order in _source.Orders) + { + if (model.Id <= order.Id) + { + model.Id = order.Id + 1; + } + } + + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + _source.Orders.Add(newOrder); + return AddSushiName(newOrder.GetViewModel); + } + + public OrderViewModel? Update(OrderBindingModel model) + { + foreach (var order in _source.Orders) + { + if (order.Id == model.Id) + { + order.Update(model); + return AddSushiName(order.GetViewModel); + } + } + return null; + } + + public OrderViewModel? Delete(OrderBindingModel model) + { + for (int i = 0; i < _source.Orders.Count; ++i) + { + if (_source.Orders[i].Id == model.Id) + { + var element = _source.Orders[i]; + _source.Orders.RemoveAt(i); + return AddSushiName(element.GetViewModel); + } + } + return null; + } + + private OrderViewModel AddSushiName(OrderViewModel model) + { + foreach (var sushi in _source.Sushis) + { + if (sushi.Id == model.SushiId) + { + model.SushiName = sushi.SushiName; + return model; + } + } + return model; + } + } +} diff --git a/SushiBarListImplements/SushiBarListImplements.csproj b/SushiBarListImplements/SushiBarListImplements.csproj new file mode 100644 index 0000000..50a9ff9 --- /dev/null +++ b/SushiBarListImplements/SushiBarListImplements.csproj @@ -0,0 +1,14 @@ + + + + net8.0 + enable + enable + + + + + + + + diff --git a/SushiBarListImplements/Sushis/Sushi.cs b/SushiBarListImplements/Sushis/Sushi.cs new file mode 100644 index 0000000..6c35561 --- /dev/null +++ b/SushiBarListImplements/Sushis/Sushi.cs @@ -0,0 +1,49 @@ +using SushiBarContracts.BindingModel; +using SushiBarContracts.ViewModels; +using SushiBarDataModels.Models; + +namespace SushiBarListImplements.Sushis +{ + public class Sushi : ISushiModel + { + public int Id { get; private set; } + public string SushiName { get; private set; } = string.Empty; + public double Price { get; private set; } + public Dictionary SushiComponents + { + get; + private set; + } = new Dictionary(); + public static Sushi? Create(SushiBindingModel? model) + { + if (model == null) + { + return null; + } + return new Sushi() + { + Id = model.Id, + SushiName = model.SushiName, + Price = model.Price, + SushiComponents = model.SushiComponents + }; + } + public void Update(SushiBindingModel? model) + { + if (model == null) + { + return; + } + SushiName = model.SushiName; + Price = model.Price; + SushiComponents = model.SushiComponents; + } + public SushiViewModel GetViewModel => new() + { + Id = Id, + SushiName = SushiName, + Price = Price, + SushiComponents = SushiComponents + }; + } +} diff --git a/SushiBarListImplements/Sushis/SushiStorage.cs b/SushiBarListImplements/Sushis/SushiStorage.cs new file mode 100644 index 0000000..48ebca2 --- /dev/null +++ b/SushiBarListImplements/Sushis/SushiStorage.cs @@ -0,0 +1,108 @@ +using SushiBarContracts.BindingModel; +using SushiBarContracts.SearchModel; +using SushiBarContracts.StoragesContracts; +using SushiBarContracts.ViewModels; +using SushiBarListImplement; + +namespace SushiBarListImplements.Sushis +{ + public class SushiStorage : ISushiStorage + { + private DataListSingleton _source; + + public SushiStorage() + { + _source = DataListSingleton.GetInstance(); + } + + public List GetFullList() + { + var result = new List(); + foreach (var order in _source.Sushis) + { + result.Add(order.GetViewModel); + } + return result; + } + + public List GetFilteredList(SushiSearchModel model) + { + var result = new List(); + if (string.IsNullOrEmpty(model.SushiName)) + { + return result; + } + foreach (var sushi in _source.Sushis) + { + if (sushi.SushiName.Contains(model.SushiName)) + { + result.Add(sushi.GetViewModel); + } + } + return result; + } + + public SushiViewModel? GetElement(SushiSearchModel model) + { + if (string.IsNullOrEmpty(model.SushiName) && !model.Id.HasValue) + { + return null; + } + foreach (var sushi in _source.Sushis) + { + if (!string.IsNullOrEmpty(model.SushiName) && sushi.SushiName == model.SushiName || + model.Id.HasValue && sushi.Id == model.Id) + { + return sushi.GetViewModel; + } + } + return null; + } + + public SushiViewModel? Insert(SushiBindingModel model) + { + model.Id = 1; + foreach (var sushi in _source.Sushis) + { + if (model.Id <= sushi.Id) + { + model.Id = sushi.Id + 1; + } + } + var newSushi = Sushi.Create(model); + if (newSushi == null) + { + return null; + } + _source.Sushis.Add(newSushi); + return newSushi.GetViewModel; + } + + public SushiViewModel? Update(SushiBindingModel model) + { + foreach (var sushi in _source.Sushis) + { + if (sushi.Id == model.Id) + { + sushi.Update(model); + return sushi.GetViewModel; + } + } + return null; + } + + public SushiViewModel? Delete(SushiBindingModel model) + { + for (int i = 0; i < _source.Sushis.Count; ++i) + { + if (_source.Sushis[i].Id == model.Id) + { + var element = _source.Sushis[i]; + _source.Sushis.RemoveAt(i); + return element.GetViewModel; + } + } + return null; + } + } +}