This commit is contained in:
Валерия Никифорова 2023-03-07 02:08:48 +04:00
parent 83b65263d5
commit 4d54177296
17 changed files with 516 additions and 13 deletions

View File

@ -17,6 +17,7 @@
<ItemGroup>
<ProjectReference Include="..\AircraftPlantBusinessLogic\AircraftPlantBusinessLogic.csproj" />
<ProjectReference Include="..\AircraftPlantContracts\AircraftPlantContracts.csproj" />
<ProjectReference Include="..\AircraftPlantFileImplement\AircraftPlantFileImplement.csproj" />
<ProjectReference Include="..\AircraftPlantListImplement\AircraftPlantListImplement.csproj" />
</ItemGroup>

View File

@ -24,6 +24,7 @@ namespace AircraftPlantView
InitializeComponent();
_logger = logger;
_logic = logic;
LoadData();
}
private void FormComponents_Load(object sender, EventArgs e)

View File

@ -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)
});

View File

@ -24,6 +24,7 @@ namespace AircraftPlantView
InitializeComponent();
_logger = logger;
_orderLogic = orderLogic;
LoadData();
}
private void FormMain_Load(object sender, EventArgs e)

View File

@ -23,6 +23,7 @@ namespace AircraftPlantView
InitializeComponent();
_logger = logger;
_logic = logic;
LoadData();
}
private void FormPlanes_Load(object sender, EventArgs e)

View File

@ -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<IComponentStorage, ComponentStorage>();
services.AddTransient<IOrderStorage, OrderStorage>();
services.AddTransient<IPlaneStorage, PlaneStorage>();
services.AddTransient<IComponentLogic, ComponentLogic>();
services.AddTransient<IOrderLogic, OrderLogic>();
services.AddTransient<IPlaneLogic, PlaneLogic>();
services.AddTransient<FormMain>();
services.AddTransient<FormComponent>();
services.AddTransient<FormComponents>();

View File

@ -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; }

View File

@ -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; }

View File

@ -6,4 +6,9 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\AircraftPlantContracts\AircraftPlantContracts.csproj" />
<ProjectReference Include="..\AircraftPlantDataModels\AircraftPlantDataModels.csproj" />
</ItemGroup>
</Project>

View File

@ -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<Component> Components { get; private set; }
public List<Order> Orders { get; private set; }
public List<Plane> 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<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);
}
}
}
}

View File

@ -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<ComponentViewModel> 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<ComponentViewModel> 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;
}
}
}

View File

@ -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<OrderViewModel> 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<OrderViewModel> 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;
}
}
}

View File

@ -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<PlaneViewModel> 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<PlaneViewModel> 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;
}
}
}

View File

@ -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()));
}
}

View File

@ -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()));
}
}

View File

@ -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<int, int> Components { get; private set; } = new();
private Dictionary<int, (IComponentModel, int)>? _planeComponents = null;
public Dictionary<int, (IComponentModel, int)> 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()));
}
}

View File

@ -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,