From 10a579904ae2c3a99a1aab9a8b623ddb13804ddd Mon Sep 17 00:00:00 2001 From: Viltskaa Date: Mon, 30 Jan 2023 23:13:26 +0400 Subject: [PATCH] add listImplenets --- SushiBar/SushiBar.sln | 6 + .../BusinessLogics/OrderLogic.cs | 49 ++++---- .../DataListSingleton.cs | 24 ++++ .../Implements/ComponentStorage.cs | 103 +++++++++++++++++ .../Implements/OrderStorage.cs | 106 ++++++++++++++++++ .../Implements/SushiStorage.cs | 102 +++++++++++++++++ .../SushibarListImplement/Models/Component.cs | 44 ++++++++ .../SushibarListImplement/Models/Order.cs | 62 ++++++++++ .../SushibarListImplement/Models/Sushi.cs | 49 ++++++++ .../SushibarListImplement.csproj | 14 +++ 10 files changed, 537 insertions(+), 22 deletions(-) create mode 100644 SushiBar/SushibarListImplement/DataListSingleton.cs create mode 100644 SushiBar/SushibarListImplement/Implements/ComponentStorage.cs create mode 100644 SushiBar/SushibarListImplement/Implements/OrderStorage.cs create mode 100644 SushiBar/SushibarListImplement/Implements/SushiStorage.cs create mode 100644 SushiBar/SushibarListImplement/Models/Component.cs create mode 100644 SushiBar/SushibarListImplement/Models/Order.cs create mode 100644 SushiBar/SushibarListImplement/Models/Sushi.cs create mode 100644 SushiBar/SushibarListImplement/SushibarListImplement.csproj diff --git a/SushiBar/SushiBar.sln b/SushiBar/SushiBar.sln index f102172..6d47062 100644 --- a/SushiBar/SushiBar.sln +++ b/SushiBar/SushiBar.sln @@ -11,6 +11,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SushiBarContracts", "SushiB EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SushiBarBusinessLogic", "SushiBarBusinessLogic\SushiBarBusinessLogic.csproj", "{A273021E-72E7-4C23-927C-A3E4851BD8D1}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SushibarListImplement", "SushibarListImplement\SushibarListImplement.csproj", "{1BA388CC-72A1-47FE-A1DC-56C6A95D2F09}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -33,6 +35,10 @@ Global {A273021E-72E7-4C23-927C-A3E4851BD8D1}.Debug|Any CPU.Build.0 = Debug|Any CPU {A273021E-72E7-4C23-927C-A3E4851BD8D1}.Release|Any CPU.ActiveCfg = Release|Any CPU {A273021E-72E7-4C23-927C-A3E4851BD8D1}.Release|Any CPU.Build.0 = Release|Any CPU + {1BA388CC-72A1-47FE-A1DC-56C6A95D2F09}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1BA388CC-72A1-47FE-A1DC-56C6A95D2F09}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1BA388CC-72A1-47FE-A1DC-56C6A95D2F09}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1BA388CC-72A1-47FE-A1DC-56C6A95D2F09}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/SushiBar/SushiBarBusinessLogic/BusinessLogics/OrderLogic.cs b/SushiBar/SushiBarBusinessLogic/BusinessLogics/OrderLogic.cs index 71f7ad1..79d8b46 100644 --- a/SushiBar/SushiBarBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/SushiBar/SushiBarBusinessLogic/BusinessLogics/OrderLogic.cs @@ -4,6 +4,7 @@ using SushiBarContracts.BusinessLogicsContracts; using SushiBarContracts.SearchModels; using SushiBarContracts.StoragesContracts; using SushiBarContracts.ViewModels; +using SushiBarDataModels.Enums; namespace SushiBarBusinessLogic.BusinessLogics { @@ -32,27 +33,18 @@ namespace SushiBarBusinessLogic.BusinessLogics public bool DeliveryOrder(OrderBindingModel model) { - CheckModel(model); - OrderViewModel? element = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id }); - if (element == null || model.Status - element?.Status != 1) - { - _logger.LogWarning("Delivery order is not ready"); - return false; - } - element.Status = SushiBarDataModels.Enums.OrderStatus.Ready; - return true; + return UpdateStatus(model, OrderStatus.Issued); } public bool FinishOrder(OrderBindingModel model) { - CheckModel(model); - OrderViewModel? element = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id }); - if (element == null || model.Status - element?.Status != 1) + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_orderStorage.Delete(model) == null) { - _logger.LogWarning("Finish order is not ready"); + _logger.LogWarning("Delete operation failed"); return false; } - element.Status = SushiBarDataModels.Enums.OrderStatus.Issued; return true; } @@ -71,16 +63,29 @@ namespace SushiBarBusinessLogic.BusinessLogics public bool TakeOrderInWork(OrderBindingModel model) { - _logger.LogInformation("ReadList .Id:{Id}", model?.Id); - List list = _orderStorage.GetFullList(); - foreach (OrderViewModel order in list) + return UpdateStatus(model, OrderStatus.Performed); + } + + private bool UpdateStatus(OrderBindingModel model, OrderStatus status) + { + CheckModel(model); + if (model.Status + 1 != status) { - if (order.Status is >= (SushiBarDataModels.Enums.OrderStatus)1 and not (SushiBarDataModels.Enums.OrderStatus)3) - { - return true; - } + _logger.LogWarning("Status update operation failed"); + return false; } - return false; + model.Status = status; + if (model.Status is OrderStatus.Issued) + { + model.DateImplement = DateTime.Now; + } + if (_orderStorage.Update(model) == null) + { + model.Status--; + _logger.LogWarning("Update operation failed"); + return false; + } + return true; } private void CheckModel(OrderBindingModel model, bool withParams = true) diff --git a/SushiBar/SushibarListImplement/DataListSingleton.cs b/SushiBar/SushibarListImplement/DataListSingleton.cs new file mode 100644 index 0000000..2ec96c9 --- /dev/null +++ b/SushiBar/SushibarListImplement/DataListSingleton.cs @@ -0,0 +1,24 @@ +using SushibarListImplement.Models; + +namespace SushibarListImplement +{ + public class DataListSingleton + { + private static DataListSingleton? _instance; + public List Components { get; set; } + public List Orders { get; set; } + public List Sushi { get; set; } + private DataListSingleton() + { + Components = new List(); + Orders = new List(); + Sushi = new List(); + } + public static DataListSingleton GetInstance() + { + _instance ??= new DataListSingleton(); + return _instance; + } + } +} + diff --git a/SushiBar/SushibarListImplement/Implements/ComponentStorage.cs b/SushiBar/SushibarListImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..b7f196c --- /dev/null +++ b/SushiBar/SushibarListImplement/Implements/ComponentStorage.cs @@ -0,0 +1,103 @@ +using SushiBarContracts.BindingModels; +using SushiBarContracts.SearchModels; +using SushiBarContracts.StoragesContracts; +using SushiBarContracts.ViewModels; +using SushibarListImplement.Models; + +namespace SushibarListImplement.Implements +{ + 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; + } + } + Component? 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/SushiBar/SushibarListImplement/Implements/OrderStorage.cs b/SushiBar/SushibarListImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..73fc1c5 --- /dev/null +++ b/SushiBar/SushibarListImplement/Implements/OrderStorage.cs @@ -0,0 +1,106 @@ +using SushiBarContracts.BindingModels; +using SushiBarContracts.SearchModels; +using SushiBarContracts.StoragesContracts; +using SushiBarContracts.ViewModels; +using SushibarListImplement.Models; + +namespace SushibarListImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + private readonly DataListSingleton _source; + + public OrderStorage() + { + _source = DataListSingleton.GetInstance(); + } + public OrderViewModel? Delete(OrderBindingModel model) + { + for (int i = 0; i < _source.Sushi.Count; i++) + { + if (_source.Orders[i].Id == model.Id) + { + Order order = _source.Orders[i]; + _source.Orders.RemoveAt(i); + return order.GetViewModel; + } + } + return null; + } + + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + foreach (Order order in _source.Orders) + { + if (order.Id == model.Id) + { + return order.GetViewModel; + } + } + return null; + } + + public List GetFilteredList(OrderSearchModel model) + { + List list = new(); + if (model.Id == 0) + { + return list; + } + foreach (Order order in _source.Orders) + { + if (order.Id == model.Id) + { + list.Add(order.GetViewModel); + } + } + return list; + } + + public List GetFullList() + { + List list = new(); + foreach (Order order in _source.Orders) + { + list.Add(order.GetViewModel); + } + return list; + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + model.Id = 1; + foreach (Order order in _source.Orders) + { + if (model.Id <= order.Id) + { + model.Id = order.Id; + } + } + Order? newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + _source.Orders.Add(newOrder); + return newOrder.GetViewModel; + } + + public OrderViewModel? Update(OrderBindingModel model) + { + foreach (Order order in _source.Orders) + { + if (order.Id == model.Id) + { + order.Update(model); + return order.GetViewModel; + } + } + return null; + } + } +} diff --git a/SushiBar/SushibarListImplement/Implements/SushiStorage.cs b/SushiBar/SushibarListImplement/Implements/SushiStorage.cs new file mode 100644 index 0000000..5c38d1c --- /dev/null +++ b/SushiBar/SushibarListImplement/Implements/SushiStorage.cs @@ -0,0 +1,102 @@ +using SushiBarContracts.BindingModels; +using SushiBarContracts.SearchModels; +using SushiBarContracts.StoragesContracts; +using SushiBarContracts.ViewModels; +using SushibarListImplement.Models; + +namespace SushibarListImplement.Implements +{ + public class SushiStorage : ISushiStorage + { + private readonly DataListSingleton _source; + public SushiStorage() + { + _source = DataListSingleton.GetInstance(); + } + public List GetFullList() + { + var result = new List(); + foreach (var sushi in _source.Sushi) + { + result.Add(sushi.GetViewModel); + } + return result; + } + public List GetFilteredList(SushiSearchModel model) + { + var result = new List(); + if (string.IsNullOrEmpty(model.SushiName)) + { + return result; + } + foreach (var sushi in _source.Sushi) + { + 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.Sushi) + { + 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.Sushi) + { + if (model.Id <= sushi.Id) + { + model.Id = sushi.Id + 1; + } + } + Sushi? newComponent = Sushi.Create(model); + if (newComponent == null) + { + return null; + } + _source.Sushi.Add(newComponent); + return newComponent.GetViewModel; + } + public SushiViewModel? Update(SushiBindingModel model) + { + foreach (var sushi in _source.Sushi) + { + if (sushi.Id == model.Id) + { + sushi.Update(model); + return sushi.GetViewModel; + } + } + return null; + } + public SushiViewModel? Delete(SushiBindingModel model) + { + for (int i = 0; i < _source.Sushi.Count; i++) + { + if (_source.Sushi[i].Id == model.Id) + { + var element = _source.Sushi[i]; + _source.Sushi.RemoveAt(i); + return element.GetViewModel; + } + } + return null; + } + } +} diff --git a/SushiBar/SushibarListImplement/Models/Component.cs b/SushiBar/SushibarListImplement/Models/Component.cs new file mode 100644 index 0000000..4abd3e5 --- /dev/null +++ b/SushiBar/SushibarListImplement/Models/Component.cs @@ -0,0 +1,44 @@ +using SushiBarContracts.BindingModels; +using SushiBarContracts.ViewModels; +using SushiBarDataModels.Models; + +namespace SushibarListImplement.Models +{ + public class Component : IComponentModel + { + public string ComponentName { get; private set; } = string.Empty; + + public double Cost { get; set; } + + public int Id { get; private 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/SushiBar/SushibarListImplement/Models/Order.cs b/SushiBar/SushibarListImplement/Models/Order.cs new file mode 100644 index 0000000..9666414 --- /dev/null +++ b/SushiBar/SushibarListImplement/Models/Order.cs @@ -0,0 +1,62 @@ +using SushiBarContracts.BindingModels; +using SushiBarContracts.ViewModels; +using SushiBarDataModels.Enums; +using SushiBarDataModels.Models; + +namespace SushibarListImplement.Models +{ + 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; set; } = OrderStatus.Unknown; + public DateTime DateCreate { get; private set; } = DateTime.Now; + public DateTime? DateImplement { get; set; } = null; + + 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; + } + Id = model.Id; + 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/SushiBar/SushibarListImplement/Models/Sushi.cs b/SushiBar/SushibarListImplement/Models/Sushi.cs new file mode 100644 index 0000000..24ce868 --- /dev/null +++ b/SushiBar/SushibarListImplement/Models/Sushi.cs @@ -0,0 +1,49 @@ +using SushiBarContracts.BindingModels; +using SushiBarContracts.ViewModels; +using SushiBarDataModels.Models; + +namespace SushibarListImplement.Models +{ + 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/SushiBar/SushibarListImplement/SushibarListImplement.csproj b/SushiBar/SushibarListImplement/SushibarListImplement.csproj new file mode 100644 index 0000000..c8c675f --- /dev/null +++ b/SushiBar/SushibarListImplement/SushibarListImplement.csproj @@ -0,0 +1,14 @@ + + + + net6.0 + enable + enable + + + + + + + +