слияние для 2 усл #9

Merged
mallafeev merged 1 commits from Lab2_Base into Lab2_Hard 2024-05-04 09:52:37 +04:00
13 changed files with 603 additions and 7 deletions
Showing only changes of commit cd38b2626b - Show all commits

View File

@ -3,17 +3,19 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.7.34202.233
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JewelryStore", "JewelryStore.csproj", "{AE4DC4C1-2347-4521-BE6F-C5B80BB7F0F0}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JewelryStore", "JewelryStore.csproj", "{AE4DC4C1-2347-4521-BE6F-C5B80BB7F0F0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JewelryStoreBusinessLogic", "..\JewelryStoreBusinessLogic\JewelryStoreBusinessLogic.csproj", "{7A3E84A4-6C9D-4C8D-BCCB-58F7BAEE52B9}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JewelryStoreBusinessLogic", "..\JewelryStoreBusinessLogic\JewelryStoreBusinessLogic.csproj", "{7A3E84A4-6C9D-4C8D-BCCB-58F7BAEE52B9}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JewelryStoreContracts", "..\JewelryStoreContracts\JewelryStoreContracts.csproj", "{F3E87835-779D-4974-96D4-51A6723C37D3}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JewelryStoreContracts", "..\JewelryStoreContracts\JewelryStoreContracts.csproj", "{F3E87835-779D-4974-96D4-51A6723C37D3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JewelryStoreListImplement", "..\JewelryStoreListImplement\JewelryStoreListImplement.csproj", "{76C0344A-4C9E-42D1-84DB-6D39D595B57D}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JewelryStoreListImplement", "..\JewelryStoreListImplement\JewelryStoreListImplement.csproj", "{76C0344A-4C9E-42D1-84DB-6D39D595B57D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JewerlyStoreDataModels", "..\JewerlyStoreDataModels\JewerlyStoreDataModels.csproj", "{4AD0B4DC-8F85-401B-8938-B49E4A3ACCF7}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JewerlyStoreDataModels", "..\JewerlyStoreDataModels\JewerlyStoreDataModels.csproj", "{4AD0B4DC-8F85-401B-8938-B49E4A3ACCF7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JewerlyStoreView", "..\JewerlyStoreView\JewerlyStoreView.csproj", "{F61608F4-B4AA-47D7-A6A0-74F9FFCF9A5B}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JewerlyStoreView", "..\JewerlyStoreView\JewerlyStoreView.csproj", "{F61608F4-B4AA-47D7-A6A0-74F9FFCF9A5B}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JewerlyStoreFileImplement", "..\JewerlyStoreFileImplement\JewerlyStoreFileImplement.csproj", "{CBCAFAE3-4CF6-43BB-A053-97FA6FD7A90B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -45,6 +47,10 @@ Global
{F61608F4-B4AA-47D7-A6A0-74F9FFCF9A5B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F61608F4-B4AA-47D7-A6A0-74F9FFCF9A5B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F61608F4-B4AA-47D7-A6A0-74F9FFCF9A5B}.Release|Any CPU.Build.0 = Release|Any CPU
{CBCAFAE3-4CF6-43BB-A053-97FA6FD7A90B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CBCAFAE3-4CF6-43BB-A053-97FA6FD7A90B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CBCAFAE3-4CF6-43BB-A053-97FA6FD7A90B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CBCAFAE3-4CF6-43BB-A053-97FA6FD7A90B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -0,0 +1,61 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JewerlyStoreFileImplement.Models;
using System.Xml.Linq;
namespace JewerlyStoreFileImplement
{
internal class DataFileSingleton
{
private static DataFileSingleton? instance;
private readonly string ComponentFileName = "Component.xml";
private readonly string OrderFileName = "Order.xml";
private readonly string JewelFileName = "Jewel.xml";
public List<Component> Components { get; private set; }
public List<Order> Orders { get; private set; }
public List<Jewel> Jewels { 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 SaveJewels() => SaveData(Jewels, JewelFileName, "Jewels", x => x.GetXElement);
public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement);
private DataFileSingleton()
{
Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!;
Jewels = LoadData(JewelFileName, "Jewel", x => Jewel.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,90 @@
using JewelryStoreContracts.BindingModels;
using JewelryStoreContracts.SearchModels;
using JewelryStoreContracts.StoragesContracts;
using JewelryStoreContracts.ViewModels;
using JewerlyStoreFileImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace JewerlyStoreFileImplement.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 element = source.Components.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
source.Components.Remove(element);
source.SaveComponents();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,85 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JewerlyStoreFileImplement.Models;
using JewelryStoreContracts.BindingModels;
using JewelryStoreContracts.SearchModels;
using JewelryStoreContracts.StoragesContracts;
using JewelryStoreContracts.ViewModels;
namespace JewerlyStoreFileImplement.Implements
{
public class JewelStorage : IJewelStorage
{
private readonly DataFileSingleton source;
public JewelStorage()
{
source = DataFileSingleton.GetInstance();
}
public JewelViewModel? GetElement(JewelSearchModel model)
{
if (string.IsNullOrEmpty(model.JewelName) && !model.Id.HasValue)
{
return null;
}
return source.Jewels.FirstOrDefault(x => (!string.IsNullOrEmpty(model.JewelName) && x.JewelName == model.JewelName) ||
(model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public List<JewelViewModel> GetFilteredList(JewelSearchModel model)
{
if (string.IsNullOrEmpty(model.JewelName))
{
return new();
}
return source.Jewels.Where(x => x.JewelName.Contains(model.JewelName)).Select(x => x.GetViewModel).ToList();
}
public List<JewelViewModel> GetFullList()
{
return source.Jewels.Select(x => x.GetViewModel).ToList();
}
public JewelViewModel? Insert(JewelBindingModel model)
{
model.Id = source.Jewels.Count > 0 ? source.Jewels.Max(x => x.Id) + 1 : 1;
var newJewel = Jewel.Create(model);
if (newJewel == null)
{
return null;
}
source.Jewels.Add(newJewel);
source.SaveJewels();
return newJewel.GetViewModel;
}
public JewelViewModel? Update(JewelBindingModel model)
{
var jewel = source.Jewels.FirstOrDefault(x => x.Id == model.Id);
if (jewel == null)
{
return null;
}
jewel.Update(model);
source.SaveJewels();
return jewel.GetViewModel;
}
public JewelViewModel? Delete(JewelBindingModel model)
{
var jewel = source.Jewels.FirstOrDefault(x => x.Id == model.Id);
if (jewel == null)
{
return null;
}
source.Jewels.Remove(jewel);
source.SaveJewels();
return jewel.GetViewModel;
}
}
}

View File

@ -0,0 +1,84 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JewerlyStoreFileImplement.Models;
using JewelryStoreContracts.BindingModels;
using JewelryStoreContracts.SearchModels;
using JewelryStoreContracts.StoragesContracts;
using JewelryStoreContracts.ViewModels;
namespace JewerlyStoreFileImplement.Implements
{
public class OrderStorage : IOrderStorage
{
private readonly DataFileSingleton _source;
public OrderStorage()
{
_source = DataFileSingleton.GetInstance();
}
public List<OrderViewModel> GetFullList()
{
return _source.Orders.Select(x => SAddJewelName(x.GetViewModel)).ToList();
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return new();
}
return _source.Orders.Where(x => x.Id == model.Id).Select(x => SAddJewelName(x.GetViewModel)).ToList();
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
return SAddJewelName(_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 SAddJewelName(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 SAddJewelName(order.GetViewModel);
}
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 SAddJewelName(element.GetViewModel);
}
return null;
}
public OrderViewModel SAddJewelName(OrderViewModel fullModel)
{
string? jewelName = _source.Jewels.FirstOrDefault(x => x.Id == fullModel?.JewelId)?.JewelName;
if (jewelName != null) fullModel.JewelName = jewelName;
return fullModel;
}
}
}

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

View File

@ -0,0 +1,69 @@
using JewelryStoreContracts.BindingModels;
using JewelryStoreContracts.ViewModels;
using JewerlyStoreDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace JewerlyStoreFileImplement.Models
{
public class Component : IComponentModel
{
public int Id { get; private set; }
public string ComponentName { get; private set; } = string.Empty;
public double Cost { get; 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)
};
}
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,93 @@
using JewelryStoreContracts.BindingModels;
using JewelryStoreContracts.ViewModels;
using JewerlyStoreDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace JewerlyStoreFileImplement.Models
{
public class Jewel : IJewelModel
{
public int Id { get; private set; }
public string JewelName { 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)>? _jewelComponents = null;
public Dictionary<int, (IComponentModel, int)> JewelComponents
{
get
{
if (_jewelComponents == null)
{
var source = DataFileSingleton.GetInstance();
_jewelComponents = Components.ToDictionary(x => x.Key, y =>
((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!, y.Value));
}
return _jewelComponents;
}
}
public static Jewel? Create(JewelBindingModel model)
{
if (model == null)
{
return null;
}
return new Jewel()
{
Id = model.Id,
JewelName = model.JewelName,
Price = model.Price,
Components = model.JewelComponents.ToDictionary(x => x.Key, x => x.Value.Item2)
};
}
public static Jewel? Create(XElement element)
{
if (element == null)
{
return null;
}
return new Jewel()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
JewelName = element.Element("JewelName")!.Value,
Price = Convert.ToDouble(element.Element("Price")!.Value),
Components = element.Element("JewelComponents")!.Elements("JewelComponent")
.ToDictionary(x => Convert.ToInt32(x.Element("Key")?.Value), x => Convert.ToInt32(x.Element("Value")?.Value))
};
}
public void Update(JewelBindingModel model)
{
if (model == null)
{
return;
}
JewelName = model.JewelName;
Price = model.Price;
Components = model.JewelComponents.ToDictionary(x => x.Key, x => x.Value.Item2);
_jewelComponents = null;
}
public JewelViewModel GetViewModel => new()
{
Id = Id,
JewelName = JewelName,
Price = Price,
JewelComponents = JewelComponents
};
public XElement GetXElement => new("Jewel",
new XAttribute("Id", Id),
new XElement("JewelName", JewelName),
new XElement("Price", Price.ToString()),
new XElement("JewelComponents", Components.Select(x =>
new XElement("JewelComponent", new XElement("Key", x.Key), new XElement("Value", x.Value)))
.ToArray()));
}
}

View File

@ -0,0 +1,91 @@
using JewelryStoreContracts.BindingModels;
using JewelryStoreContracts.ViewModels;
using JewerlyStoreDataModels.Enums;
using JewerlyStoreDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace JewerlyStoreFileImplement.Models
{
public class Order : IOrderModel
{
public int JewelId { 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 int Id { get; private set; }
public static Order? Create(OrderBindingModel? model)
{
if (model == null)
{
return null;
}
return new Order()
{
Id = model.Id,
JewelId = model.JewelId,
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),
JewelId = Convert.ToInt32(element.Element("JewelId")!.Value),
Sum = Convert.ToDouble(element.Element("Sum")!.Value),
Count = Convert.ToInt32(element.Element("Count")!.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,
JewelId = JewelId,
Count = Count,
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement
};
public XElement GetXElement => new("Order",
new XAttribute("Id", Id),
new XElement("JewelId", JewelId),
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

@ -98,6 +98,7 @@
Controls.Add(dataGridView);
Name = "FormComponents";
Text = "Компоненты";
Load += FormComponents_Load;
((System.ComponentModel.ISupportInitialize)dataGridView).EndInit();
ResumeLayout(false);
}

View File

@ -98,6 +98,7 @@
Controls.Add(dataGridView);
Name = "FormJewels";
Text = "Изделия";
Load += FormJewels_Load;
((System.ComponentModel.ISupportInitialize)dataGridView).EndInit();
ResumeLayout(false);
}

View File

@ -18,6 +18,7 @@
<ProjectReference Include="..\JewelryStoreContracts\JewelryStoreContracts.csproj" />
<ProjectReference Include="..\JewelryStoreListImplement\JewelryStoreListImplement.csproj" />
<ProjectReference Include="..\JewerlyStoreDataModels\JewerlyStoreDataModels.csproj" />
<ProjectReference Include="..\JewerlyStoreFileImplement\JewerlyStoreFileImplement.csproj" />
</ItemGroup>
</Project>

View File

@ -1,6 +1,6 @@
using JewelryStoreContracts.BusinessLogicsContracts;
using JewelryStoreContracts.StoragesContracts;
using JewelryStoreListImplement.Implements;
using JewerlyStoreFileImplement.Implements;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging;