PIbd22_Kuznetsov_A.V._lab2

This commit is contained in:
Anatoliy 2024-05-04 19:42:41 +04:00
parent d703eee695
commit 0d9216f028
14 changed files with 519 additions and 4 deletions

View File

@ -9,9 +9,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SewingDressesDataModels", "
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SewingDressesView", "SewingDressesView\SewingDressesView.csproj", "{E39E6495-2356-4AF8-8C24-C2C5E113341B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SewingDressesBusinessLogic", "SewingDressesBusinessLogic\SewingDressesBusinessLogic.csproj", "{3A0DDBD6-0799-4047-A316-C1EEC0F24DFE}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SewingDressesBusinessLogic", "SewingDressesBusinessLogic\SewingDressesBusinessLogic.csproj", "{3A0DDBD6-0799-4047-A316-C1EEC0F24DFE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SewingDressesListImplement", "SewingDressesListImplement\SewingDressesListImplement.csproj", "{0F2DC551-6221-4471-BBAA-C5C65E45E66C}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SewingDressesListImplement", "SewingDressesListImplement\SewingDressesListImplement.csproj", "{0F2DC551-6221-4471-BBAA-C5C65E45E66C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SewingDressesFileImplement", "SewingDressesFileImplement\SewingDressesFileImplement.csproj", "{340D02B6-5B03-47A2-8498-BB7DBBCB7F5B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -39,6 +41,10 @@ Global
{0F2DC551-6221-4471-BBAA-C5C65E45E66C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0F2DC551-6221-4471-BBAA-C5C65E45E66C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0F2DC551-6221-4471-BBAA-C5C65E45E66C}.Release|Any CPU.Build.0 = Release|Any CPU
{340D02B6-5B03-47A2-8498-BB7DBBCB7F5B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{340D02B6-5B03-47A2-8498-BB7DBBCB7F5B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{340D02B6-5B03-47A2-8498-BB7DBBCB7F5B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{340D02B6-5B03-47A2-8498-BB7DBBCB7F5B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -0,0 +1,49 @@
using SewingDressesFileImplement.Models;
using System.Xml.Linq;
namespace SewingDressesFileImplement
{
internal class DataFileSingleton
{
private static DataFileSingleton? instance;
private readonly string ComponentFileName = "Component.xml";
private readonly string OrderFileName = "Order.xml";
private readonly string DressFileName = "Dress.xml";
public List<Component> Components { get; private set; }
public List<Order> Orders { get; private set; }
public List<Dress> Dresses { get; private set;}
public static DataFileSingleton GetInstance()
{
if (instance == null)
{
instance = new DataFileSingleton();
}
return instance;
}
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);
}
}
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>();
}
public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement);
public void SaveDresses() => SaveData(Dresses, DressFileName, "Dresses", x => x.GetXElement);
public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement);
private DataFileSingleton()
{
Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!;
Dresses = LoadData(DressFileName, "Dress", x => Dress.Create(x)!)!;
Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!;
}
}
}

View File

@ -0,0 +1,73 @@
using SewingDressesContracts.BindingModels;
using SewingDressesContracts.SearchModels;
using SewingDressesContracts.StoragesContracts;
using SewingDressesContracts.ViewModels;
using SewingDressesFileImplement.Models;
namespace SewingDressesFileImplement.Implements
{
public class ComponentStorage : IComponentStorage
{
private readonly DataFileSingleton source;
public ComponentStorage()
{
source = DataFileSingleton.GetInstance();
}
public List<ComponentViewModel> GetFullList()
{
return source.Components.Select(x => x.GetViewModel).ToList();
}
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 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 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;
}
public ComponentViewModel? Delete(ComponentBindingModel model)
{
var component = source.Components.FirstOrDefault(x => x.Id == model.Id);
if (component == null)
{
return null;
}
source.Components.Remove(component);
source.SaveComponents();
return component.GetViewModel;
}
}
}

View File

@ -0,0 +1,71 @@
using SewingDressesContracts.BindingModels;
using SewingDressesContracts.SearchModels;
using SewingDressesContracts.StoragesContracts;
using SewingDressesContracts.ViewModels;
using SewingDressesDataModels.Models;
using SewingDressesFileImplement.Models;
namespace SewingDressesFileImplement.Implements
{
public class DressStorage : IDressStorage
{
private readonly DataFileSingleton source;
public DressStorage()
{
source = DataFileSingleton.GetInstance();
}
public List<DressViewModel> GetFullList()
{
return source.Dresses.Select(x => x.GetViewModel).ToList();
}
public List<DressViewModel> GetFilteredList(DressSearchModel model)
{
return source.Dresses.Where(x => x.Id == model.Id)
.Select(x => x.GetViewModel)
.ToList();
}
public DressViewModel? GetElement(DressSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
return source.Dresses.FirstOrDefault(x => (!string.IsNullOrEmpty(model.DressName) && x.DressName == model.DressName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public DressViewModel? Insert(DressBindingModel model)
{
model.Id = source.Dresses.Count > 0 ? source.Dresses.Max(x => x.Id) + 1 : 1;
var newDress = Dress.Create(model);
if (newDress == null)
{
return null;
}
source.Dresses.Add(newDress);
source.SaveDresses();
return newDress.GetViewModel;
}
public DressViewModel? Update(DressBindingModel model)
{
var component = source.Dresses.FirstOrDefault(x => x.Id == model.Id);
if (component == null)
{
return null;
}
component.Update(model);
source.SaveDresses();
return component.GetViewModel;
}
public DressViewModel? Delete(DressBindingModel model)
{
var component = source.Dresses.FirstOrDefault(x => x.Id == model.Id);
if (component == null)
{
return null;
}
source.Dresses.Remove(component);
source.SaveDresses();
return component.GetViewModel;
}
}
}

View File

@ -0,0 +1,77 @@
using SewingDressesContracts.BindingModels;
using SewingDressesContracts.SearchModels;
using SewingDressesContracts.StoragesContracts;
using SewingDressesContracts.ViewModels;
using SewingDressesDataModels.Models;
using SewingDressesFileImplement.Models;
namespace SewingDressesFileImplement.Implements
{
public class OrderStorage : IOrderStorage
{
private readonly DataFileSingleton source;
public OrderStorage()
{
source = DataFileSingleton.GetInstance();
}
public List<OrderViewModel> GetFullList()
{
return source.Orders.Select(x => AcessDressesStorage(x.GetViewModel)).ToList();
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
return source.Orders.Where(x => x.Id == model.Id)
.Select(x => AcessDressesStorage( x.GetViewModel))
.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;
}
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 component = source.Orders.FirstOrDefault(x => x.Id == model.Id);
if (component == null)
{
return null;
}
component.Update(model);
source.SaveOrders();
return component.GetViewModel;
}
public OrderViewModel? Delete(OrderBindingModel model)
{
var component = source.Orders.FirstOrDefault(x => x.Id == model.Id);
if (component == null)
{
return null;
}
source.Orders.Remove(component);
source.SaveOrders();
return component.GetViewModel;
}
public OrderViewModel AcessDressesStorage(OrderViewModel model)
{
var component = source.Dresses.FirstOrDefault(x => x.Id == model.DressId);
model.DressName = component.DressName;
return model;
}
}
}

View File

@ -0,0 +1,58 @@
using SewingDressesContracts.BindingModels;
using SewingDressesContracts.ViewModels;
using SewingDressesDataModels.Models;
using System.Xml.Linq;
namespace SewingDressesFileImplement.Models
{
public class Component : IComponentModel
{
public int Id { get; private set; }
public string ComponentName { get; private set; } = string.Empty;
public double Cost { get; private set; }
public static Component? Create(ComponentBindingModel model)
{
if (model == null)
{
return null;
}
return new Component()
{
Id = model.Id,
ComponentName = model.ComponentName,
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, new System.Globalization.CultureInfo("en-US"))
};
}
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

@ -0,0 +1,78 @@
using SewingDressesContracts.BindingModels;
using SewingDressesContracts.ViewModels;
using SewingDressesDataModels.Models;
using System.Xml.Linq;
namespace SewingDressesFileImplement.Models
{
public class Dress : IDressModel
{
public int Id { get; private set; }
public string DressName { 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)>? _dressComponents = null;
public Dictionary<int, (IComponentModel, int)> DressComponents
{
get
{
if (_dressComponents == null)
{
var source = DataFileSingleton.GetInstance();
_dressComponents = Components.ToDictionary(x => x.Key, y => ((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!, y.Value));
}
return _dressComponents;
}
}
public static Dress? Create(DressBindingModel model)
{
if (model == null)
{
return null;
}
return new Dress()
{
Id = model.Id,
DressName = model.DressName,
Price = model.Price,
Components = model.DressComponents.ToDictionary(x => x.Key, x => x.Value.Item2)
};
}
public static Dress? Create(XElement element)
{
if (element == null)
{ return null; }
return new Dress()
{
Id = Convert.ToInt32(element.Attribute("Id")?.Value),
DressName = element.Element("DressName")!.Value,
Price = Convert.ToDouble(element.Element("Price")!.Value, new System.Globalization.CultureInfo("en-US")),
Components = element.Element("DressComponents")!.Elements("DressComponent").ToDictionary(x =>Convert.ToInt32(x.Element("Key")?.Value), x => Convert.ToInt32(x.Element("Value")?.Value))
};
}
public void Update(DressBindingModel model)
{
if (model == null)
{
return;
}
DressName = model.DressName;
Price = model.Price;
Components = model.DressComponents.ToDictionary(x => x.Key, x => x.Value.Item2);
_dressComponents = null;
}
public DressViewModel GetViewModel => new()
{
Id = Id,
DressName = DressName,
Price = Price,
DressComponents = DressComponents
};
public XElement GetXElement => new("Dress",
new XAttribute("Id", Id),
new XElement("DressName", DressName),
new XElement("Price", Price),
new XElement("DressComponents", Components.Select(x => new XElement("DressComponent", new XElement("Key", x.Key), new XElement("Value", x.Value))).ToArray()));
}
}

View File

@ -0,0 +1,82 @@
using SewingDressesContracts.BindingModels;
using SewingDressesContracts.ViewModels;
using SewingDressesDataModels.Enums;
using SewingDressesDataModels.Models;
using System.Data;
using System.Xml.Linq;
namespace SewingDressesFileImplement.Models
{
public class Order : IOrderModel
{
public int Id { get; private set; }
public int DressId { get; private set; }
public int Count { get; private set; }
public double Sum { get; private set; }
public OrderStatus Status { get; private set; }
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,
DressId = model.DressId,
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
DateCreate = model.DateCreate
};
}
public static Order? Create(XElement element)
{
if (element == null)
{
return null;
}
var dateImplement = element.Element("DateImplement")?.Value;
return new Order()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
DressId = Convert.ToInt32(element.Element("DressId")!.Value),
Count = Convert.ToInt32(element.Element("Count")!.Value),
Sum = Convert.ToDouble(element.Element("Sum")!.Value, new System.Globalization.CultureInfo("en-US")),
Status = (OrderStatus)Convert.ToInt32(element.Element("Status")!.Value),
DateCreate = Convert.ToDateTime(element.Element("DateCreate")?.Value),
DateImplement = string.IsNullOrEmpty(dateImplement) ? null : Convert.ToDateTime(dateImplement)
};
}
public void Update(OrderBindingModel model)
{
if (model == null)
{
return;
}
Status = model.Status;
DateImplement = model.DateImplement;
}
public OrderViewModel GetViewModel => new()
{
Id = Id,
DressId = DressId,
Count = Count,
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement
};
public XElement GetXElement => new("Order",
new XAttribute("Id", Id),
new XElement("DressId", DressId),
new XElement("Count", Count),
new XElement("Sum", Sum),
new XElement("Status", Status - OrderStatus.Принят),
new XElement("DateCreate", DateCreate),
new XElement("DateImplement", DateImplement));
}
}

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="..\SewingDressesContracts\SewingDressesContracts.csproj" />
<ProjectReference Include="..\SewingDressesDataModels\SewingDressesDataModels.csproj" />
</ItemGroup>
</Project>

View File

@ -99,8 +99,8 @@ namespace SewingDressesView
{
dataGridView.DataSource = list;
dataGridView.Columns["Id"].Visible = false;
dataGridView.Columns["DressComponents"].Visible = false;
dataGridView.Columns["DressName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridView.Columns["DressComponents"].Visible = false;
}
_logger.LogInformation("Load components");
}

View File

@ -148,6 +148,7 @@
MainMenuStrip = menuStrip1;
Name = "MainForm";
Text = "Магазин пошива одежды";
Load += MainForm_Load;
((System.ComponentModel.ISupportInitialize)dataGridView).EndInit();
menuStrip1.ResumeLayout(false);
menuStrip1.PerformLayout();

View File

@ -141,5 +141,10 @@ namespace SewingDressesView
{
LoadData();
}
private void MainForm_Load(object sender, EventArgs e)
{
LoadData();
}
}
}

View File

@ -1,7 +1,7 @@
using SewingDressesBusinessLogic.BusinessLogic;
using SewingDressesContracts.BusinessLogicsContracts;
using SewingDressesContracts.StoragesContracts;
using SewingDressesListImplement.Implements;
using SewingDressesFileImplement.Implements;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging;

View File

@ -11,6 +11,7 @@
<ItemGroup>
<ProjectReference Include="..\SewingDressesBusinessLogic\SewingDressesBusinessLogic.csproj" />
<ProjectReference Include="..\SewingDressesContracts\SewingDressesContracts.csproj" />
<ProjectReference Include="..\SewingDressesFileImplement\SewingDressesFileImplement.csproj" />
<ProjectReference Include="..\SewingDressesListImplement\SewingDressesListImplement.csproj" />
</ItemGroup>