diff --git a/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/IceCreamLogic.cs b/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/IceCreamLogic.cs index 36cf92c..1032200 100644 --- a/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/IceCreamLogic.cs +++ b/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/IceCreamLogic.cs @@ -89,7 +89,7 @@ namespace IceCreamBusinessLogic.BusinessLogics return true; } - public void CheckModel(IceCreamBindingModel model, bool withParams = true) + private void CheckModel(IceCreamBindingModel model, bool withParams = true) { if(model == null) { @@ -105,15 +105,14 @@ namespace IceCreamBusinessLogic.BusinessLogics } if (model.Price <= 0) { - throw new ArgumentNullException("Цена изделия должна быть больше 0", nameof(model.Price)); + throw new ArgumentNullException("Цена изделия должна быть больше 0", nameof(model.Price)); } _logger.LogInformation("IceCream. IceCreamName:{IceCreamName}. Price:{Price}. Id: { Id}", model.IceCreamName, model.Price, model.Id); var element = _iceCreamStorage.GetElement(new IceCreamSearchModel { IceCreamName = model.IceCreamName }); if (element != null && element.Id != model.Id) { - throw new InvalidOperationException("Изделие с таким названием уже есть"); + throw new InvalidOperationException("Изделие с таким названием уже есть"); } } - } } diff --git a/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/OrderLogic.cs b/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/OrderLogic.cs index 4ed3644..417ac7f 100644 --- a/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/OrderLogic.cs @@ -13,28 +13,33 @@ using AbstractIceCreamShopDataModels.Enums; namespace IceCreamBusinessLogic.BusinessLogics { - internal class OrderLogic : IOrderLogic + public class OrderLogic : IOrderLogic { + private readonly ILogger _logger; private readonly IOrderStorage _orderStorage; - public OrderLogic(IOrderStorage orderStorage) + public OrderLogic(IOrderStorage orderStorage, ILogger logger) { _orderStorage = orderStorage; + _logger = logger; } - public bool CreateOrder(OrderBindingModel model) { - _orderStorage.Insert(new OrderBindingModel + CheckModel(model); + if(model.Status != OrderStatus.Неизвестен) { - IceCreamId = model.IceCreamId, - Count = model.Count, - Sum = model.Sum, - Status = OrderStatus.Принят, - DateCreate = DateTime.Now, - DateImplement = model.DateImplement - }); - + _logger.LogWarning("Insert operation failed. Order status incorrect."); + return false; + } + model.Status = OrderStatus.Принят; + if (_orderStorage.Insert(model) == null) + { + model.Status = OrderStatus.Неизвестен; + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; } public bool DeliveryOrder(OrderBindingModel model) @@ -56,5 +61,30 @@ namespace IceCreamBusinessLogic.BusinessLogics { throw new NotImplementedException(); } + + private void CheckModel(OrderBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (model.IceCreamId < 0) + { + throw new ArgumentNullException("Некорректный идентификатор изделия", nameof(model.IceCreamId)); + } + if (model.Count <= 0) + { + throw new ArgumentNullException("Количество изделий в заказе должно быть больше 0", nameof(model.Count)); + } + if (model.Sum <= 0) + { + throw new ArgumentNullException("Сумма заказа должна быть больше 0", nameof(model.Sum)); + } + _logger.LogInformation("Order. OrderID:{Id}.Sum:{ Sum}. DocumentId: { DocumentId}", model.Id, model.Sum, model.IceCreamId); + } } } diff --git a/IceCreamShop/IceCreamShop.sln b/IceCreamShop/IceCreamShop.sln index 13cb21e..5222396 100644 --- a/IceCreamShop/IceCreamShop.sln +++ b/IceCreamShop/IceCreamShop.sln @@ -11,6 +11,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IceCreamShopContracts", "Ic EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IceCreamBusinessLogic", "IceCreamBusinessLogic\IceCreamBusinessLogic.csproj", "{E4C64BCD-22DF-43B5-8324-AE64F9237DFF}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IceCreamShopListImplement", "IceCreamShopListImplement\IceCreamShopListImplement.csproj", "{EBB0BAF6-8DEE-42EF-8AF5-571B4E0DE95E}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -33,6 +35,10 @@ Global {E4C64BCD-22DF-43B5-8324-AE64F9237DFF}.Debug|Any CPU.Build.0 = Debug|Any CPU {E4C64BCD-22DF-43B5-8324-AE64F9237DFF}.Release|Any CPU.ActiveCfg = Release|Any CPU {E4C64BCD-22DF-43B5-8324-AE64F9237DFF}.Release|Any CPU.Build.0 = Release|Any CPU + {EBB0BAF6-8DEE-42EF-8AF5-571B4E0DE95E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {EBB0BAF6-8DEE-42EF-8AF5-571B4E0DE95E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {EBB0BAF6-8DEE-42EF-8AF5-571B4E0DE95E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {EBB0BAF6-8DEE-42EF-8AF5-571B4E0DE95E}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/IceCreamShop/IceCreamShop/IceCreamShop.csproj b/IceCreamShop/IceCreamShop/IceCreamShopView.csproj similarity index 100% rename from IceCreamShop/IceCreamShop/IceCreamShop.csproj rename to IceCreamShop/IceCreamShop/IceCreamShopView.csproj diff --git a/IceCreamShop/IceCreamShopListImplement/DataListSingleton.cs b/IceCreamShop/IceCreamShopListImplement/DataListSingleton.cs new file mode 100644 index 0000000..21ea776 --- /dev/null +++ b/IceCreamShop/IceCreamShopListImplement/DataListSingleton.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using IceCreamShopListImplement.Models; + +namespace IceCreamShopListImplement +{ + public class DataListSingleton + { + private static DataListSingleton? _instance; + public List Components { get; set; } + public List Orders { get; set; } + public List iceCreams { get; set; } + private DataListSingleton() + { + Components = new List(); + Orders = new List(); + iceCreams = new List(); + } + public static DataListSingleton GetInstance() + { + if (_instance == null) + { + _instance = new DataListSingleton(); + } + return _instance; + } + } +} diff --git a/IceCreamShop/IceCreamShopListImplement/IceCreamShopListImplement.csproj b/IceCreamShop/IceCreamShopListImplement/IceCreamShopListImplement.csproj new file mode 100644 index 0000000..0689277 --- /dev/null +++ b/IceCreamShop/IceCreamShopListImplement/IceCreamShopListImplement.csproj @@ -0,0 +1,14 @@ + + + + net6.0 + enable + enable + + + + + + + + diff --git a/IceCreamShop/IceCreamShopListImplement/Implements/ComponentStorage.cs b/IceCreamShop/IceCreamShopListImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..925cc44 --- /dev/null +++ b/IceCreamShop/IceCreamShopListImplement/Implements/ComponentStorage.cs @@ -0,0 +1,110 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.SearchModels; +using IceCreamShopContracts.StoragesContracts; +using IceCreamShopContracts.ViewModels; +using IceCreamShopListImplement.Models; + +namespace IceCreamShopListImplement.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; + } + } + 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/IceCreamShop/IceCreamShopListImplement/Implements/IceCreamStorage.cs b/IceCreamShop/IceCreamShopListImplement/Implements/IceCreamStorage.cs new file mode 100644 index 0000000..c37b762 --- /dev/null +++ b/IceCreamShop/IceCreamShopListImplement/Implements/IceCreamStorage.cs @@ -0,0 +1,96 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.SearchModels; +using IceCreamShopContracts.StoragesContracts; +using IceCreamShopContracts.ViewModels; +using IceCreamShopListImplement.Models; + +namespace IceCreamShopListImplement.Implements +{ + public class IceCreamStorage : IIceCreamStorage + { + private readonly DataListSingleton _source; + + public IceCreamStorage() + { + _source = DataListSingleton.GetInstance(); + } + + public IceCreamViewModel? Delete(IceCreamBindingModel model) + { + for (int i = 0; i < _source.iceCreams.Count; ++i) + { + if (_source.iceCreams[i].Id == model.Id) + { + var element = _source.iceCreams[i]; + _source.iceCreams.RemoveAt(i); + return element.GetViewModel; + } + } + return null; + } + + public IceCreamViewModel? GetElement(IceCreamSearchModel model) + { + if (string.IsNullOrEmpty(model.IceCreamName) && !model.Id.HasValue) return null; + foreach (var iceCream in _source.iceCreams) + { + if ((!string.IsNullOrEmpty(model.IceCreamName) && iceCream.IceCreamName == model.IceCreamName) || + (model.Id.HasValue && iceCream.Id == model.Id)) + { + return iceCream.GetViewModel; + } + } + return null; + } + + public List GetFilteredList(IceCreamSearchModel model) + { + var result = new List(); + if (string.IsNullOrEmpty(model.IceCreamName)) return result; + foreach(var iceCream in _source.iceCreams) + { + if (iceCream.IceCreamName.Contains(model.IceCreamName)) + result.Add(iceCream.GetViewModel); + } + return result; + } + + public List GetFullList() + { + var result = new List(); + foreach (var iceCream in _source.iceCreams) result.Add(iceCream.GetViewModel); + return result; + } + + public IceCreamViewModel? Insert(IceCreamBindingModel model) + { + model.Id = 1; + foreach(var iceCream in _source.iceCreams) + { + if(model.Id <= iceCream.Id) model.Id = iceCream.Id + 1; + } + var newIceCream = IceCream.Create(model); + if (newIceCream == null) return null; + _source.iceCreams.Add(newIceCream); + return newIceCream.GetViewModel; + } + + public IceCreamViewModel? Update(IceCreamBindingModel model) + { + foreach (var iceCream in _source.iceCreams) + { + if (iceCream.Id == model.Id) + { + iceCream.Update(model); + return iceCream.GetViewModel; + } + } + return null; + } + } +} diff --git a/IceCreamShop/IceCreamShopListImplement/Implements/OrderStorage.cs b/IceCreamShop/IceCreamShopListImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..f452688 --- /dev/null +++ b/IceCreamShop/IceCreamShopListImplement/Implements/OrderStorage.cs @@ -0,0 +1,103 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.SearchModels; +using IceCreamShopContracts.StoragesContracts; +using IceCreamShopContracts.ViewModels; +using IceCreamShopListImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IceCreamShopListImplement.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.Orders.Count; ++i) + { + if (_source.Orders[i].Id == model.Id) + { + var element = _source.Orders[i]; + _source.Orders.RemoveAt(i); + return element.GetViewModel; + } + } + return null; + } + + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) return null; + foreach (var order in _source.Orders) + { + if (order.Id == model.Id) + return order.GetViewModel; + } + return null; + } + + public List GetFilteredList(OrderSearchModel model) + { + var result = new List(); + if (!model.Id.HasValue) return result; + + foreach (var order in _source.Orders) + { + if (order.Id == model.Id) + result.Add(order.GetViewModel); + } + return result; + } + + public List GetFullList() + { + var result = new List(); + foreach (var order in _source.Orders) + { + result.Add(order.GetViewModel); + } + return result; + } + + 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 newOrder.GetViewModel; + + } + + public OrderViewModel? Update(OrderBindingModel model) + { + foreach (var order in _source.Orders) + { + if (order.Id == model.Id) + { + order.Update(model); + return order.GetViewModel; + } + } + return null; + } + } +} diff --git a/IceCreamShop/IceCreamShopListImplement/Models/Component.cs b/IceCreamShop/IceCreamShopListImplement/Models/Component.cs new file mode 100644 index 0000000..b2b116f --- /dev/null +++ b/IceCreamShop/IceCreamShopListImplement/Models/Component.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.ViewModels; +using AbstractIceCreamShopDataModels.Models; + +namespace IceCreamShopListImplement.Models +{ + 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/IceCreamShop/IceCreamShopListImplement/Models/IceCream.cs b/IceCreamShop/IceCreamShopListImplement/Models/IceCream.cs new file mode 100644 index 0000000..b595c46 --- /dev/null +++ b/IceCreamShop/IceCreamShopListImplement/Models/IceCream.cs @@ -0,0 +1,55 @@ +using AbstractIceCreamShopDataModels.Models; +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + + +namespace IceCreamShopListImplement.Models +{ + public class IceCream : IIceCreamModel + { + public int Id { get; private set; } + public string IceCreamName { get; private set; } = string.Empty; + public double Price { get; private set; } + public Dictionary IceCreamComponents + { + get; + private set; + } = new Dictionary(); + public static IceCream? Create(IceCreamBindingModel? model) + { + if (model == null) + { + return null; + } + return new IceCream() + { + Id = model.Id, + IceCreamName = model.IceCreamName, + Price = model.Price, + IceCreamComponents = model.IceCreamComponents + }; + } + public void Update(IceCreamBindingModel? model) + { + if (model == null) + { + return; + } + IceCreamName = model.IceCreamName; + Price = model.Price; + IceCreamComponents = model.IceCreamComponents; + } + public IceCreamViewModel GetViewModel => new() + { + Id = Id, + IceCreamName = IceCreamName, + Price = Price, + IceCreamComponents = IceCreamComponents + }; + } +} diff --git a/IceCreamShop/IceCreamShopListImplement/Models/Order.cs b/IceCreamShop/IceCreamShopListImplement/Models/Order.cs new file mode 100644 index 0000000..9a011e7 --- /dev/null +++ b/IceCreamShop/IceCreamShopListImplement/Models/Order.cs @@ -0,0 +1,68 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.ViewModels; +using AbstractIceCreamShopDataModels.Models; +using AbstractIceCreamShopDataModels.Enums; +using System.Data; + +namespace IceCreamShopListImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; private set; } + + public int IceCreamId { get; private set; } + + public int Count { get; private set; } + + public double Sum { get; private set; } + + public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен; + + public DateTime DateCreate { get; private set; } = DateTime.Now; + + public DateTime? DateImplement { get; private set; } + + public static Order? Create (OrderBindingModel? model) + { + if (model == null) return null; + return new Order + { + Id = model.Id, + IceCreamId = model.IceCreamId, + 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; + IceCreamId = model.IceCreamId; + Count = model.Count; + Sum = model.Sum; + Status = model.Status; + DateCreate = model.DateCreate; + DateImplement = model.DateImplement; + } + + public OrderViewModel GetViewModel => new() + { + Id = Id, + IceCreamId = IceCreamId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement + }; + } +}