labwork2
This commit is contained in:
parent
e8a62f7cc5
commit
6a6b063244
@ -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
|
||||
|
@ -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;
|
||||
/// <summary>
|
||||
/// The main entry point for the application.
|
||||
/// The main entry point for the application.
|
||||
/// </summary>
|
||||
[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();
|
||||
|
@ -18,6 +18,7 @@
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\SecuritySystemBusinessLogiс\SecuritySystemBusinessLogiс.csproj" />
|
||||
<ProjectReference Include="..\SecuritySystemContracts\SecuritySystemContracts.csproj" />
|
||||
<ProjectReference Include="..\SecuritySystemFileImplement\SecuritySystemFileImplement.csproj" />
|
||||
<ProjectReference Include="..\SecuritySystemListImplement\SecuritySystemListImplement.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
|
@ -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<Sensor> Sensors { get; private set; }
|
||||
|
||||
public List<Order> Orders { get; private set; }
|
||||
|
||||
public List<Secure> 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<T>? LoadData<T>(string filename, string xmlNodeName, Func<XElement, T> selectFunction)
|
||||
{
|
||||
if (File.Exists(filename))
|
||||
{
|
||||
return XDocument.Load(filename)?.Root?.Elements(xmlNodeName)?.Select(selectFunction)?.ToList();
|
||||
}
|
||||
|
||||
return new List<T>();
|
||||
}
|
||||
|
||||
private static void SaveData<T>(List<T> data, string filename, string xmlNodeName, Func<T, XElement> selectFunction)
|
||||
{
|
||||
if (data != null)
|
||||
{
|
||||
new XDocument(new XElement(xmlNodeName, data.Select(selectFunction).ToArray())).Save(filename);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -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<OrderViewModel> GetFullList()
|
||||
{
|
||||
return source.Orders.Select(x => GetViewModel(x)).ToList();
|
||||
}
|
||||
|
||||
public List<OrderViewModel> 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;
|
||||
}
|
||||
}
|
||||
}
|
@ -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<SecureViewModel> GetFullList()
|
||||
{
|
||||
return source.Secures.Select(x => x.GetViewModel).ToList();
|
||||
}
|
||||
|
||||
public List<SecureViewModel> 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;
|
||||
}
|
||||
}
|
||||
}
|
@ -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<SensorViewModel> GetFullList()
|
||||
{
|
||||
return source.Sensors.Select(x => x.GetViewModel).ToList();
|
||||
}
|
||||
|
||||
public List<SensorViewModel> 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;
|
||||
}
|
||||
}
|
||||
}
|
103
SecuritySystem/SecuritySystemFileImplement/Models/Order.cs
Normal file
103
SecuritySystem/SecuritySystemFileImplement/Models/Order.cs
Normal file
@ -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()));
|
||||
}
|
||||
}
|
108
SecuritySystem/SecuritySystemFileImplement/Models/Secure.cs
Normal file
108
SecuritySystem/SecuritySystemFileImplement/Models/Secure.cs
Normal file
@ -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<int, int> Sensors { get; private set; } = new();
|
||||
|
||||
|
||||
private Dictionary<int, (ISensorModel, int)>? _secureSensors = null;
|
||||
|
||||
public Dictionary<int, (ISensorModel, int)> 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()));
|
||||
}
|
||||
}
|
76
SecuritySystem/SecuritySystemFileImplement/Models/Sensor.cs
Normal file
76
SecuritySystem/SecuritySystemFileImplement/Models/Sensor.cs
Normal file
@ -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()));
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\SecuritySystemContracts\SecuritySystemContracts.csproj" />
|
||||
<ProjectReference Include="..\SecuritySystemDataModels\SecuritySystemDataModels.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
Loading…
x
Reference in New Issue
Block a user