laba is done

This commit is contained in:
vladimir_zinovev 2024-03-20 22:21:11 +04:00
parent 11ca98df8b
commit 7c2e0f5168
12 changed files with 670 additions and 1 deletions

View File

@ -13,6 +13,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FurnitureAssemblyListImplem
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FurnitureAssemblyBusinessLogic", "FurnitureAssemblyBusinessLogic\FurnitureAssemblyBusinessLogic.csproj", "{D5118A99-17DE-4722-AA56-3A358CE0BB62}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FurnitureAssemblyFileImplement", "FurnitureAssemblyFileImplement\FurnitureAssemblyFileImplement.csproj", "{AD1729BF-2142-4014-9C7C-A2BAF426DFEB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -39,6 +41,10 @@ Global
{D5118A99-17DE-4722-AA56-3A358CE0BB62}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D5118A99-17DE-4722-AA56-3A358CE0BB62}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D5118A99-17DE-4722-AA56-3A358CE0BB62}.Release|Any CPU.Build.0 = Release|Any CPU
{AD1729BF-2142-4014-9C7C-A2BAF426DFEB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AD1729BF-2142-4014-9C7C-A2BAF426DFEB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AD1729BF-2142-4014-9C7C-A2BAF426DFEB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AD1729BF-2142-4014-9C7C-A2BAF426DFEB}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -46,10 +46,13 @@
this.dataGridView.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
this.dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView.Location = new System.Drawing.Point(0, 0);
this.dataGridView.MultiSelect = false;
this.dataGridView.Name = "dataGridView";
this.dataGridView.ReadOnly = true;
this.dataGridView.RowHeadersVisible = false;
this.dataGridView.RowHeadersWidth = 82;
this.dataGridView.RowTemplate.Height = 41;
this.dataGridView.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
this.dataGridView.Size = new System.Drawing.Size(1033, 753);
this.dataGridView.TabIndex = 0;
//

View File

@ -28,6 +28,7 @@
<ItemGroup>
<ProjectReference Include="..\FurnitureAssemblyBusinessLogic\FurnitureAssemblyBusinessLogic.csproj" />
<ProjectReference Include="..\FurnitureAssemblyContracts\FurnitureAssemblyContracts.csproj" />
<ProjectReference Include="..\FurnitureAssemblyFileImplement\FurnitureAssemblyFileImplement.csproj" />
<ProjectReference Include="..\FurnitureAssemblyListImplement\FurnitureAssemblyListImplement.csproj" />
</ItemGroup>

View File

@ -2,7 +2,7 @@ using FurnitureAssemblyBusinessLogic;
using FurnitureAssemblyBusinessLogic.BusinessLogics;
using FurnitureAssemblyContracts.BusinessLogicsContracts;
using FurnitureAssemblyContracts.StoragesContracts;
using FurnitureAssemblyListImplement.Implements;
using FurnitureAssemblyFileImplement.Implements;
using FurnitureAssemblyView;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

View File

@ -0,0 +1,69 @@
using FurnitureAssemblyFileImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace FurnitureAssemblyFileImplement
{
internal class DataFileSingleton
{
private static DataFileSingleton? instance;
private readonly string WorkpieceFileName = "Workpiece.xml";
private readonly string OrderFileName = "Order.xml";
private readonly string FurnitureFileName = "Furniture.xml";
public List<Workpiece> Workpieces { get; private set; }
public List<Order> Orders { get; private set; }
public List<Furniture> Furnitures { get; private set; }
public static DataFileSingleton GetInstance()
{
if (instance == null)
{
instance = new DataFileSingleton();
}
return instance;
}
public void SaveWorkpieces() => SaveData(Workpieces, WorkpieceFileName, "Workpieces", x => x.GetXElement);
public void SaveFurnitures() => SaveData(Furnitures, FurnitureFileName, "Furnitures", x => x.GetXElement);
public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement);
private DataFileSingleton()
{
Workpieces = LoadData(WorkpieceFileName, "Workpiece", x => Workpiece.Create(x)!)!;
Furnitures = LoadData(FurnitureFileName, "Furniture", x => Furniture.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,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\FurnitureAssemblyContracts\FurnitureAssemblyContracts.csproj" />
<ProjectReference Include="..\FurnitureAssemblyDataModels\FurnitureAssemblyDataModels.csproj" />
<ProjectReference Include="..\FurnitureAssemblyListImplement\FurnitureAssemblyListImplement.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,96 @@
using FurnitureAssemblyContracts.BindingModels;
using FurnitureAssemblyContracts.SearchModels;
using FurnitureAssemblyContracts.StoragesContracts;
using FurnitureAssemblyContracts.ViewModels;
using FurnitureAssemblyFileImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FurnitureAssemblyFileImplement.Implements
{
public class FurnitureStorage : IFurnitureStorage
{
private readonly DataFileSingleton source;
public FurnitureStorage()
{
source = DataFileSingleton.GetInstance();
}
public List<FurnitureViewModel> GetFullList()
{
return source.Furnitures.Select(x => x.GetViewModel).ToList();
}
public List<FurnitureViewModel> GetFilteredList(FurnitureSearchModel model)
{
if (string.IsNullOrEmpty(model.FurnitureName))
{
return new();
}
return source.Furnitures.Where(x => x.FurnitureName.Contains(model.FurnitureName)).Select(x => x.GetViewModel).ToList();
}
public FurnitureViewModel? GetElement(FurnitureSearchModel model)
{
if (string.IsNullOrEmpty(model.FurnitureName) && !model.Id.HasValue)
{
return null;
}
return source.Furnitures.FirstOrDefault(x => (!string.IsNullOrEmpty(model.FurnitureName) && x.FurnitureName == model.FurnitureName)
|| (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public FurnitureViewModel? Insert(FurnitureBindingModel model)
{
model.Id = source.Furnitures.Count > 0 ? source.Furnitures.Max(x => x.Id) + 1 : 1;
var newFurniture = Furniture.Create(model);
if (newFurniture == null)
{
return null;
}
source.Furnitures.Add(newFurniture);
source.SaveFurnitures();
return newFurniture.GetViewModel;
}
public FurnitureViewModel? Update(FurnitureBindingModel model)
{
var furniture = source.Furnitures.FirstOrDefault(x => x.Id == model.Id);
if (furniture == null)
{
return null;
}
furniture.Update(model);
source.SaveFurnitures();
return furniture.GetViewModel;
}
public FurnitureViewModel? Delete(FurnitureBindingModel model)
{
var element = source.Furnitures.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
source.Furnitures.Remove(element);
source.SaveFurnitures();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,106 @@
using FurnitureAssemblyContracts.BindingModels;
using FurnitureAssemblyContracts.SearchModels;
using FurnitureAssemblyContracts.StoragesContracts;
using FurnitureAssemblyContracts.ViewModels;
using FurnitureAssemblyFileImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FurnitureAssemblyFileImplement.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 furniture = source.Furnitures.FirstOrDefault(x => x.Id == order.FurnitureId);
viewModel.FurnitureName = furniture?.FurnitureName;
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,94 @@
using FurnitureAssemblyContracts.BindingModels;
using FurnitureAssemblyContracts.SearchModels;
using FurnitureAssemblyContracts.StoragesContracts;
using FurnitureAssemblyContracts.ViewModels;
using FurnitureAssemblyFileImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FurnitureAssemblyFileImplement.Implements
{
public class WorkpieceStorage : IWorkpieceStorage
{
private readonly DataFileSingleton source;
public WorkpieceStorage()
{
source = DataFileSingleton.GetInstance();
}
public List<WorkpieceViewModel> GetFullList()
{
return source.Workpieces.Select(x => x.GetViewModel).ToList();
}
public List<WorkpieceViewModel> GetFilteredList(WorkpieceSearchModel model)
{
if (string.IsNullOrEmpty(model.WorkpieceName))
{
return new();
}
return source.Workpieces.Where(x => x.WorkpieceName.Contains(model.WorkpieceName)).Select(x => x.GetViewModel).ToList();
}
public WorkpieceViewModel? GetElement(WorkpieceSearchModel model)
{
if (string.IsNullOrEmpty(model.WorkpieceName) && !model.Id.HasValue)
{
return null;
}
return source.Workpieces.FirstOrDefault(x => (!string.IsNullOrEmpty(model.WorkpieceName) && x.WorkpieceName == model.WorkpieceName)
|| (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public WorkpieceViewModel? Insert(WorkpieceBindingModel model)
{
model.Id = source.Workpieces.Count > 0 ? source.Workpieces.Max(x => x.Id) + 1 : 1;
var newWorkpiece = Workpiece.Create(model);
if (newWorkpiece == null)
{
return null;
}
source.Workpieces.Add(newWorkpiece);
source.SaveWorkpieces();
return newWorkpiece.GetViewModel;
}
public WorkpieceViewModel? Update(WorkpieceBindingModel model)
{
var workPiece = source.Workpieces.FirstOrDefault(x => x.Id == model.Id);
if (workPiece == null)
{
return null;
}
workPiece.Update(model);
source.SaveWorkpieces();
return workPiece.GetViewModel;
}
public WorkpieceViewModel? Delete(WorkpieceBindingModel model)
{
var element = source.Workpieces.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
source.Workpieces.Remove(element);
source.SaveWorkpieces();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,105 @@
using FurnitureAssemblyContracts.BindingModels;
using FurnitureAssemblyContracts.ViewModels;
using FurnitureAssemblyDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace FurnitureAssemblyFileImplement.Models
{
internal class Furniture
{
public int Id { get; private set; }
public string FurnitureName { get; private set; } = string.Empty;
public double Price { get; private set; }
public Dictionary<int, int> Workpieces { get; private set; } = new();
private Dictionary<int, (IWorkpieceModel, int)>? _furnitureWorkpieces = null;
public Dictionary<int, (IWorkpieceModel, int)> FurnitureWorkpieces
{
get
{
if (_furnitureWorkpieces == null)
{
var source = DataFileSingleton.GetInstance();
_furnitureWorkpieces = Workpieces.ToDictionary(x => x.Key,
y => ((source.Workpieces.FirstOrDefault(z => z.Id == y.Key) as IWorkpieceModel)!, y.Value));
}
return _furnitureWorkpieces;
}
}
public static Furniture? Create(FurnitureBindingModel model)
{
if (model == null)
{
return null;
}
return new Furniture()
{
Id = model.Id,
FurnitureName = model.FurnitureName,
Price = model.Price,
Workpieces = model.FurnitureWorkpieces.ToDictionary(x => x.Key, x => x.Value.Item2)
};
}
public static Furniture? Create(XElement element)
{
if (element == null)
{
return null;
}
return new Furniture()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
FurnitureName = element.Element("FurnitureName")!.Value,
Price = Convert.ToDouble(element.Element("Price")!.Value),
Workpieces = element.Element("FurnitureWorkpieces")!.Elements("FurnitureWorkpieces").ToDictionary(
x => Convert.ToInt32(x.Element("Key")?.Value),
y => Convert.ToInt32(y.Element("Value")?.Value))
};
}
public void Update(FurnitureBindingModel model)
{
if (model == null)
{
return;
}
FurnitureName = model.FurnitureName;
Price = model.Price;
Workpieces = model.FurnitureWorkpieces.ToDictionary(x => x.Key, x => x.Value.Item2);
_furnitureWorkpieces = null;
}
public FurnitureViewModel GetViewModel => new()
{
Id = Id,
FurnitureName = FurnitureName,
Price = Price,
FurnitureWorkpieces = FurnitureWorkpieces
};
public XElement GetXElement => new("Furniture",
new XAttribute("Id", Id),
new XElement("FurnitureName", FurnitureName),
new XElement("Price", Price.ToString()),
new XElement("FurnitureWorkpieces", Workpieces.Select(
x => new XElement("FurnitureWorkpieces",
new XElement("Key", x.Key),
new XElement("Value", x.Value))
).ToArray()));
}
}

View File

@ -0,0 +1,99 @@
using FurnitureAssemblyContracts.BindingModels;
using FurnitureAssemblyContracts.ViewModels;
using FurnitureAssemblyDataModels.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace FurnitureAssemblyFileImplement.Models
{
internal class Order
{
public int Id { get; private set; }
public int FurnitureId { 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,
FurnitureId = model.FurnitureId,
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),
FurnitureId = Convert.ToInt32(element.Element("FurnitureId")!.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,
FurnitureId = FurnitureId,
Count = Count,
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement
};
public XElement GetXElement => new("Order",
new XAttribute("Id", Id),
new XElement("FurnitureId", FurnitureId.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

@ -0,0 +1,75 @@
using FurnitureAssemblyContracts.BindingModels;
using FurnitureAssemblyContracts.ViewModels;
using FurnitureAssemblyDataModels.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 FurnitureAssemblyFileImplement.Models
{
public class Workpiece : IWorkpieceModel
{
public int Id { get; private set; }
public string WorkpieceName { get; private set; } = string.Empty;
public double Cost { get; set; }
public static Workpiece? Create(WorkpieceBindingModel model)
{
if (model == null)
{
return null;
}
return new Workpiece()
{
Id = model.Id,
WorkpieceName = model.WorkpieceName,
Cost = model.Cost
};
}
public static Workpiece? Create(XElement element)
{
if (element == null)
{
return null;
}
return new Workpiece()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
WorkpieceName = element.Element("WorkpieceName")!.Value,
Cost = Convert.ToDouble(element.Element("Cost")!.Value)
};
}
public void Update(WorkpieceBindingModel model)
{
if (model == null)
{
return;
}
WorkpieceName = model.WorkpieceName;
Cost = model.Cost;
}
public WorkpieceViewModel GetViewModel => new()
{
Id = Id,
WorkpieceName = WorkpieceName,
Cost = Cost
};
public XElement GetXElement => new("Workpiece",
new XAttribute("Id", Id),
new XElement("WorkpieceName", WorkpieceName),
new XElement("Cost", Cost.ToString()));
}
}