From a83bd28099cc9837adf46edaf0f879a19b09f81d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Wed, 15 Feb 2023 04:07:03 +0400 Subject: [PATCH] fix --- ConfectionaryFileImplement/Component.cs | 61 ++++++++++++- .../ConfectioneryFileImplement.csproj | 5 + ConfectionaryFileImplement/Order.cs | 23 ++++- ConfectionaryFileImplement/OrderStorage.cs | 76 ++++++++++++++-- ConfectionaryFileImplement/Pastry.cs | 91 +++++++++++++++++-- ConfectionaryFileImplement/PastryStorage.cs | 18 ++-- 6 files changed, 247 insertions(+), 27 deletions(-) diff --git a/ConfectionaryFileImplement/Component.cs b/ConfectionaryFileImplement/Component.cs index f24745d..91da900 100644 --- a/ConfectionaryFileImplement/Component.cs +++ b/ConfectionaryFileImplement/Component.cs @@ -1,7 +1,60 @@ -namespace ConfectionaryFileImplement -{ - public class Class1 - { +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels.Models; +using System.Xml.Linq; + +namespace ConfectioneryFileImplement.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())); } } \ No newline at end of file diff --git a/ConfectionaryFileImplement/ConfectioneryFileImplement.csproj b/ConfectionaryFileImplement/ConfectioneryFileImplement.csproj index 132c02c..41b6c3e 100644 --- a/ConfectionaryFileImplement/ConfectioneryFileImplement.csproj +++ b/ConfectionaryFileImplement/ConfectioneryFileImplement.csproj @@ -6,4 +6,9 @@ enable + + + + + diff --git a/ConfectionaryFileImplement/Order.cs b/ConfectionaryFileImplement/Order.cs index 66eed4f..d3067f1 100644 --- a/ConfectionaryFileImplement/Order.cs +++ b/ConfectionaryFileImplement/Order.cs @@ -4,7 +4,7 @@ using ConfectioneryDataModels.Enums; using ConfectioneryDataModels.Models; using System.Xml.Linq; -namespace ConfectioneryFileImplement +namespace ConfectioneryFileImplement.Models { public class Order : IOrderModel { @@ -39,6 +39,25 @@ namespace ConfectioneryFileImplement Id = model.Id, }; } + public static Order? Create(XElement element) + { + if (element == null) + { + return null; + } + var dateImplement = element.Element("DateImplement")!.Value; + return new() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + Sum = Convert.ToDouble(element.Element("Sum")!.Value), + Count = Convert.ToInt32(element.Element("Count")!.Value), + Status = (OrderStatus)Convert.ToInt32(element.Element("Status")!.Value), + PastryId = Convert.ToInt32(element.Element("PastryId")!.Value), + DateCreate = Convert.ToDateTime(element.Element("DateCreate")!.Value), + DateImplement = string.IsNullOrEmpty(dateImplement) ? null : Convert.ToDateTime(dateImplement), + }; + } + public void Update(OrderBindingModel? model) { if (model == null) @@ -68,7 +87,7 @@ namespace ConfectioneryFileImplement new XElement("PastryId", PastryId), new XElement("Count", Count), new XElement("Sum", Sum.ToString()), - new XElement("Status", Status), + new XElement("Status", (int)Status), new XElement("DateCreate", DateCreate), new XElement("DateImplement", DateImplement) ); diff --git a/ConfectionaryFileImplement/OrderStorage.cs b/ConfectionaryFileImplement/OrderStorage.cs index 5b79348..aae2076 100644 --- a/ConfectionaryFileImplement/OrderStorage.cs +++ b/ConfectionaryFileImplement/OrderStorage.cs @@ -1,12 +1,76 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContract; +using ConfectioneryContracts.ViewModels; +using ConfectioneryFileImplement.Models; namespace ConfectioneryFileImplement { - internal class OrderStorage + public class OrderStorage : IOrderStorage { + private readonly DataFileSingleton _source; + public OrderStorage() + { + _source = DataFileSingleton.GetInstance(); + } + + public OrderViewModel? Delete(OrderBindingModel model) + { + var element = _source.Orders.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + _source.Orders.Remove(element); + _source.SaveOrders(); + return element.GetViewModel; + } + return null; + } + + 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) + { + var result = GetElement(model); + return result != null ? new() { result } : new(); + } + + 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 pastry = _source.Orders.FirstOrDefault(x => x.Id == model.Id); + if (pastry == null) + { + return null; + } + pastry.Update(model); + _source.SaveOrders(); + return pastry.GetViewModel; + } } } diff --git a/ConfectionaryFileImplement/Pastry.cs b/ConfectionaryFileImplement/Pastry.cs index 7baf7e7..f32067c 100644 --- a/ConfectionaryFileImplement/Pastry.cs +++ b/ConfectionaryFileImplement/Pastry.cs @@ -1,12 +1,87 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels.Models; +using ConfectioneryDataModels.Models; +using System.Xml.Linq; -namespace ConfectionaryFileImplement +namespace ConfectioneryFileImplement.Models { - internal class Pastry + public class Pastry : IPastryModel { + public int Id { get; private set; } + public string PastryName { get; private set; } = string.Empty; + public double Price { get; private set; } + public Dictionary Components { get; private set; } = new(); + private Dictionary? _PastryComponents = null; + public Dictionary PastryComponents + { + get + { + if (_PastryComponents == null) + { + var source = DataFileSingleton.GetInstance(); + _PastryComponents = Components.ToDictionary(x => x.Key, y => + ((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!, + y.Value)); + } + return _PastryComponents; + } + } + public static Pastry? Create(PastryBindingModel model) + { + if (model == null) + { + return null; + } + return new Pastry() + { + Id = model.Id, + PastryName = model.PastryName, + Price = model.Price, + Components = model.PastryComponents.ToDictionary(x => x.Key, x => x.Value.Item2) + }; + } + public static Pastry? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Pastry() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + PastryName = element.Element("PastryName")!.Value, + Price = Convert.ToDouble(element.Element("Price")!.Value), + Components = element.Element("PastryComponents")!.Elements("PastryComponent").ToDictionary(x => + Convert.ToInt32(x.Element("Key")?.Value), x => + Convert.ToInt32(x.Element("Value")?.Value)) + }; + } + public void Update(PastryBindingModel model) + { + if (model == null) + { + return; + } + PastryName = model.PastryName; + Price = model.Price; + Components = model.PastryComponents.ToDictionary(x => x.Key, x => x.Value.Item2); + _PastryComponents = null; + } + public PastryViewModel GetViewModel => new() + { + Id = Id, + PastryName = PastryName, + Price = Price, + PastryComponents = PastryComponents + }; + public XElement GetXElement => new("Pastry", + new XAttribute("Id", Id), + new XElement("PastryName", PastryName), + new XElement("Price", Price.ToString()), + new XElement("PastryComponents", Components.Select(x => + new XElement("PastryComponent", + new XElement("Key", x.Key), + new XElement("Value", x.Value))).ToArray())); } -} +} \ No newline at end of file diff --git a/ConfectionaryFileImplement/PastryStorage.cs b/ConfectionaryFileImplement/PastryStorage.cs index 9fbdedd..4e03c2c 100644 --- a/ConfectionaryFileImplement/PastryStorage.cs +++ b/ConfectionaryFileImplement/PastryStorage.cs @@ -2,11 +2,7 @@ using ConfectioneryContracts.SearchModels; using ConfectioneryContracts.StoragesContract; using ConfectioneryContracts.ViewModels; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using ConfectioneryFileImplement.Models; namespace ConfectioneryFileImplement { @@ -24,7 +20,7 @@ namespace ConfectioneryFileImplement if (element != null) { _source.Pastries.Remove(element); - _source.Savepastrys(); + _source.SavePastries(); return element.GetViewModel; } return null; @@ -63,7 +59,15 @@ namespace ConfectioneryFileImplement public PastryViewModel? Insert(PastryBindingModel model) { - throw new NotImplementedException(); + model.Id = _source.Pastries.Count > 0 ? _source.Pastries.Max(x => x.Id) + 1 : 1; + var newPastry = Pastry.Create(model); + if (newPastry == null) + { + return null; + } + _source.Pastries.Add(newPastry); + _source.SavePastries(); + return newPastry.GetViewModel; } public PastryViewModel? Update(PastryBindingModel model)