From eb47697811890f36b6a45f78b2c9c41610ff40da Mon Sep 17 00:00:00 2001 From: Ivan_Starostin Date: Sat, 27 Apr 2024 19:10:04 +0400 Subject: [PATCH 1/3] Upload files to 'ShipyardFileImplement' --- ShipyardFileImplement/Component.cs | 64 ++++++++++++++++ ShipyardFileImplement/ComponentStorage.cs | 89 ++++++++++++++++++++++ ShipyardFileImplement/DataFileSingleton.cs | 60 +++++++++++++++ ShipyardFileImplement/Order.cs | 87 +++++++++++++++++++++ ShipyardFileImplement/OrderStorage.cs | 89 ++++++++++++++++++++++ 5 files changed, 389 insertions(+) create mode 100644 ShipyardFileImplement/Component.cs create mode 100644 ShipyardFileImplement/ComponentStorage.cs create mode 100644 ShipyardFileImplement/DataFileSingleton.cs create mode 100644 ShipyardFileImplement/Order.cs create mode 100644 ShipyardFileImplement/OrderStorage.cs diff --git a/ShipyardFileImplement/Component.cs b/ShipyardFileImplement/Component.cs new file mode 100644 index 0000000..7e000f2 --- /dev/null +++ b/ShipyardFileImplement/Component.cs @@ -0,0 +1,64 @@ +using ShipyardContracts.BindingModels; +using ShipyardContracts.ViewModels; +using ShipyardDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace ShipyardFileImplement.Models +{ + public class Component : IComponentModel + { + public int Id { get; set; } + public string ComponentName { get; 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/ShipyardFileImplement/ComponentStorage.cs b/ShipyardFileImplement/ComponentStorage.cs new file mode 100644 index 0000000..50dbab6 --- /dev/null +++ b/ShipyardFileImplement/ComponentStorage.cs @@ -0,0 +1,89 @@ +using ShipyardContracts.BindingModels; +using ShipyardContracts.SearchModels; +using ShipyardContracts.StoragesContracts; +using ShipyardContracts.ViewModels; +using ShipyardFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ShipyardFileImplement.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; + } + } +} \ No newline at end of file diff --git a/ShipyardFileImplement/DataFileSingleton.cs b/ShipyardFileImplement/DataFileSingleton.cs new file mode 100644 index 0000000..a13ee66 --- /dev/null +++ b/ShipyardFileImplement/DataFileSingleton.cs @@ -0,0 +1,60 @@ +using ShipyardFileImplement.Models; +using System.ComponentModel; +using System.Xml.Linq; +using Component = ShipyardFileImplement.Models.Component; + +namespace ShipyardFileImplement +{ + internal class DataFileSingleton + { + private static DataFileSingleton? instance; + private readonly string ComponentFileName = "Component.xml"; + private readonly string OrderFileName = "Order.xml"; + private readonly string ShipFileName = "Ship.xml"; + public List Components { get; private set; } + public List Orders { get; private set; } + public List Ships { 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 SaveShips() => SaveData(Ships, ShipFileName, + "Ships", x => x.GetXElement); + public void SaveOrders() => SaveData(Orders, OrderFileName, + "Orders", x => x.GetXElement); + private DataFileSingleton() + { + Components = LoadData(ComponentFileName, "Component", x => + Component.Create(x)!)!; + Ships = LoadData(ShipFileName, "Ship", x => + Ship.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); + } + } + } +} \ No newline at end of file diff --git a/ShipyardFileImplement/Order.cs b/ShipyardFileImplement/Order.cs new file mode 100644 index 0000000..de53b25 --- /dev/null +++ b/ShipyardFileImplement/Order.cs @@ -0,0 +1,87 @@ +using ShipyardContracts.BindingModels; +using ShipyardContracts.ViewModels; +using ShipyardDataModels.Enums; +using ShipyardDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace ShipyardFileImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; private set; } + public int ShipId { get; private set; } + public int Count { get; private set; } + public double Sum { get; private set; } + public OrderStatus Status { get; private set; } + public DateTime DateCreate { get; private set; } + public DateTime? DateImplement { get; private set; } + + public static Order? Create(OrderBindingModel model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + ShipId = model.ProductId, + 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), + ShipId = Convert.ToInt32(element.Element("ShipId")!.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.ToString()), + 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; + } + Status = model.Status; + DateImplement = model.DateImplement; + } + public OrderViewModel GetViewModel => new() + { + Id = Id, + ShipId = ShipId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement, + }; + public XElement GetXElement => new("Order", + new XAttribute("Id", Id), + new XElement("ShipId", ShipId), + new XElement("Sum", Sum.ToString()), + new XElement("Count", Count), + new XElement("Status", Status.ToString()), + new XElement("DateCreate", DateCreate.ToString()), + new XElement("DateImplement", DateImplement.ToString()) + ); + } +} \ No newline at end of file diff --git a/ShipyardFileImplement/OrderStorage.cs b/ShipyardFileImplement/OrderStorage.cs new file mode 100644 index 0000000..1122256 --- /dev/null +++ b/ShipyardFileImplement/OrderStorage.cs @@ -0,0 +1,89 @@ +using ShipyardContracts.BindingModels; +using ShipyardContracts.SearchModels; +using ShipyardContracts.StoragesContracts; +using ShipyardContracts.ViewModels; +using ShipyardFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ShipyardFileImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + private readonly DataFileSingleton source; + public OrderStorage() + { + source = DataFileSingleton.GetInstance(); + } + public List GetFullList() + { + return source.Orders.Select(x => AccessShipStorage(x.GetViewModel)).ToList(); + } + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + return source.Orders + .Where(x => x.Id == model.Id) + .Select(x => AccessShipStorage(x.GetViewModel)) + .ToList(); + } + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + return AccessShipStorage(source.Orders.FirstOrDefault( + x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel + ); + } + 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 AccessShipStorage(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 AccessShipStorage(order.GetViewModel); + } + 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 AccessShipStorage(element.GetViewModel); + } + return null; + } + public OrderViewModel? AccessShipStorage(OrderViewModel model) + { + if (model == null) + return null; + model = source.Ships.Where(x => x.Id == model.ShipId).FirstOrDefault(); + return model; + } + } +} \ No newline at end of file -- 2.25.1 From 496710f2acb8023f8eadcf4828ba1f6bea8287ee Mon Sep 17 00:00:00 2001 From: Ivan_Starostin Date: Sat, 27 Apr 2024 19:10:30 +0400 Subject: [PATCH 2/3] Upload files to 'ShipyardFileImplement' --- ShipyardFileImplement/Ship.cs | 95 +++++++++++++++++++ ShipyardFileImplement/ShipStorage.cs | 89 +++++++++++++++++ .../ShipyardFileImplemented.csproj | 23 +++++ 3 files changed, 207 insertions(+) create mode 100644 ShipyardFileImplement/Ship.cs create mode 100644 ShipyardFileImplement/ShipStorage.cs create mode 100644 ShipyardFileImplement/ShipyardFileImplemented.csproj diff --git a/ShipyardFileImplement/Ship.cs b/ShipyardFileImplement/Ship.cs new file mode 100644 index 0000000..ebcaea9 --- /dev/null +++ b/ShipyardFileImplement/Ship.cs @@ -0,0 +1,95 @@ +using ShipyardContracts.BindingModels; +using ShipyardContracts.ViewModels; +using ShipyardDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace ShipyardFileImplement.Models +{ + public class Ship : IShipModel + { + public int Id { get; private set; } + public string ShipName { get; private set; } = string.Empty; + public double Price { get; private set; } + public Dictionary Components { get; private set; } = new(); + private Dictionary? _ShipComponents = null; + public Dictionary ShipComponents + { + get + { + if (_ShipComponents == null) + { + var source = DataFileSingleton.GetInstance(); + _ShipComponents = Components.ToDictionary(x => x.Key, y => + ((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!, + y.Value)); + } + return _ShipComponents; + } + } + public static Ship? Create(ShipBindingModel model) + { + if (model == null) + { + return null; + } + return new Ship() + { + Id = model.Id, + ShipName = model.ShipName, + Price = model.Price, + Components = model.ShipComponents.ToDictionary(x => x.Key, x + => x.Value.Item2) + }; + } + public static Ship? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Ship() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + ShipName = element.Element("ShipName")!.Value, + Price = Convert.ToDouble(element.Element("Price")!.Value), + Components = + element.Element("ShipComponents")!.Elements("ShipComponent") + .ToDictionary(x => + Convert.ToInt32(x.Element("Key")?.Value), x => + Convert.ToInt32(x.Element("Value")?.Value)) + }; + } + public void Update(ShipBindingModel model) + { + if (model == null) + { + return; + } + ShipName = model.ShipName; + Price = model.Price; + Components = model.ShipComponents.ToDictionary(x => x.Key, x => + x.Value.Item2); + _ShipComponents = null; + } + public ShipViewModel GetViewModel => new() + { + Id = Id, + ShipName = ShipName, + Price = Price, + ShipComponents = ShipComponents + }; + public XElement GetXElement => new("Ship", + new XAttribute("Id", Id), + new XElement("ShipName", ShipName), + new XElement("Price", Price.ToString()), + new XElement("ShipComponents", Components.Select(x => + new XElement("ShipComponent", + new XElement("Key", x.Key), + new XElement("Value", x.Value))).ToArray())); + } +} diff --git a/ShipyardFileImplement/ShipStorage.cs b/ShipyardFileImplement/ShipStorage.cs new file mode 100644 index 0000000..60a8656 --- /dev/null +++ b/ShipyardFileImplement/ShipStorage.cs @@ -0,0 +1,89 @@ +using ShipyardContracts.BindingModels; +using ShipyardContracts.SearchModels; +using ShipyardContracts.StoragesContracts; +using ShipyardContracts.ViewModels; +using ShipyardFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ShipyardFileImplement.Implements +{ + public class ShipStorage : IShipStorage + { + private readonly DataFileSingleton source; + public ShipStorage() + { + source = DataFileSingleton.GetInstance(); + } + public List GetFullList() + { + return source.Ships + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(ShipSearchModel model) + { + if (string.IsNullOrEmpty(model.ShipName)) + { + return new(); + } + return source.Ships + .Where(x => x.ShipName.Contains(model.ShipName)) + .Select(x => x.GetViewModel) + .ToList(); + } + public ShipViewModel? GetElement(ShipSearchModel model) + { + if (string.IsNullOrEmpty(model.ShipName) && !model.Id.HasValue) + { + return null; + } + return source.Ships + .FirstOrDefault(x => + (!string.IsNullOrEmpty(model.ShipName) && x.ShipName == + model.ShipName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + public ShipViewModel? Insert(ShipBindingModel model) + { + model.Id = source.Ships.Count > 0 ? source.Ships.Max(x => x.Id) + 1 : 1; + var newShip = Ship.Create(model); + if (newShip == null) + { + return null; + } + source.Ships.Add(newShip); + source.SaveShips(); + return newShip.GetViewModel; + } + public ShipViewModel? Update(ShipBindingModel model) + { + var Ship = source.Ships.FirstOrDefault(x => x.Id == + model.Id); + if (Ship == null) + { + return null; + } + Ship.Update(model); + source.SaveShips(); + return Ship.GetViewModel; + } + public ShipViewModel? Delete(ShipBindingModel model) + { + var Ship = source.Ships.FirstOrDefault(x => x.Id == + model.Id); + if (Ship != null) + { + source.Ships.Remove(Ship); + source.SaveShips(); + return Ship.GetViewModel; + } + return null; + } + } +} \ No newline at end of file diff --git a/ShipyardFileImplement/ShipyardFileImplemented.csproj b/ShipyardFileImplement/ShipyardFileImplemented.csproj new file mode 100644 index 0000000..6559eb4 --- /dev/null +++ b/ShipyardFileImplement/ShipyardFileImplemented.csproj @@ -0,0 +1,23 @@ + + + + net6.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + -- 2.25.1 From a371c92e52ab1a678397bad9b0731b3402111a84 Mon Sep 17 00:00:00 2001 From: Ivan_Starostin Date: Sat, 27 Apr 2024 19:12:06 +0400 Subject: [PATCH 3/3] Upload files to 'ShipyardView' --- ShipyardView/Program.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/ShipyardView/Program.cs b/ShipyardView/Program.cs index 885e062..bfc2f81 100644 --- a/ShipyardView/Program.cs +++ b/ShipyardView/Program.cs @@ -2,12 +2,14 @@ using Microsoft.Extensions.DependencyInjection; using ShipyardBusinessLogic.BusinessLogics; using ShipyardContracts.BusinessLogicsContracts; using ShipyardContracts.StoragesContracts; -using ShipyardListImplement.Implements; -using ShipyardListImplement; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; using System; using ShipyardBusinessLogic; +using ShipyardFileImplement; +using ShipyardFileImplement.Implements; +using OrderStorage = ShipyardFileImplement.Implements.OrderStorage; +using ShipStorage = ShipyardFileImplement.Implements.ShipStorage; namespace ShipyardView { -- 2.25.1