diff --git a/SecuritySystem/SecuritySystem.sln b/SecuritySystem/SecuritySystem.sln index 000b33c..8ffe208 100644 --- a/SecuritySystem/SecuritySystem.sln +++ b/SecuritySystem/SecuritySystem.sln @@ -3,15 +3,17 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.7.34031.279 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SecuritySystem", "SecuritySystem\SecuritySystem.csproj", "{0548AE9D-59FF-4288-A519-85FF31627D7E}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SecuritySystem", "SecuritySystem\SecuritySystem.csproj", "{0548AE9D-59FF-4288-A519-85FF31627D7E}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SecuritySystemDataModels", "SecuritySystemDataModels\SecuritySystemDataModels.csproj", "{0A4860B4-91DC-4174-A4A5-789729F0E206}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SecuritySystemDataModels", "SecuritySystemDataModels\SecuritySystemDataModels.csproj", "{0A4860B4-91DC-4174-A4A5-789729F0E206}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SecuritySystemBusinessLogiс", "SecuritySystemBusinessLogiс\SecuritySystemBusinessLogiс.csproj", "{4A8ECDB2-CD12-4A42-8580-C75F044851A0}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SecuritySystemBusinessLogiс", "SecuritySystemBusinessLogiс\SecuritySystemBusinessLogiс.csproj", "{4A8ECDB2-CD12-4A42-8580-C75F044851A0}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SecuritySystemContracts", "SecuritySystemContracts\SecuritySystemContracts.csproj", "{96317F35-0916-4563-B898-069D6EC0B303}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SecuritySystemContracts", "SecuritySystemContracts\SecuritySystemContracts.csproj", "{96317F35-0916-4563-B898-069D6EC0B303}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SecuritySystemListImplement", "SecuritySystemListImplement\SecuritySystemListImplement.csproj", "{EEFB1807-2668-4DBF-80A0-06594D2C811C}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SecuritySystemListImplement", "SecuritySystemListImplement\SecuritySystemListImplement.csproj", "{EEFB1807-2668-4DBF-80A0-06594D2C811C}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SecuritySystemFileImplement", "SecuritySystemFileImplement\SecuritySystemFileImplement.csproj", "{2F21ABC1-9037-47B7-A9F8-4590BF86848D}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -39,6 +41,10 @@ Global {EEFB1807-2668-4DBF-80A0-06594D2C811C}.Debug|Any CPU.Build.0 = Debug|Any CPU {EEFB1807-2668-4DBF-80A0-06594D2C811C}.Release|Any CPU.ActiveCfg = Release|Any CPU {EEFB1807-2668-4DBF-80A0-06594D2C811C}.Release|Any CPU.Build.0 = Release|Any CPU + {2F21ABC1-9037-47B7-A9F8-4590BF86848D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2F21ABC1-9037-47B7-A9F8-4590BF86848D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2F21ABC1-9037-47B7-A9F8-4590BF86848D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2F21ABC1-9037-47B7-A9F8-4590BF86848D}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/SecuritySystem/SecuritySystem/Program.cs b/SecuritySystem/SecuritySystem/Program.cs index 7d690c7..717dc91 100644 --- a/SecuritySystem/SecuritySystem/Program.cs +++ b/SecuritySystem/SecuritySystem/Program.cs @@ -1,12 +1,11 @@ using SecuritySystemBusinessLogic.BusinessLogic; using SecuritySystemContracts.BusinessLogicsContracts; using SecuritySystemContracts.StoragesContracts; -using SecuritySystemListImplement.Implements; +using SecuritySystemFileImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; -using System.Drawing; namespace SecuritySystem { @@ -15,12 +14,12 @@ namespace SecuritySystem private static ServiceProvider? _serviceProvider; public static ServiceProvider? ServiceProvider => _serviceProvider; /// - /// The main entry point for the application. + /// The main entry point for the application. /// [STAThread] static void Main() { - // To customize application configuration such as set high DPI settings or default font; + // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); var services = new ServiceCollection(); diff --git a/SecuritySystem/SecuritySystem/SecuritySystem.csproj b/SecuritySystem/SecuritySystem/SecuritySystem.csproj index 35c7ef6..9d55f73 100644 --- a/SecuritySystem/SecuritySystem/SecuritySystem.csproj +++ b/SecuritySystem/SecuritySystem/SecuritySystem.csproj @@ -18,6 +18,7 @@ + diff --git a/SecuritySystem/SecuritySystemFileImplement/DataFileSingleton.cs b/SecuritySystem/SecuritySystemFileImplement/DataFileSingleton.cs new file mode 100644 index 0000000..e27001d --- /dev/null +++ b/SecuritySystem/SecuritySystemFileImplement/DataFileSingleton.cs @@ -0,0 +1,68 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; +using SecuritySystemFileImplement.Models; + +namespace SecuritySystemFileImplement +{ + public class DataFileSingleton + { + private static DataFileSingleton? instance; + + private readonly string SensorFileName = "Sensor.xml"; + + private readonly string OrderFileName = "Order.xml"; + + private readonly string SecureFileName = "Secure.xml"; + + public List Sensors { get; private set; } + + public List Orders { get; private set; } + + public List Secures { get; private set; } + + public static DataFileSingleton GetInstance() + { + if (instance == null) + { + instance = new DataFileSingleton(); + } + + return instance; + } + + public void SaveSensors() => SaveData(Sensors, SensorFileName, "Sensors", x => x.GetXElement); + + public void SaveSecures() => SaveData(Secures, SecureFileName, "Secures", x => x.GetXElement); + + public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); + + private DataFileSingleton() + { + Sensors = LoadData(SensorFileName, "Sensor", x => Sensor.Create(x)!)!; + Secures = LoadData(SecureFileName, "Secure", x => Secure.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/SecuritySystem/SecuritySystemFileImplement/Implements/OrderStorage.cs b/SecuritySystem/SecuritySystemFileImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..7b646dc --- /dev/null +++ b/SecuritySystem/SecuritySystemFileImplement/Implements/OrderStorage.cs @@ -0,0 +1,111 @@ +using SecuritySystemContracts.BindingModels; +using SecuritySystemContracts.SearchModels; +using SecuritySystemContracts.StoragesContracts; +using SecuritySystemContracts.ViewModels; +using SecuritySystemFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SecuritySystemFileImplement.Implements +{ + //реализация интерфейса хранилища заказов + public class OrderStorage : IOrderStorage + { + private readonly DataFileSingleton source; + + public OrderStorage() + { + source = DataFileSingleton.GetInstance(); + } + + public List GetFullList() + { + return source.Orders.Select(x => GetViewModel(x)).ToList(); + } + + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + + return source.Orders.Where(x => x.Id == model.Id).Select(x => GetViewModel(x)).ToList(); + } + + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + + return source.Orders.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + //для загрузки названий изделия в заказе + private OrderViewModel GetViewModel(Order order) + { + var viewModel = order.GetViewModel; + + var secure = source.Secures.FirstOrDefault(x => x.Id == order.SecureId); + + if (secure != null) + { + viewModel.SecureName = secure.SecureName; + } + + return viewModel; + } + + 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 GetViewModel(newOrder); + } + + 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 GetViewModel(order); + } + + 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 GetViewModel(element); + } + + return null; + } + } +} diff --git a/SecuritySystem/SecuritySystemFileImplement/Implements/SecureStorage.cs b/SecuritySystem/SecuritySystemFileImplement/Implements/SecureStorage.cs new file mode 100644 index 0000000..caac86c --- /dev/null +++ b/SecuritySystem/SecuritySystemFileImplement/Implements/SecureStorage.cs @@ -0,0 +1,97 @@ +using SecuritySystemContracts.BindingModels; +using SecuritySystemContracts.SearchModels; +using SecuritySystemContracts.StoragesContracts; +using SecuritySystemContracts.ViewModels; +using SecuritySystemFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SecuritySystemFileImplement.Implements +{ + //реализация интерфейса хранилища изделий + public class SecureStorage : ISecureStorage + { + private readonly DataFileSingleton source; + + public SecureStorage() + { + source = DataFileSingleton.GetInstance(); + } + + public List GetFullList() + { + return source.Secures.Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(SecureSearchModel model) + { + if (string.IsNullOrEmpty(model.SecureName)) + { + return new(); + } + + return source.Secures.Where(x => x.SecureName.Contains(model.SecureName)).Select(x => x.GetViewModel).ToList(); + } + + public SecureViewModel? GetElement(SecureSearchModel model) + { + if (string.IsNullOrEmpty(model.SecureName) && !model.Id.HasValue) + { + return null; + } + + return source.Secures.FirstOrDefault(x => (!string.IsNullOrEmpty(model.SecureName) && x.SecureName == model.SecureName) + || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public SecureViewModel? Insert(SecureBindingModel model) + { + model.Id = source.Secures.Count > 0 ? source.Secures.Max(x => x.Id) + 1 : 1; + + var newSecure = Secure.Create(model); + + if (newSecure == null) + { + return null; + } + + source.Secures.Add(newSecure); + source.SaveSecures(); + + return newSecure.GetViewModel; + } + + public SecureViewModel? Update(SecureBindingModel model) + { + var secure = source.Secures.FirstOrDefault(x => x.Id == model.Id); + + if (secure == null) + { + return null; + } + + secure.Update(model); + source.SaveSecures(); + + return secure.GetViewModel; + } + + public SecureViewModel? Delete(SecureBindingModel model) + { + var element = source.Secures.FirstOrDefault(x => x.Id == model.Id); + + if (element != null) + { + source.Secures.Remove(element); + source.SaveSecures(); + + return element.GetViewModel; + } + + return null; + } + } +} diff --git a/SecuritySystem/SecuritySystemFileImplement/Implements/SensorStorage.cs b/SecuritySystem/SecuritySystemFileImplement/Implements/SensorStorage.cs new file mode 100644 index 0000000..794b627 --- /dev/null +++ b/SecuritySystem/SecuritySystemFileImplement/Implements/SensorStorage.cs @@ -0,0 +1,98 @@ +using SecuritySystemContracts.BindingModels; +using SecuritySystemContracts.SearchModels; +using SecuritySystemContracts.StoragesContracts; +using SecuritySystemContracts.ViewModels; +using SecuritySystemFileImplement.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SecuritySystemFileImplement.Implements +{ + //реализация интерфейса хранилища заготовок + public class SensorStorage : ISensorStorage + { + private readonly DataFileSingleton source; + + public SensorStorage() + { + source = DataFileSingleton.GetInstance(); + } + + public List GetFullList() + { + return source.Sensors.Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(SensorSearchModel model) + { + if (string.IsNullOrEmpty(model.SensorName)) + { + return new(); + } + + return source.Sensors.Where(x => x.SensorName.Contains(model.SensorName)).Select(x => x.GetViewModel).ToList(); + } + + public SensorViewModel? GetElement(SensorSearchModel model) + { + if (string.IsNullOrEmpty(model.SensorName) && !model.Id.HasValue) + { + return null; + } + + return source.Sensors.FirstOrDefault(x => (!string.IsNullOrEmpty(model.SensorName) && x.SensorName == model.SensorName) + || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public SensorViewModel? Insert(SensorBindingModel model) + { + model.Id = source.Sensors.Count > 0 ? source.Sensors.Max(x => x.Id) + 1 : 1; + + var newSensor = Sensor.Create(model); + + if (newSensor == null) + { + return null; + } + + source.Sensors.Add(newSensor); + source.SaveSensors(); + + return newSensor.GetViewModel; + } + + public SensorViewModel? Update(SensorBindingModel model) + { + var sensor = source.Sensors.FirstOrDefault(x => x.Id == model.Id); + + if (sensor == null) + { + return null; + } + + sensor.Update(model); + source.SaveSensors(); + + return sensor.GetViewModel; + } + + public SensorViewModel? Delete(SensorBindingModel model) + { + var element = source.Sensors.FirstOrDefault(x => x.Id == model.Id); + + if (element != null) + { + source.Sensors.Remove(element); + source.SaveSensors(); + + return element.GetViewModel; + } + + return null; + } + } +} diff --git a/SecuritySystem/SecuritySystemFileImplement/Models/Order.cs b/SecuritySystem/SecuritySystemFileImplement/Models/Order.cs new file mode 100644 index 0000000..c7f7d1b --- /dev/null +++ b/SecuritySystem/SecuritySystemFileImplement/Models/Order.cs @@ -0,0 +1,103 @@ +using SecuritySystemContracts.BindingModels; +using SecuritySystemContracts.ViewModels; +using SecuritySystemDataModels.Enums; +using SecuritySystemDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace SecuritySystemFileImplement.Models +{ + //класс, реализующий интерфейс модели заказа + public class Order : IOrderModel + { + public int Id { get; private set; } + + public int SecureId { 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, + SecureId = model.SecureId, + 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), + SecureId = Convert.ToInt32(element.Element("SecureId")!.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 = 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, + SecureId = SecureId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement + }; + + + public XElement GetXElement => new("Order", + new XAttribute("Id", Id), + new XElement("SecureId", SecureId.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/SecuritySystem/SecuritySystemFileImplement/Models/Secure.cs b/SecuritySystem/SecuritySystemFileImplement/Models/Secure.cs new file mode 100644 index 0000000..29581ff --- /dev/null +++ b/SecuritySystem/SecuritySystemFileImplement/Models/Secure.cs @@ -0,0 +1,108 @@ +using SecuritySystemContracts.BindingModels; +using SecuritySystemContracts.ViewModels; +using SecuritySystemDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace SecuritySystemFileImplement.Models +{ + //класс реализующий интерфейс модели изделия + public class Secure : ISecureModel + { + public int Id { get; private set; } + + public string SecureName { get; private set; } = string.Empty; + + public double Price { get; private set; } + + public Dictionary Sensors { get; private set; } = new(); + + + private Dictionary? _secureSensors = null; + + public Dictionary SecureSensors + { + get + { + if (_secureSensors == null) + { + var source = DataFileSingleton.GetInstance(); + + _secureSensors = Sensors.ToDictionary(x => x.Key, + y => ((source.Sensors.FirstOrDefault(z => z.Id == y.Key) as ISensorModel)!, y.Value)); + } + + return _secureSensors; + } + } + + public static Secure? Create(SecureBindingModel model) + { + if (model == null) + { + return null; + } + + return new Secure() + { + Id = model.Id, + SecureName = model.SecureName, + Price = model.Price, + Sensors = model.SecureSensors.ToDictionary(x => x.Key, x => x.Value.Item2) + }; + } + + public static Secure? Create(XElement element) + { + if (element == null) + { + return null; + } + + return new Secure() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + SecureName = element.Element("SecureName")!.Value, + Price = Convert.ToDouble(element.Element("Price")!.Value), + Sensors = element.Element("SecureSensors")!.Elements("SecureSensors").ToDictionary( + x => Convert.ToInt32(x.Element("Key")?.Value), + x => Convert.ToInt32(x.Element("Value")?.Value)) + }; + } + + public void Update(SecureBindingModel model) + { + if (model == null) + { + return; + } + + SecureName = model.SecureName; + Price = model.Price; + Sensors = model.SecureSensors.ToDictionary(x => x.Key, x => x.Value.Item2); + _secureSensors = null; + } + + public SecureViewModel GetViewModel => new() + { + Id = Id, + SecureName = SecureName, + Price = Price, + SecureSensors = SecureSensors + }; + + public XElement GetXElement => new("Secure", + new XAttribute("Id", Id), + new XElement("SecureName", SecureName), + new XElement("Price", Price.ToString()), + new XElement("SecureSensors", Sensors.Select( + x => new XElement("SecureSensors", + new XElement("Key", x.Key), + new XElement("Value", x.Value)) + ).ToArray())); + } +} diff --git a/SecuritySystem/SecuritySystemFileImplement/Models/Sensor.cs b/SecuritySystem/SecuritySystemFileImplement/Models/Sensor.cs new file mode 100644 index 0000000..4859ccf --- /dev/null +++ b/SecuritySystem/SecuritySystemFileImplement/Models/Sensor.cs @@ -0,0 +1,76 @@ +using SecuritySystemContracts.BindingModels; +using SecuritySystemContracts.ViewModels; +using SecuritySystemDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace SecuritySystemFileImplement.Models +{ + //реализация интерфейса модели заготовки + public class Sensor : ISensorModel + { + public int Id { get; private set; } + + public string SensorName { get; private set; } = string.Empty; + + public double Cost { get; set; } + + public static Sensor? Create(SensorBindingModel model) + { + if (model == null) + { + return null; + } + + return new Sensor() + { + Id = model.Id, + SensorName = model.SensorName, + Cost = model.Cost + }; + } + + public static Sensor? Create(XElement element) + { + if (element == null) + { + return null; + } + + return new Sensor() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + SensorName = element.Element("SensorName")!.Value, + Cost = Convert.ToDouble(element.Element("Cost")!.Value) + }; + } + + public void Update(SensorBindingModel model) + { + if (model == null) + { + return; + } + + SensorName = model.SensorName; + Cost = model.Cost; + } + + public SensorViewModel GetViewModel => new() + { + Id = Id, + SensorName = SensorName, + Cost = Cost + }; + + public XElement GetXElement => new("Sensor", + new XAttribute("Id", Id), + new XElement("SensorName", SensorName), + new XElement("Cost", Cost.ToString())); + } +} diff --git a/SecuritySystem/SecuritySystemFileImplement/SecuritySystemFileImplement.csproj b/SecuritySystem/SecuritySystemFileImplement/SecuritySystemFileImplement.csproj new file mode 100644 index 0000000..7d1c090 --- /dev/null +++ b/SecuritySystem/SecuritySystemFileImplement/SecuritySystemFileImplement.csproj @@ -0,0 +1,14 @@ + + + + net6.0 + enable + enable + + + + + + + +