diff --git a/AircraftPlant/AircraftPlant/AircraftPlantView.csproj b/AircraftPlant/AircraftPlant/AircraftPlantView.csproj index 4cd2407..483fd00 100644 --- a/AircraftPlant/AircraftPlant/AircraftPlantView.csproj +++ b/AircraftPlant/AircraftPlant/AircraftPlantView.csproj @@ -17,6 +17,7 @@ + diff --git a/AircraftPlant/AircraftPlant/FormComponents.cs b/AircraftPlant/AircraftPlant/FormComponents.cs index cfb4462..6bfb9f1 100644 --- a/AircraftPlant/AircraftPlant/FormComponents.cs +++ b/AircraftPlant/AircraftPlant/FormComponents.cs @@ -24,6 +24,7 @@ namespace AircraftPlantView InitializeComponent(); _logger = logger; _logic = logic; + LoadData(); } private void FormComponents_Load(object sender, EventArgs e) diff --git a/AircraftPlant/AircraftPlant/FormCreateOrder.cs b/AircraftPlant/AircraftPlant/FormCreateOrder.cs index dd4d35d..aae14d3 100644 --- a/AircraftPlant/AircraftPlant/FormCreateOrder.cs +++ b/AircraftPlant/AircraftPlant/FormCreateOrder.cs @@ -101,6 +101,7 @@ namespace AircraftPlantView var operationResult = _logicO.CreateOrder(new OrderBindingModel { PlaneId = Convert.ToInt32(comboBoxPlane.SelectedValue), + PlaneName = comboBoxPlane.Text, Count = Convert.ToInt32(textBoxCount.Text), Sum = Convert.ToDouble(textBoxSum.Text) }); diff --git a/AircraftPlant/AircraftPlant/FormMain.cs b/AircraftPlant/AircraftPlant/FormMain.cs index a947a30..5525d7d 100644 --- a/AircraftPlant/AircraftPlant/FormMain.cs +++ b/AircraftPlant/AircraftPlant/FormMain.cs @@ -24,6 +24,7 @@ namespace AircraftPlantView InitializeComponent(); _logger = logger; _orderLogic = orderLogic; + LoadData(); } private void FormMain_Load(object sender, EventArgs e) diff --git a/AircraftPlant/AircraftPlant/FormPlanes.cs b/AircraftPlant/AircraftPlant/FormPlanes.cs index 6fa481d..347e171 100644 --- a/AircraftPlant/AircraftPlant/FormPlanes.cs +++ b/AircraftPlant/AircraftPlant/FormPlanes.cs @@ -23,6 +23,7 @@ namespace AircraftPlantView InitializeComponent(); _logger = logger; _logic = logic; + LoadData(); } private void FormPlanes_Load(object sender, EventArgs e) diff --git a/AircraftPlant/AircraftPlant/Program.cs b/AircraftPlant/AircraftPlant/Program.cs index f3783fb..0229f99 100644 --- a/AircraftPlant/AircraftPlant/Program.cs +++ b/AircraftPlant/AircraftPlant/Program.cs @@ -1,7 +1,7 @@ using AircraftPlantBusinessLogic.BusinessLogics; using AircraftPlantContracts.BusinessLogicsContracts; using AircraftPlantContracts.StoragesContracts; -using AircraftPlantListImplement.Implements; +using AircraftPlantFileImplement.Implements; using AircraftPlantView; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; @@ -40,9 +40,11 @@ namespace AircraftPlant services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); diff --git a/AircraftPlant/AircraftPlantContracts/BindingModels/OrderBindingModel.cs b/AircraftPlant/AircraftPlantContracts/BindingModels/OrderBindingModel.cs index 6025955..85643d3 100644 --- a/AircraftPlant/AircraftPlantContracts/BindingModels/OrderBindingModel.cs +++ b/AircraftPlant/AircraftPlantContracts/BindingModels/OrderBindingModel.cs @@ -12,6 +12,8 @@ namespace AircraftPlantContracts.BindingModels { public int PlaneId { get; set; } + public string PlaneName { get; set; } = string.Empty; + public int Count { get; set; } public double Sum { get; set; } diff --git a/AircraftPlant/AircraftPlantDataModels/Models/IOrderModel.cs b/AircraftPlant/AircraftPlantDataModels/Models/IOrderModel.cs index dcd2df3..9a09799 100644 --- a/AircraftPlant/AircraftPlantDataModels/Models/IOrderModel.cs +++ b/AircraftPlant/AircraftPlantDataModels/Models/IOrderModel.cs @@ -10,6 +10,7 @@ namespace AircraftPlantDataModels.Models public interface IOrderModel : IId { int PlaneId { get; } + string PlaneName { get; } int Count { get; } double Sum { get; } OrderStatus Status { get; } diff --git a/AircraftPlant/AircraftPlantFileImplement/AircraftPlantFileImplement.csproj b/AircraftPlant/AircraftPlantFileImplement/AircraftPlantFileImplement.csproj index 132c02c..8b75aa9 100644 --- a/AircraftPlant/AircraftPlantFileImplement/AircraftPlantFileImplement.csproj +++ b/AircraftPlant/AircraftPlantFileImplement/AircraftPlantFileImplement.csproj @@ -6,4 +6,9 @@ enable + + + + + diff --git a/AircraftPlant/AircraftPlantFileImplement/DataFileSingleton.cs b/AircraftPlant/AircraftPlantFileImplement/DataFileSingleton.cs index 78f4ebc..72f8234 100644 --- a/AircraftPlant/AircraftPlantFileImplement/DataFileSingleton.cs +++ b/AircraftPlant/AircraftPlantFileImplement/DataFileSingleton.cs @@ -1,12 +1,62 @@ -using System; +using AircraftPlantFileImplement.Models; +using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Xml.Linq; namespace AircraftPlantFileImplement { - internal class DataFileSingleton + public class DataFileSingleton { + private static DataFileSingleton? instance; + + private readonly string ComponentFileName = "Component.xml"; + + private readonly string OrderFileName = "Order.xml"; + + private readonly string PlaneFileName = "Plane.xml"; + + public List Components { get; private set; } + public List Orders { get; private set; } + public List Planes { 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 SavePlanes() => SaveData(Planes, PlaneFileName, "Planes", x => x.GetXElement); + public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); + + private DataFileSingleton() + { + Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; + Planes = LoadData(PlaneFileName, "Plane", x => Plane.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/AircraftPlant/AircraftPlantFileImplement/Implements/ComponentStorage.cs b/AircraftPlant/AircraftPlantFileImplement/Implements/ComponentStorage.cs index 0aa10fd..a08cafd 100644 --- a/AircraftPlant/AircraftPlantFileImplement/Implements/ComponentStorage.cs +++ b/AircraftPlant/AircraftPlantFileImplement/Implements/ComponentStorage.cs @@ -1,4 +1,9 @@ -using System; +using AircraftPlantContracts.BindingModels; +using AircraftPlantContracts.SearchModels; +using AircraftPlantContracts.StoragesContracts; +using AircraftPlantContracts.ViewModels; +using AircraftPlantFileImplement.Models; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -6,7 +11,73 @@ using System.Threading.Tasks; namespace AircraftPlantFileImplement.Implements { - internal class ComponentStorage + public class ComponentStorage : IComponentStorage { + private readonly DataFileSingleton source; + + public ComponentStorage() + { + source = DataFileSingleton.GetInstance(); + } + + 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; + } + + 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 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 List GetFullList() + { + return source.Components.Select(x => x.GetViewModel).ToList(); + } + + 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; + } } } diff --git a/AircraftPlant/AircraftPlantFileImplement/Implements/OrderStorage.cs b/AircraftPlant/AircraftPlantFileImplement/Implements/OrderStorage.cs index fa5555e..d30d5a6 100644 --- a/AircraftPlant/AircraftPlantFileImplement/Implements/OrderStorage.cs +++ b/AircraftPlant/AircraftPlantFileImplement/Implements/OrderStorage.cs @@ -1,4 +1,9 @@ -using System; +using AircraftPlantContracts.BindingModels; +using AircraftPlantContracts.SearchModels; +using AircraftPlantContracts.StoragesContracts; +using AircraftPlantContracts.ViewModels; +using AircraftPlantFileImplement.Models; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -6,7 +11,73 @@ using System.Threading.Tasks; namespace AircraftPlantFileImplement.Implements { - 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) + { + 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; + } } } diff --git a/AircraftPlant/AircraftPlantFileImplement/Implements/PlaneStorage.cs b/AircraftPlant/AircraftPlantFileImplement/Implements/PlaneStorage.cs index ff6f192..9f4e722 100644 --- a/AircraftPlant/AircraftPlantFileImplement/Implements/PlaneStorage.cs +++ b/AircraftPlant/AircraftPlantFileImplement/Implements/PlaneStorage.cs @@ -1,4 +1,9 @@ -using System; +using AircraftPlantContracts.BindingModels; +using AircraftPlantContracts.SearchModels; +using AircraftPlantContracts.StoragesContracts; +using AircraftPlantContracts.ViewModels; +using AircraftPlantFileImplement.Models; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -6,7 +11,74 @@ using System.Threading.Tasks; namespace AircraftPlantFileImplement.Implements { - internal class PlaneStorage + public class PlaneStorage : IPlaneStorage { + private readonly DataFileSingleton source; + + public PlaneStorage() + { + source = DataFileSingleton.GetInstance(); + } + + public PlaneViewModel? Delete(PlaneBindingModel model) + { + var element = source.Planes.FirstOrDefault(x => x.Id == model.Id); + + if (element != null) + { + source.Planes.Remove(element); + source.SavePlanes(); + return element.GetViewModel; + } + return null; + } + + public PlaneViewModel? GetElement(PlaneSearchModel model) + { + if (string.IsNullOrEmpty(model.PlaneName) && !model.Id.HasValue) + { + return null; + } + return source.Planes.FirstOrDefault(x => (!string.IsNullOrEmpty(model.PlaneName) && x.PlaneName == model.PlaneName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public List GetFilteredList(PlaneSearchModel model) + { + if (string.IsNullOrEmpty(model.PlaneName)) + { + return new(); + } + return source.Planes.Where(x => x.PlaneName.Contains(model.PlaneName)).Select(x => x.GetViewModel).ToList(); + } + + public List GetFullList() + { + return source.Planes.Select(x => x.GetViewModel).ToList(); + } + + public PlaneViewModel? Insert(PlaneBindingModel model) + { + model.Id = source.Planes.Count > 0 ? source.Planes.Max(x => x.Id) + 1 : 1; + var newPlane = Plane.Create(model); + if (newPlane == null) + { + return null; + } + source.Planes.Add(newPlane); + source.SavePlanes(); + return newPlane.GetViewModel; + } + + public PlaneViewModel? Update(PlaneBindingModel model) + { + var plane = source.Planes.FirstOrDefault(x => x.Id == model.Id); + if (plane == null) + { + return null; + } + plane.Update(model); + source.SavePlanes(); + return plane.GetViewModel; + } } } diff --git a/AircraftPlant/AircraftPlantFileImplement/Models/Component.cs b/AircraftPlant/AircraftPlantFileImplement/Models/Component.cs index 9a23edf..3143c2a 100644 --- a/AircraftPlant/AircraftPlantFileImplement/Models/Component.cs +++ b/AircraftPlant/AircraftPlantFileImplement/Models/Component.cs @@ -1,10 +1,12 @@ using AircraftPlantContracts.BindingModels; +using AircraftPlantContracts.ViewModels; using AircraftPlantDataModels.Models; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Xml.Linq; namespace AircraftPlantFileImplement.Models { @@ -29,5 +31,41 @@ namespace AircraftPlantFileImplement.Models 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/AircraftPlant/AircraftPlantFileImplement/Models/Order.cs b/AircraftPlant/AircraftPlantFileImplement/Models/Order.cs index 9617757..8d1752b 100644 --- a/AircraftPlant/AircraftPlantFileImplement/Models/Order.cs +++ b/AircraftPlant/AircraftPlantFileImplement/Models/Order.cs @@ -1,12 +1,108 @@ -using System; +using AircraftPlantContracts.BindingModels; +using AircraftPlantContracts.ViewModels; +using AircraftPlantDataModels.Enums; +using AircraftPlantDataModels.Models; +using System; using System.Collections.Generic; using System.Linq; +using System.Reflection.Metadata; using System.Text; using System.Threading.Tasks; +using System.Xml.Linq; namespace AircraftPlantFileImplement.Models { - internal class Order + public class Order : IOrderModel { + public int PlaneId { get; private set; } + + public string PlaneName { 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 int Id { get; private set; } + + public static Order? Create(OrderBindingModel model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + PlaneId = model.PlaneId, + PlaneName = model.PlaneName, + 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; + } + + var order = new Order() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + PlaneId = Convert.ToInt32(element.Element("PlaneId")!.Value), + PlaneName = element.Element("PlaneName")!.Value, + Count = Convert.ToInt32(element.Element("Count")!.Value), + Sum = Convert.ToDouble(element.Element("Sum")!.Value), + Status = (OrderStatus)Enum.Parse(typeof(OrderStatus), element.Element("Status")!.Value), + DateCreate = DateTime.ParseExact(element.Element("DateCreate")!.Value, "G", null) + }; + + DateTime.TryParse(element.Element("DateImplement")!.Value, out DateTime dateImpl); + order.DateImplement = dateImpl; + + return order; + } + + public void Update(OrderBindingModel model) + { + if (model == null) + { + return; + } + Status = model.Status; + DateImplement = model.DateImplement; + } + + public OrderViewModel GetViewModel => new() + { + Id = Id, + PlaneId = PlaneId, + PlaneName = PlaneName, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement + }; + + public XElement GetXElement => new("Order", + new XAttribute("Id", Id), + new XElement("PlaneName", PlaneName), + new XElement("PlaneId", PlaneId.ToString()), + 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())); } } diff --git a/AircraftPlant/AircraftPlantFileImplement/Models/Plane.cs b/AircraftPlant/AircraftPlantFileImplement/Models/Plane.cs index 184f468..343705f 100644 --- a/AircraftPlant/AircraftPlantFileImplement/Models/Plane.cs +++ b/AircraftPlant/AircraftPlantFileImplement/Models/Plane.cs @@ -1,12 +1,98 @@ -using System; +using AircraftPlantContracts.BindingModels; +using AircraftPlantContracts.ViewModels; +using AircraftPlantDataModels.Models; +using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Xml.Linq; namespace AircraftPlantFileImplement.Models { - internal class Plane + public class Plane : IPlaneModel { + public string PlaneName { get; private set; } = string.Empty; + + public double Price { get; private set; } + + public Dictionary Components { get; private set; } = new(); + + private Dictionary? _planeComponents = null; + + public Dictionary PlaneComponents + { + get + { + if (_planeComponents == null) + { + var source = DataFileSingleton.GetInstance(); + _planeComponents = Components.ToDictionary(x => x.Key, y => ((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!, y.Value)); + } + return _planeComponents; + } + } + + public int Id { get; private set; } + + public static Plane? Create(PlaneBindingModel model) + { + if (model == null) + { + return null; + } + return new Plane() + { + Id = model.Id, + PlaneName = model.PlaneName, + Price = model.Price, + Components = model.PlaneComponents.ToDictionary(x => x.Key, x => x.Value.Item2) + + }; + } + + public static Plane? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Plane() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + PlaneName = element.Element("PlaneName")!.Value, + Price = Convert.ToDouble(element.Element("Price")!.Value), + Components = element.Element("PlaneComponents")!.Elements("PlaneComponent").ToDictionary(x => Convert.ToInt32(x.Element("Key")?.Value), x => Convert.ToInt32(x.Element("Value")?.Value)) + }; + } + + public void Update(PlaneBindingModel model) + { + if (model == null) + { + return; + } + PlaneName = model.PlaneName; + Price = model.Price; + Components = model.PlaneComponents.ToDictionary(x => x.Key, x => x.Value.Item2); + _planeComponents = null; + } + + public PlaneViewModel GetViewModel => new() + { + Id = Id, + PlaneName = PlaneName, + Price = Price, + PlaneComponents = PlaneComponents + }; + + public XElement GetXElement => new("Plane", + new XAttribute("Id", Id), + new XElement("PlaneName", PlaneName), + new XElement("Price", Price.ToString()), + new XElement("PlaneComponents", Components.Select(x => + new XElement("PlaneComponent", + new XElement("Key", x.Key), + new XElement("Value", x.Value))).ToArray())); } } diff --git a/AircraftPlant/AircraftPlantListImplement/Models/Order.cs b/AircraftPlant/AircraftPlantListImplement/Models/Order.cs index a699d4c..00f29f8 100644 --- a/AircraftPlant/AircraftPlantListImplement/Models/Order.cs +++ b/AircraftPlant/AircraftPlantListImplement/Models/Order.cs @@ -15,6 +15,8 @@ namespace AircraftPlantListImplement.Models { public int PlaneId { get; private set; } + public string PlaneName { get; private set; } = string.Empty; + public int Count { get; private set; } public double Sum { get; private set; } @@ -36,6 +38,7 @@ namespace AircraftPlantListImplement.Models return new Order { PlaneId = model.PlaneId, + PlaneName = model.PlaneName, Count = model.Count, Sum = model.Sum, Status = model.Status, @@ -58,6 +61,7 @@ namespace AircraftPlantListImplement.Models public OrderViewModel GetViewModel => new() { PlaneId = PlaneId, + PlaneName = PlaneName, Count = Count, Sum = Sum, DateCreate = DateCreate,