diff --git a/IceCreamShop/AbstractIceCreamShopDataModels/AbstractIceCreamShopDataModels.sln b/IceCreamShop/AbstractIceCreamShopDataModels/AbstractIceCreamShopDataModels.sln deleted file mode 100644 index 7681ee8..0000000 --- a/IceCreamShop/AbstractIceCreamShopDataModels/AbstractIceCreamShopDataModels.sln +++ /dev/null @@ -1,25 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 17 -VisualStudioVersion = 17.3.32922.545 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AbstractIceCreamShopDataModels", "AbstractIceCreamShopDataModels\AbstractIceCreamShopDataModels.csproj", "{28830087-9D9F-4E66-84CC-94C5CF376BF1}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Release|Any CPU = Release|Any CPU - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {28830087-9D9F-4E66-84CC-94C5CF376BF1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {28830087-9D9F-4E66-84CC-94C5CF376BF1}.Debug|Any CPU.Build.0 = Debug|Any CPU - {28830087-9D9F-4E66-84CC-94C5CF376BF1}.Release|Any CPU.ActiveCfg = Release|Any CPU - {28830087-9D9F-4E66-84CC-94C5CF376BF1}.Release|Any CPU.Build.0 = Release|Any CPU - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {A7BDC253-D956-404D-9013-AD0FE1F32DD1} - EndGlobalSection -EndGlobal diff --git a/IceCreamShop/AbstractIceCreamShopDataModels/AbstractIceCreamShopDataModels/AbstractIceCreamShopDataModels.csproj b/IceCreamShop/AbstractIceCreamShopDataModels/AbstractIceCreamShopDataModels/AbstractIceCreamShopDataModels.csproj deleted file mode 100644 index 132c02c..0000000 --- a/IceCreamShop/AbstractIceCreamShopDataModels/AbstractIceCreamShopDataModels/AbstractIceCreamShopDataModels.csproj +++ /dev/null @@ -1,9 +0,0 @@ - - - - net6.0 - enable - enable - - - diff --git a/IceCreamShop/AbstractIceCreamShopDataModels/AbstractIceCreamShopDataModels/IComponentModel.cs b/IceCreamShop/AbstractIceCreamShopDataModels/AbstractIceCreamShopDataModels/IComponentModel.cs deleted file mode 100644 index 12729fc..0000000 --- a/IceCreamShop/AbstractIceCreamShopDataModels/AbstractIceCreamShopDataModels/IComponentModel.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace AbstractIceCreamShopDataModels.Models -{ - public interface IComponentModel : IId - { - string ComponentName { get; } - double Cost { get; } - } -} diff --git a/IceCreamShop/AbstractIceCreamShopDataModels/AbstractIceCreamShopDataModels/IId.cs b/IceCreamShop/AbstractIceCreamShopDataModels/AbstractIceCreamShopDataModels/IId.cs deleted file mode 100644 index eb6aca9..0000000 --- a/IceCreamShop/AbstractIceCreamShopDataModels/AbstractIceCreamShopDataModels/IId.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace AbstractIceCreamShopDataModels -{ - public interface IId - { - int Id { get; } - } -} diff --git a/IceCreamShop/AbstractIceCreamShopDataModels/AbstractIceCreamShopDataModels/IOrderModel.cs b/IceCreamShop/AbstractIceCreamShopDataModels/AbstractIceCreamShopDataModels/IOrderModel.cs deleted file mode 100644 index f41ac85..0000000 --- a/IceCreamShop/AbstractIceCreamShopDataModels/AbstractIceCreamShopDataModels/IOrderModel.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - - -namespace AbstractIceCreamShopDataModels.Models -{ - public interface IOrderModel : IId - { - int ProductId { get; } - int Count { get; } - double Sum { get; } - OrderStatus Status { get; } - DateTime DateCreate { get; } - DateTime? DateImplement { get; } - } -} diff --git a/IceCreamShop/AbstractIceCreamShopDataModels/AbstractIceCreamShopDataModels/IProductModel.cs b/IceCreamShop/AbstractIceCreamShopDataModels/AbstractIceCreamShopDataModels/IProductModel.cs deleted file mode 100644 index 19cde0e..0000000 --- a/IceCreamShop/AbstractIceCreamShopDataModels/AbstractIceCreamShopDataModels/IProductModel.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace AbstractIceCreamShopDataModels.Models -{ - public interface IProductModel : IId - { - string ProductName { get; } - double Price { get; } - Dictionary ProductComponents { get; } - } -} diff --git a/IceCreamShop/AbstractIceCreamShopDataModels/AbstractIceCreamShopDataModels/OrderStatus.cs b/IceCreamShop/AbstractIceCreamShopDataModels/AbstractIceCreamShopDataModels/OrderStatus.cs deleted file mode 100644 index fb30825..0000000 --- a/IceCreamShop/AbstractIceCreamShopDataModels/AbstractIceCreamShopDataModels/OrderStatus.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace AbstractIceCreamShopDataModels -{ - public enum OrderStatus - { - Неизвестен = -1, - Принят = 0, - Выполняется = 1, - Готов = 2, - Выдан = 3 - } -} \ No newline at end of file diff --git a/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/ComponentLogic.cs b/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/ComponentLogic.cs index abfb55d..b71e2d2 100644 --- a/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/ComponentLogic.cs +++ b/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/ComponentLogic.cs @@ -11,7 +11,7 @@ using IceCreamShopContracts.StoragesContracts; using IceCreamShopContracts.ViewModels; -namespace IceCreamShopBusinessLogic.BusinessLogics +namespace IceCreamBusinessLogic.BusinessLogics { public class ComponentLogic : IComponentLogic { @@ -101,7 +101,7 @@ namespace IceCreamShopBusinessLogic.BusinessLogics 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 }); + var element = _componentStorage.GetElement(new ComponentSearchModel { ComponentName = model.ComponentName }); if (element != null && element.Id != model.Id) { throw new InvalidOperationException("Компонент с таким названием уже есть"); diff --git a/IceCreamShop/IceCreamShop.sln b/IceCreamShop/IceCreamShop.sln index b6c01d0..d53647f 100644 --- a/IceCreamShop/IceCreamShop.sln +++ b/IceCreamShop/IceCreamShop.sln @@ -11,7 +11,9 @@ 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}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IceCreamShopListImplement", "IceCreamShopListImplement\IceCreamShopListImplement.csproj", "{EBB0BAF6-8DEE-42EF-8AF5-571B4E0DE95E}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IceCreamShopFileImplement", "IceCreamShopFileImplement\IceCreamShopFileImplement.csproj", "{382E618C-D833-4E98-9786-7A831953ABD0}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -39,6 +41,10 @@ Global {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 + {382E618C-D833-4E98-9786-7A831953ABD0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {382E618C-D833-4E98-9786-7A831953ABD0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {382E618C-D833-4E98-9786-7A831953ABD0}.Release|Any CPU.ActiveCfg = Release|Any CPU + {382E618C-D833-4E98-9786-7A831953ABD0}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/IceCreamShop/IceCreamShop/IceCreamShopView.csproj b/IceCreamShop/IceCreamShop/IceCreamShopView.csproj index 6a7e820..21ad05e 100644 --- a/IceCreamShop/IceCreamShop/IceCreamShopView.csproj +++ b/IceCreamShop/IceCreamShop/IceCreamShopView.csproj @@ -16,6 +16,7 @@ + diff --git a/IceCreamShop/IceCreamShop/Program.cs b/IceCreamShop/IceCreamShop/Program.cs index 24dcf8a..9e9dcb1 100644 --- a/IceCreamShop/IceCreamShop/Program.cs +++ b/IceCreamShop/IceCreamShop/Program.cs @@ -1,12 +1,11 @@ using IceCreamShopContracts.BusinessLogicsContracts; using IceCreamShopContracts.StoragesContracts; -using IceCreamShopListImplement.Implements; using IceCreamBusinessLogic.BusinessLogics; using IceCreamShopView; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; -using IceCreamShopBusinessLogic.BusinessLogics; +using IceCreamShopFileImplement.Implements; namespace IceCreamShop { diff --git a/IceCreamShop/IceCreamShopBusinessLogic/BusinessLogics/ComponentLogic.cs b/IceCreamShop/IceCreamShopBusinessLogic/BusinessLogics/ComponentLogic.cs deleted file mode 100644 index d6b3897..0000000 --- a/IceCreamShop/IceCreamShopBusinessLogic/BusinessLogics/ComponentLogic.cs +++ /dev/null @@ -1,122 +0,0 @@ -using AbstractShopContracts.BindingModels; -using AbstractShopContracts.BusinessLogicsContracts; -using AbstractShopContracts.SearchModels; -using AbstractShopContracts.StoragesContracts; -using AbstractShopContracts.ViewModels; -using Microsoft.Extensions.Logging; - -using IceCreamShopContracts.BindingModels; - -namespace IceCreamShopBusinessLogic.BusinessLogics -{ - 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/IceCreamShop/IceCreamShopFileImplement/DataFileSingleton.cs b/IceCreamShop/IceCreamShopFileImplement/DataFileSingleton.cs new file mode 100644 index 0000000..8be6b0f --- /dev/null +++ b/IceCreamShop/IceCreamShopFileImplement/DataFileSingleton.cs @@ -0,0 +1,54 @@ +using IceCreamShopFileImplement.Models; +using System.Xml.Linq; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IceCreamShopFileImplement +{ + public class DataFileSingleton + { + private static DataFileSingleton? instance; + private readonly string ComponentFileName = "Component.xml"; + private readonly string OrderFileName = "Order.xml"; + private readonly string IceCreamFileName = "IceCream.xml"; + public List Components { get; private set; } + public List Orders { get; private set; } + public List IceCreams { get; private set; } + public static DataFileSingleton GetInstance() + { + if (instance == null) + { + instance = new DataFileSingleton(); + } + return instance; + } + public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement); + public void SaveIceCreams() => SaveData(IceCreams, IceCreamFileName, "IceCreams", x => x.GetXElement); + public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); + private DataFileSingleton() + { + Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; + IceCreams = LoadData(IceCreamFileName, "IceCream", x => IceCream.Create(x)!)!; + Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; + } + private static List? LoadData(string filename, string xmlNodeName, Func selectFunction) + { + if (File.Exists(filename)) + { + return + XDocument.Load(filename)?.Root?.Elements(xmlNodeName)?.Select(selectFunction)?.ToList(); + } + return new List(); + } + private static void SaveData(List data, string filename, string xmlNodeName, Func selectFunction) + { + if (data != null) + { + new XDocument(new XElement(xmlNodeName, data.Select(selectFunction).ToArray())).Save(filename); + } + } + } +} diff --git a/IceCreamShop/IceCreamShopBusinessLogic/1.csproj b/IceCreamShop/IceCreamShopFileImplement/IceCreamShopFileImplement.csproj similarity index 78% rename from IceCreamShop/IceCreamShopBusinessLogic/1.csproj rename to IceCreamShop/IceCreamShopFileImplement/IceCreamShopFileImplement.csproj index 2243cc7..0689277 100644 --- a/IceCreamShop/IceCreamShopBusinessLogic/1.csproj +++ b/IceCreamShop/IceCreamShopFileImplement/IceCreamShopFileImplement.csproj @@ -1,7 +1,6 @@ - Exe net6.0 enable enable @@ -9,6 +8,7 @@ + diff --git a/IceCreamShop/IceCreamShopFileImplement/Implements/ComponentStorage.cs b/IceCreamShop/IceCreamShopFileImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..cd34d47 --- /dev/null +++ b/IceCreamShop/IceCreamShopFileImplement/Implements/ComponentStorage.cs @@ -0,0 +1,78 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.SearchModels; +using IceCreamShopContracts.StoragesContracts; +using IceCreamShopContracts.ViewModels; +using IceCreamShopFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IceCreamShopFileImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + private readonly DataFileSingleton source; + public ComponentStorage() + { + source = DataFileSingleton.GetInstance(); + } + public List GetFullList() + { + return source.Components.Select(x => x.GetViewModel).ToList(); + } + public List GetFilteredList(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + return source.Components.Where(x => x.ComponentName.Contains(model.ComponentName)).Select(x => x.GetViewModel).ToList(); + } + public ComponentViewModel? GetElement(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue) + { + return null; + } + return source.Components.FirstOrDefault(x => + (!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName ==model.ComponentName) || + (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + public ComponentViewModel? Insert(ComponentBindingModel model) + { + model.Id = source.Components.Count > 0 ? source.Components.Max(x => x.Id) + 1 : 1; + var newComponent = Component.Create(model); + if (newComponent == null) + { + return null; + } + source.Components.Add(newComponent); + source.SaveComponents(); + return newComponent.GetViewModel; + } + public ComponentViewModel? Update(ComponentBindingModel model) + { + var component = source.Components.FirstOrDefault(x => x.Id == model.Id); + if (component == null) + { + return null; + } + component.Update(model); + source.SaveComponents(); + return component.GetViewModel; + } + public ComponentViewModel? Delete(ComponentBindingModel model) + { + var element = source.Components.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + source.Components.Remove(element); + source.SaveComponents(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/IceCreamShop/IceCreamShopFileImplement/Implements/IceCreamStorage.cs b/IceCreamShop/IceCreamShopFileImplement/Implements/IceCreamStorage.cs new file mode 100644 index 0000000..c09e963 --- /dev/null +++ b/IceCreamShop/IceCreamShopFileImplement/Implements/IceCreamStorage.cs @@ -0,0 +1,84 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.SearchModels; +using IceCreamShopContracts.StoragesContracts; +using IceCreamShopContracts.ViewModels; +using IceCreamShopFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Diagnostics.SymbolStore; +using System.Linq; +using System.Reflection.Metadata; +using System.Text; +using System.Threading.Tasks; + +namespace IceCreamShopFileImplement.Implements +{ + public class IceCreamStorage : IIceCreamStorage + { + private readonly DataFileSingleton source; + + public IceCreamStorage() + { + source = DataFileSingleton.GetInstance(); + } + + public IceCreamViewModel? GetElement(IceCreamSearchModel model) + { + if (string.IsNullOrEmpty(model.IceCreamName) && !model.Id.HasValue) + { + return null; + } + return source.IceCreams.FirstOrDefault(x => (!string.IsNullOrEmpty(model.IceCreamName) && x.IceCreamName == model.IceCreamName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public List GetFilteredList(IceCreamSearchModel model) + { + if (string.IsNullOrEmpty(model.IceCreamName)) + { + return new(); + } + return source.IceCreams.Where(x => x.IceCreamName.Contains(model.IceCreamName)).Select(x => x.GetViewModel).ToList(); + } + + public List GetFullList() + { + return source.IceCreams.Select(x => x.GetViewModel).ToList(); + } + + public IceCreamViewModel? Insert(IceCreamBindingModel model) + { + model.Id = source.IceCreams.Count > 0 ? source.IceCreams.Max(x => x.Id) + 1 : 1; + var newDoc = IceCream.Create(model); + if (newDoc == null) + { + return null; + } + source.IceCreams.Add(newDoc); + source.SaveIceCreams(); + return newDoc.GetViewModel; + } + + public IceCreamViewModel? Update(IceCreamBindingModel model) + { + var document = source.IceCreams.FirstOrDefault(x => x.Id == model.Id); + if (document == null) + { + return null; + } + document.Update(model); + source.SaveIceCreams(); + return document.GetViewModel; + } + public IceCreamViewModel? Delete(IceCreamBindingModel model) + { + var document = source.IceCreams.FirstOrDefault(x => x.Id == model.Id); + if (document == null) + { + return null; + } + document.Update(model); + source.SaveIceCreams(); + return document.GetViewModel; + } + } +} diff --git a/IceCreamShop/IceCreamShopFileImplement/Implements/OrderStorage.cs b/IceCreamShop/IceCreamShopFileImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..e1cf694 --- /dev/null +++ b/IceCreamShop/IceCreamShopFileImplement/Implements/OrderStorage.cs @@ -0,0 +1,86 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.SearchModels; +using IceCreamShopContracts.StoragesContracts; +using IceCreamShopContracts.ViewModels; +using IceCreamShopFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IceCreamShopFileImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + private readonly DataFileSingleton source; + + public OrderStorage() + { + source = DataFileSingleton.GetInstance(); + } + + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + return source.Orders + .FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + return source.Orders + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + return source.Orders.Select(x => x.GetViewModel).ToList(); + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + model.Id = source.Orders.Count > 0 ? source.Orders.Max(x => x.Id) + 1 : 1; + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + source.Orders.Add(newOrder); + source.SaveOrders(); + return newOrder.GetViewModel; + } + + public OrderViewModel? Update(OrderBindingModel model) + { + var order = source.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order == null) + { + return null; + } + order.Update(model); + source.SaveOrders(); + return order.GetViewModel; + } + public OrderViewModel? Delete(OrderBindingModel model) + { + var order = source.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order == null) + { + return null; + } + order.Update(model); + source.SaveOrders(); + return order.GetViewModel; + } + } +} diff --git a/IceCreamShop/IceCreamShopFileImplement/Models/Component.cs b/IceCreamShop/IceCreamShopFileImplement/Models/Component.cs new file mode 100644 index 0000000..01bd975 --- /dev/null +++ b/IceCreamShop/IceCreamShopFileImplement/Models/Component.cs @@ -0,0 +1,64 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.ViewModels; +using AbstractIceCreamShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace IceCreamShopFileImplement.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 static Component? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Component() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + ComponentName = element.Element("ComponentName")!.Value, + Cost = Convert.ToDouble(element.Element("Cost")!.Value) + }; + } + 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 + }; + public XElement GetXElement => new("Component", + new XAttribute("Id", Id), + new XElement("ComponentName", ComponentName), + new XElement("Cost", Cost.ToString())); + } +} diff --git a/IceCreamShop/IceCreamShopFileImplement/Models/IceCream.cs b/IceCreamShop/IceCreamShopFileImplement/Models/IceCream.cs new file mode 100644 index 0000000..e72613b --- /dev/null +++ b/IceCreamShop/IceCreamShopFileImplement/Models/IceCream.cs @@ -0,0 +1,88 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.ViewModels; +using AbstractIceCreamShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace IceCreamShopFileImplement.Models +{ + public class IceCream + { + public int Id { get; private set; } + public string IceCreamName { get; private set; } = string.Empty; + public double Price { get; private set; } + public Dictionary Components { get; private set; } = new(); + private Dictionary? _iceCreamComponents = null; + public Dictionary IceCreamComponents + { + get + { + if (_iceCreamComponents == null) + { + var source = DataFileSingleton.GetInstance(); + _iceCreamComponents = Components.ToDictionary(x => x.Key, y => + ((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!, y.Value)); + } + return _iceCreamComponents; + } + } + public static IceCream? Create(IceCreamBindingModel model) + { + if (model == null) + { + return null; + } + return new IceCream() + { + Id = model.Id, + IceCreamName = model.IceCreamName, + Price = model.Price, + Components = model.IceCreamComponents.ToDictionary(x => x.Key, x => x.Value.Item2) + }; + } + public static IceCream? Create(XElement element) + { + if (element == null) + { + return null; + } + return new IceCream() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + IceCreamName = element.Element("IceCreamName")!.Value, + Price = Convert.ToDouble(element.Element("Price")!.Value), + Components = element.Element("IceCreamComponents")!.Elements("IceCreamComponent") + .ToDictionary(x => Convert.ToInt32(x.Element("Key")?.Value), x => + Convert.ToInt32(x.Element("Value")?.Value)) + }; + } + public void Update(IceCreamBindingModel model) + { + if (model == null) + { + return; + } + IceCreamName = model.IceCreamName; + Price = model.Price; + Components = model.IceCreamComponents.ToDictionary(x => x.Key, x => x.Value.Item2); + _iceCreamComponents = null; + } + public IceCreamViewModel GetViewModel => new() + { + Id = Id, + IceCreamName = IceCreamName, + Price = Price, + IceCreamComponents = IceCreamComponents + }; + public XElement GetXElement => new("IceCream", + new XAttribute("Id", Id), + new XElement("IceCreamName", IceCreamName), + new XElement("Price", Price.ToString()), + new XElement("IceCreamComponents", Components.Select(x => + new XElement("IceCreamComponents", new XElement("Key", x.Key), new XElement("Value", x.Value))).ToArray())); + } +} diff --git a/IceCreamShop/IceCreamShopFileImplement/Models/Order.cs b/IceCreamShop/IceCreamShopFileImplement/Models/Order.cs new file mode 100644 index 0000000..c70e982 --- /dev/null +++ b/IceCreamShop/IceCreamShopFileImplement/Models/Order.cs @@ -0,0 +1,104 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.ViewModels; +using AbstractIceCreamShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using AbstractIceCreamShopDataModels.Enums; +using System.Xml.Linq; +using System.Reflection.Metadata; + +namespace IceCreamShopFileImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; private set; } + public int IceCreamId { get; private set; } + public string IceCreamName { get; private set; } = string.Empty; + + 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, + IceCreamName = model.IceCreamName, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement + }; + } + public static Order? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Order() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + IceCreamId = Convert.ToInt32(element.Element("IceCreamId")!.Value), + IceCreamName = element.Element("IceCreamName")!.Value, + Sum = Convert.ToDouble(element.Element("Sum")!.Value), + Count = Convert.ToInt32(element.Element("Count")!.Value), + Status = (OrderStatus)Enum.Parse(typeof(OrderStatus), element.Element("Status")!.Value), + DateCreate = Convert.ToDateTime(element.Element("DateCreate")!.Value), + DateImplement = string.IsNullOrEmpty(element.Element("DateImplement")!.Value) ? null : Convert.ToDateTime(element.Element("DateImplement")!.Value) + }; + } + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + Id = model.Id; + IceCreamId = model.IceCreamId; + IceCreamName = model.IceCreamName; + Count = model.Count; + Sum = model.Sum; + Status = model.Status; + DateCreate = model.DateCreate; + DateImplement = model.DateImplement; + } + public OrderViewModel GetViewModel => new() + { + Id = Id, + IceCreamId = IceCreamId, + IceCreamName = IceCreamName, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement, + }; + public XElement GetXElement => new( + "Order", + new XAttribute("Id", Id), + new XElement("IceCreamId", IceCreamId.ToString()), + new XElement("IceCreamName", IceCreamName), + new XElement("Count", Count.ToString()), + new XElement("Sum", Sum.ToString()), + new XElement("Status", Status.ToString()), + new XElement("DateCreate", DateCreate.ToString()), + new XElement("DateImplement", DateImplement.ToString()) + ); + } +}