Фулл вторая лаба получается

This commit is contained in:
Marselchi 2024-03-10 21:25:34 +04:00
parent f94e335f3d
commit 22eda2809c
11 changed files with 545 additions and 8 deletions

View File

@ -3,15 +3,17 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.8.34408.163
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ShipyardDataModels", "ShipyardDataModels\ShipyardDataModels.csproj", "{260F4D58-9D03-457C-B522-CE27360CDD31}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ShipyardDataModels", "ShipyardDataModels\ShipyardDataModels.csproj", "{260F4D58-9D03-457C-B522-CE27360CDD31}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ShipyardContracts", "ShipyardContracts\ShipyardContracts.csproj", "{C2E0380E-51D2-4B27-A385-459E91DC75CD}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ShipyardContracts", "ShipyardContracts\ShipyardContracts.csproj", "{C2E0380E-51D2-4B27-A385-459E91DC75CD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ShipyardBusinessLogic", "ShipyardBusinessLogic\ShipyardBusinessLogic.csproj", "{652CC6F7-1097-46F4-B516-A010D6D747EA}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ShipyardBusinessLogic", "ShipyardBusinessLogic\ShipyardBusinessLogic.csproj", "{652CC6F7-1097-46F4-B516-A010D6D747EA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ShipyardListImplement", "ShipyardListImplement\ShipyardListImplement.csproj", "{0F6E8B4C-CD20-4371-8316-E13FDA31AE57}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ShipyardListImplement", "ShipyardListImplement\ShipyardListImplement.csproj", "{0F6E8B4C-CD20-4371-8316-E13FDA31AE57}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ShipyardView", "ShipyardView\ShipyardView.csproj", "{BE881F13-7D8F-43B7-AA58-18BBE668FA79}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ShipyardView", "ShipyardView\ShipyardView.csproj", "{BE881F13-7D8F-43B7-AA58-18BBE668FA79}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ShipyardFileImplement", "ShipyardFileImplement\ShipyardFileImplement.csproj", "{804C4D1B-2098-4653-8CBA-F5931605A28D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -39,6 +41,10 @@ Global
{BE881F13-7D8F-43B7-AA58-18BBE668FA79}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BE881F13-7D8F-43B7-AA58-18BBE668FA79}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BE881F13-7D8F-43B7-AA58-18BBE668FA79}.Release|Any CPU.Build.0 = Release|Any CPU
{804C4D1B-2098-4653-8CBA-F5931605A28D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{804C4D1B-2098-4653-8CBA-F5931605A28D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{804C4D1B-2098-4653-8CBA-F5931605A28D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{804C4D1B-2098-4653-8CBA-F5931605A28D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -0,0 +1,51 @@
using ShipyardFileImplement.Models;
using System.Xml.Linq;
namespace ShipyardFileImplement
{
internal class DataFileSingleton
{
private static DataFileSingleton? instance;
private readonly string DetailFileName = "Detail.xml";
private readonly string OrderFileName = "Order.xml";
private readonly string ShipFileName = "Ship.xml";
public List<Detail> Details { get; private set; }
public List<Order> Orders { get; private set; }
public List<Ship> Ships { get; private set; }
public static DataFileSingleton GetInstance()
{
if (instance == null)
{
instance = new DataFileSingleton();
}
return instance;
}
public void SaveDetails() => SaveData(Details, DetailFileName, "Details", 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()
{
Details = LoadData(DetailFileName, "Detail", x => Detail.Create(x)!)!;
Ships = LoadData(ShipFileName, "Ship", x => Ship.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

@ -0,0 +1,75 @@
using ShipyardContracts.BindingModels;
using ShipyardContracts.StoragesContracts;
using ShipyardContracts.ViewModels;
using ShipyardContracts.SearchModels;
using ShipyardFileImplement.Models;
namespace ShipyardFileImplement.Implements
{
public class DetailStorage : IDetailStorage
{
private readonly DataFileSingleton source;
public DetailStorage()
{
source = DataFileSingleton.GetInstance();
}
public List<DetailViewModel> GetFullList()
{
return source.Details.Select(x => x.GetViewModel).ToList();
}
public List<DetailViewModel> GetFilteredList(DetailSearchModel model)
{
if (string.IsNullOrEmpty(model.DetailName))
{
return new();
}
return source.Details
.Where(x => x.DetailName.Contains(model.DetailName)).Select(x => x.GetViewModel).ToList();
}
public DetailViewModel? GetElement(DetailSearchModel model)
{
if (string.IsNullOrEmpty(model.DetailName) && !model.Id.HasValue)
{
return null;
}
return source.Details
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.DetailName)
&& x.DetailName == model.DetailName)
|| (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public DetailViewModel? Insert(DetailBindingModel model)
{
model.Id = source.Details.Count > 0 ? source.Details.Max(x => x.Id) + 1 : 1;
var newComponent = Detail.Create(model);
if (newComponent == null)
{
return null;
}
source.Details.Add(newComponent);
source.SaveDetails();
return newComponent.GetViewModel;
}
public DetailViewModel? Update(DetailBindingModel model)
{
var component = source.Details.FirstOrDefault(x => x.Id == model.Id);
if (component == null)
{
return null;
}
component.Update(model);
source.SaveDetails();
return component.GetViewModel;
}
public DetailViewModel? Delete(DetailBindingModel model)
{
var element = source.Details.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
source.Details.Remove(element);
source.SaveDetails();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,88 @@
using ShipyardContracts.BindingModels;
using ShipyardContracts.StoragesContracts;
using ShipyardContracts.ViewModels;
using ShipyardContracts.SearchModels;
using ShipyardFileImplement.Models;
namespace ShipyardFileImplement.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.Equals(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 ship = source.Ships.FirstOrDefault(x => x.Id == order.ShipId);
if (ship != null)
{
viewModel.ShipName = ship.ShipName;
}
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;
}
}
}

View File

@ -0,0 +1,73 @@
using ShipyardContracts.BindingModels;
using ShipyardContracts.StoragesContracts;
using ShipyardContracts.ViewModels;
using ShipyardContracts.SearchModels;
using ShipyardFileImplement.Models;
namespace ShipyardFileImplement.Implements
{
public class ShipStorage : IShipStorage
{
private readonly DataFileSingleton source;
public ShipStorage()
{
source = DataFileSingleton.GetInstance();
}
public List<ShipViewModel> GetFullList()
{
return source.Ships.Select(x => x.GetViewModel).ToList();
}
public List<ShipViewModel> 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 element = source.Ships.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
source.Ships.Remove(element);
source.SaveShips();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,60 @@
using ShipyardContracts.BindingModels;
using ShipyardContracts.ViewModels;
using ShipyardDataModels.Models;
using System.Xml.Linq;
namespace ShipyardFileImplement.Models
{
public class Detail : IDetailModel
{
public int Id { get; private set; }
public string DetailName { get; private set; } = string.Empty;
public double Cost { get; set; }
public static Detail? Create(DetailBindingModel model)
{
if (model == null)
{
return null;
}
return new Detail()
{
Id = model.Id,
DetailName = model.DetailName,
Cost = model.Cost
};
}
public static Detail? Create(XElement element)
{
if (element == null)
{
return null;
}
return new Detail()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
DetailName = element.Element("DetailName")!.Value,
Cost = Convert.ToDouble(element.Element("Cost")!.Value)
};
}
public void Update(DetailBindingModel model)
{
if (model == null)
{
return;
}
DetailName = model.DetailName;
Cost = model.Cost;
}
public DetailViewModel GetViewModel => new()
{
Id = Id,
DetailName = DetailName,
Cost = Cost
};
public XElement GetXElement => new("Detail",
new XAttribute("Id", Id),
new XElement("DetailName", DetailName),
new XElement("Cost", Cost.ToString()));
}
}

View File

@ -0,0 +1,91 @@
using ShipyardContracts.BindingModels;
using ShipyardContracts.ViewModels;
using ShipyardDataModels.Enums;
using ShipyardDataModels.Models;
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; } = 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,
ShipId = model.ShipId,
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),
ShipId = Convert.ToInt32(element.Element("ShipId")!.Value),
Count = Convert.ToInt32(element.Element("Count")!.Value),
Sum = Convert.ToDouble(element.Element("Sum")!.Value),
DateCreate = DateTime.ParseExact(element.Element("DateCreate")!.Value, "G", null),
};
DateTime.TryParse(element.Element("DateImplement")!.Value, out DateTime dateImpl);
order.DateImplement = dateImpl;
if (!Enum.TryParse(element.Element("Status")!.Value, out OrderStatus status))
{
return null;
}
order.Status = status;
return order;
}
public void Update(OrderBindingModel? model)
{
if (model == null)
{
return;
}
Status = model.Status;
DateImplement = model.DateImplement;
}
public OrderViewModel GetViewModel => new()
{
ShipId = ShipId,
Count = Count,
Sum = Sum,
DateCreate = DateCreate,
DateImplement = DateImplement,
Id = Id,
Status = Status,
};
public XElement GetXElement => new("Order",
new XAttribute("Id", Id),
new XElement("ShipId", ShipId),
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

@ -0,0 +1,80 @@
using ShipyardContracts.BindingModels;
using ShipyardContracts.ViewModels;
using ShipyardDataModels.Models;
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<int, int> Details { get; private set; } = new();
private Dictionary<int, (IDetailModel, int)>? _shipDetails = null;
public Dictionary<int, (IDetailModel, int)> ShipDetails
{
get
{
if (_shipDetails == null)
{
var source = DataFileSingleton.GetInstance();
_shipDetails = Details.ToDictionary(x => x.Key, y => ((source.Details.FirstOrDefault(z => z.Id == y.Key) as IDetailModel)!, y.Value));
}
return _shipDetails;
}
}
public static Ship? Create(ShipBindingModel model)
{
if (model == null)
{
return null;
}
return new Ship()
{
Id = model.Id,
ShipName = model.ShipName,
Price = model.Price,
Details = model.ShipDetails.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),
Details = element.Element("ShipDetails")!.Elements("ShipDetail").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;
Details = model.ShipDetails.ToDictionary(x => x.Key, x => x.Value.Item2);
_shipDetails = null;
}
public ShipViewModel GetViewModel => new()
{
Id = Id,
ShipName = ShipName,
Price = Price,
ShipDetails = ShipDetails
};
public XElement GetXElement => new("Ship",
new XAttribute("Id", Id),
new XElement("ShipName", ShipName),
new XElement("Price", Price.ToString()),
new XElement("ShipDetails", ShipDetails.Select(x => new XElement("ShipDetails",
new XElement("Key", x.Key), new XElement("Value", x.Value))).ToArray()));
}
}

View File

@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\ShipyardContracts\ShipyardContracts.csproj" />
<ProjectReference Include="..\ShipyardDataModels\ShipyardDataModels.csproj" />
</ItemGroup>
</Project>

View File

@ -1,9 +1,8 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using ShipyardBusinessLogic.BusinessLogics;
using ShipyardContracts.BusinessLogicsContracts;
using ShipyardContracts.StoragesContracts;
using ShipyardListImplement.Implements;
using ShipyardFileImplement.Implements;
using NLog.Extensions.Logging;
namespace ShipyardView

View File

@ -18,7 +18,7 @@
<ItemGroup>
<ProjectReference Include="..\ShipyardBusinessLogic\ShipyardBusinessLogic.csproj" />
<ProjectReference Include="..\ShipyardListImplement\ShipyardListImplement.csproj" />
<ProjectReference Include="..\ShipyardFileImplement\ShipyardFileImplement.csproj" />
</ItemGroup>
<ItemGroup>