Готовая 2 лаб. работа.
This commit is contained in:
parent
63ae753b2a
commit
c24396e2b0
@ -3,15 +3,17 @@ Microsoft Visual Studio Solution File, Format Version 12.00
|
|||||||
# Visual Studio Version 17
|
# Visual Studio Version 17
|
||||||
VisualStudioVersion = 17.3.32819.101
|
VisualStudioVersion = 17.3.32819.101
|
||||||
MinimumVisualStudioVersion = 10.0.40219.1
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JewelryStore", "JewelryStore\JewelryStore.csproj", "{187BBC53-52AA-4C47-B98B-E119AE64ADA4}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JewelryStore", "JewelryStore\JewelryStore.csproj", "{187BBC53-52AA-4C47-B98B-E119AE64ADA4}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JewelryStoreDataModels", "JewelryShopDataModels\JewelryStoreDataModels.csproj", "{C6D79969-6DC9-4F60-BCC4-A679CB472595}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JewelryStoreDataModels", "JewelryShopDataModels\JewelryStoreDataModels.csproj", "{C6D79969-6DC9-4F60-BCC4-A679CB472595}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JewelryStoreContracts", "JewelryStoreContracts\JewelryStoreContracts.csproj", "{F7C956E1-26FC-4914-9ABC-71A16F88B7E8}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JewelryStoreContracts", "JewelryStoreContracts\JewelryStoreContracts.csproj", "{F7C956E1-26FC-4914-9ABC-71A16F88B7E8}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JewelryStoreBusinessLogic", "JewelryStoreBusinessLogic\JewelryStoreBusinessLogic.csproj", "{B4D1E829-A99F-47D2-B06D-5042CE058CA9}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JewelryStoreBusinessLogic", "JewelryStoreBusinessLogic\JewelryStoreBusinessLogic.csproj", "{B4D1E829-A99F-47D2-B06D-5042CE058CA9}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JewelryStoreListImplement", "JewelryStoreListImplement\JewelryStoreListImplement.csproj", "{12C6B5CD-CE5C-4095-91B8-53A5CB08E780}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JewelryStoreListImplement", "JewelryStoreListImplement\JewelryStoreListImplement.csproj", "{12C6B5CD-CE5C-4095-91B8-53A5CB08E780}"
|
||||||
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JewelryStoreFileImplement", "JewelryStoreFileImplement\JewelryStoreFileImplement.csproj", "{12AA98A9-D1DA-4DC3-B98C-105004B8498A}"
|
||||||
EndProject
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
@ -39,6 +41,10 @@ Global
|
|||||||
{12C6B5CD-CE5C-4095-91B8-53A5CB08E780}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{12C6B5CD-CE5C-4095-91B8-53A5CB08E780}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{12C6B5CD-CE5C-4095-91B8-53A5CB08E780}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{12C6B5CD-CE5C-4095-91B8-53A5CB08E780}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{12C6B5CD-CE5C-4095-91B8-53A5CB08E780}.Release|Any CPU.Build.0 = Release|Any CPU
|
{12C6B5CD-CE5C-4095-91B8-53A5CB08E780}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{12AA98A9-D1DA-4DC3-B98C-105004B8498A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{12AA98A9-D1DA-4DC3-B98C-105004B8498A}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{12AA98A9-D1DA-4DC3-B98C-105004B8498A}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{12AA98A9-D1DA-4DC3-B98C-105004B8498A}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
11
JewelryStore/JewelryStore/FormComponents.Designer.cs
generated
11
JewelryStore/JewelryStore/FormComponents.Designer.cs
generated
@ -38,13 +38,20 @@
|
|||||||
//
|
//
|
||||||
// dataGridView
|
// dataGridView
|
||||||
//
|
//
|
||||||
|
this.dataGridView.AllowUserToAddRows = false;
|
||||||
|
this.dataGridView.AllowUserToDeleteRows = false;
|
||||||
this.dataGridView.BackgroundColor = System.Drawing.SystemColors.Window;
|
this.dataGridView.BackgroundColor = System.Drawing.SystemColors.Window;
|
||||||
this.dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
|
this.dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
|
||||||
this.dataGridView.Location = new System.Drawing.Point(12, 11);
|
this.dataGridView.Dock = System.Windows.Forms.DockStyle.Left;
|
||||||
|
this.dataGridView.Location = new System.Drawing.Point(0, 0);
|
||||||
|
this.dataGridView.MultiSelect = false;
|
||||||
this.dataGridView.Name = "dataGridView";
|
this.dataGridView.Name = "dataGridView";
|
||||||
|
this.dataGridView.ReadOnly = true;
|
||||||
|
this.dataGridView.RowHeadersVisible = false;
|
||||||
this.dataGridView.RowHeadersWidth = 62;
|
this.dataGridView.RowHeadersWidth = 62;
|
||||||
this.dataGridView.RowTemplate.Height = 33;
|
this.dataGridView.RowTemplate.Height = 33;
|
||||||
this.dataGridView.Size = new System.Drawing.Size(525, 427);
|
this.dataGridView.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
|
||||||
|
this.dataGridView.Size = new System.Drawing.Size(525, 450);
|
||||||
this.dataGridView.TabIndex = 0;
|
this.dataGridView.TabIndex = 0;
|
||||||
//
|
//
|
||||||
// buttonAdd
|
// buttonAdd
|
||||||
|
@ -87,6 +87,7 @@
|
|||||||
//
|
//
|
||||||
this.textBoxSum.Location = new System.Drawing.Point(134, 104);
|
this.textBoxSum.Location = new System.Drawing.Point(134, 104);
|
||||||
this.textBoxSum.Name = "textBoxSum";
|
this.textBoxSum.Name = "textBoxSum";
|
||||||
|
this.textBoxSum.ReadOnly = true;
|
||||||
this.textBoxSum.Size = new System.Drawing.Size(291, 31);
|
this.textBoxSum.Size = new System.Drawing.Size(291, 31);
|
||||||
this.textBoxSum.TabIndex = 5;
|
this.textBoxSum.TabIndex = 5;
|
||||||
//
|
//
|
||||||
|
2
JewelryStore/JewelryStore/FormJewel.Designer.cs
generated
2
JewelryStore/JewelryStore/FormJewel.Designer.cs
generated
@ -57,6 +57,7 @@
|
|||||||
this.Count});
|
this.Count});
|
||||||
this.dataGridView.Location = new System.Drawing.Point(6, 25);
|
this.dataGridView.Location = new System.Drawing.Point(6, 25);
|
||||||
this.dataGridView.Name = "dataGridView";
|
this.dataGridView.Name = "dataGridView";
|
||||||
|
this.dataGridView.RowHeadersVisible = false;
|
||||||
this.dataGridView.RowHeadersWidth = 62;
|
this.dataGridView.RowHeadersWidth = 62;
|
||||||
this.dataGridView.RowTemplate.Height = 33;
|
this.dataGridView.RowTemplate.Height = 33;
|
||||||
this.dataGridView.Size = new System.Drawing.Size(468, 325);
|
this.dataGridView.Size = new System.Drawing.Size(468, 325);
|
||||||
@ -112,6 +113,7 @@
|
|||||||
//
|
//
|
||||||
// textBoxPrice
|
// textBoxPrice
|
||||||
//
|
//
|
||||||
|
this.textBoxPrice.Enabled = false;
|
||||||
this.textBoxPrice.Location = new System.Drawing.Point(124, 57);
|
this.textBoxPrice.Location = new System.Drawing.Point(124, 57);
|
||||||
this.textBoxPrice.Name = "textBoxPrice";
|
this.textBoxPrice.Name = "textBoxPrice";
|
||||||
this.textBoxPrice.Size = new System.Drawing.Size(267, 31);
|
this.textBoxPrice.Size = new System.Drawing.Size(267, 31);
|
||||||
|
@ -66,13 +66,4 @@
|
|||||||
<metadata name="Count.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
|
<metadata name="Count.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
|
||||||
<value>True</value>
|
<value>True</value>
|
||||||
</metadata>
|
</metadata>
|
||||||
<metadata name="JewelId.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
|
|
||||||
<value>True</value>
|
|
||||||
</metadata>
|
|
||||||
<metadata name="ComponentName.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
|
|
||||||
<value>True</value>
|
|
||||||
</metadata>
|
|
||||||
<metadata name="Count.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
|
|
||||||
<value>True</value>
|
|
||||||
</metadata>
|
|
||||||
</root>
|
</root>
|
@ -56,6 +56,7 @@
|
|||||||
//
|
//
|
||||||
// comboBoxComponent
|
// comboBoxComponent
|
||||||
//
|
//
|
||||||
|
this.comboBoxComponent.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
|
||||||
this.comboBoxComponent.FormattingEnabled = true;
|
this.comboBoxComponent.FormattingEnabled = true;
|
||||||
this.comboBoxComponent.Location = new System.Drawing.Point(148, 18);
|
this.comboBoxComponent.Location = new System.Drawing.Point(148, 18);
|
||||||
this.comboBoxComponent.Name = "comboBoxComponent";
|
this.comboBoxComponent.Name = "comboBoxComponent";
|
||||||
|
4
JewelryStore/JewelryStore/FormMain.Designer.cs
generated
4
JewelryStore/JewelryStore/FormMain.Designer.cs
generated
@ -78,12 +78,16 @@
|
|||||||
//
|
//
|
||||||
// dataGridView
|
// dataGridView
|
||||||
//
|
//
|
||||||
|
this.dataGridView.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
|
||||||
|
| System.Windows.Forms.AnchorStyles.Left)
|
||||||
|
| System.Windows.Forms.AnchorStyles.Right)));
|
||||||
this.dataGridView.BackgroundColor = System.Drawing.SystemColors.Window;
|
this.dataGridView.BackgroundColor = System.Drawing.SystemColors.Window;
|
||||||
this.dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
|
this.dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
|
||||||
this.dataGridView.Location = new System.Drawing.Point(12, 36);
|
this.dataGridView.Location = new System.Drawing.Point(12, 36);
|
||||||
this.dataGridView.Name = "dataGridView";
|
this.dataGridView.Name = "dataGridView";
|
||||||
this.dataGridView.RowHeadersWidth = 62;
|
this.dataGridView.RowHeadersWidth = 62;
|
||||||
this.dataGridView.RowTemplate.Height = 33;
|
this.dataGridView.RowTemplate.Height = 33;
|
||||||
|
this.dataGridView.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
|
||||||
this.dataGridView.Size = new System.Drawing.Size(851, 402);
|
this.dataGridView.Size = new System.Drawing.Size(851, 402);
|
||||||
this.dataGridView.TabIndex = 1;
|
this.dataGridView.TabIndex = 1;
|
||||||
//
|
//
|
||||||
|
@ -22,7 +22,14 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\JewelryStoreBusinessLogic\JewelryStoreBusinessLogic.csproj" />
|
<ProjectReference Include="..\JewelryStoreBusinessLogic\JewelryStoreBusinessLogic.csproj" />
|
||||||
<ProjectReference Include="..\JewelryStoreContracts\JewelryStoreContracts.csproj" />
|
<ProjectReference Include="..\JewelryStoreContracts\JewelryStoreContracts.csproj" />
|
||||||
|
<ProjectReference Include="..\JewelryStoreFileImplement\JewelryStoreFileImplement.csproj" />
|
||||||
<ProjectReference Include="..\JewelryStoreListImplement\JewelryStoreListImplement.csproj" />
|
<ProjectReference Include="..\JewelryStoreListImplement\JewelryStoreListImplement.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<None Update="nlog.config">
|
||||||
|
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||||
|
</None>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
@ -1,6 +1,6 @@
|
|||||||
using JewelryStoreContracts.BusinessLogicsContracts;
|
using JewelryStoreContracts.BusinessLogicsContracts;
|
||||||
using JewelryStoreContracts.StoragesContracts;
|
using JewelryStoreContracts.StoragesContracts;
|
||||||
using JewelryStoreListImplement.Implements;
|
using JewelryStoreFileImplement.Implements;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using NLog.Extensions.Logging;
|
using NLog.Extensions.Logging;
|
||||||
|
55
JewelryStore/JewelryStoreFileImplement/DataFileSingleton.cs
Normal file
55
JewelryStore/JewelryStoreFileImplement/DataFileSingleton.cs
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
using JewelryStoreFileImplement.Models;
|
||||||
|
using System.Xml.Linq;
|
||||||
|
|
||||||
|
namespace JewelryStoreFileImplement
|
||||||
|
{
|
||||||
|
public 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,90 @@
|
|||||||
|
using JewelryStoreContracts.BindingModels;
|
||||||
|
using JewelryStoreContracts.SearchModels;
|
||||||
|
using JewelryStoreContracts.StoragesContracts;
|
||||||
|
using JewelryStoreContracts.ViewModels;
|
||||||
|
using JewelryStoreFileImplement.Models;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace JewelryStoreFileImplement.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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,85 @@
|
|||||||
|
using JewelryStoreContracts.BindingModels;
|
||||||
|
using JewelryStoreContracts.SearchModels;
|
||||||
|
using JewelryStoreContracts.StoragesContracts;
|
||||||
|
using JewelryStoreContracts.ViewModels;
|
||||||
|
using JewelryStoreFileImplement.Models;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace JewelryStoreFileImplement.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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,83 @@
|
|||||||
|
using JewelryStoreContracts.BindingModels;
|
||||||
|
using JewelryStoreContracts.SearchModels;
|
||||||
|
using JewelryStoreContracts.StoragesContracts;
|
||||||
|
using JewelryStoreContracts.ViewModels;
|
||||||
|
using JewelryStoreFileImplement.Models;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace JewelryStoreFileImplement.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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net6.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\JewelryShopDataModels\JewelryStoreDataModels.csproj" />
|
||||||
|
<ProjectReference Include="..\JewelryStoreContracts\JewelryStoreContracts.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
69
JewelryStore/JewelryStoreFileImplement/Models/Component.cs
Normal file
69
JewelryStore/JewelryStoreFileImplement/Models/Component.cs
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
using JewelryStoreContracts.BindingModels;
|
||||||
|
using JewelryStoreContracts.ViewModels;
|
||||||
|
using JewelryStoreDataModels.Models;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using System.Xml.Linq;
|
||||||
|
|
||||||
|
namespace JewelryStoreFileImplement.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()));
|
||||||
|
}
|
||||||
|
}
|
93
JewelryStore/JewelryStoreFileImplement/Models/Jewel.cs
Normal file
93
JewelryStore/JewelryStoreFileImplement/Models/Jewel.cs
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
using JewelryStoreContracts.BindingModels;
|
||||||
|
using JewelryStoreContracts.ViewModels;
|
||||||
|
using JewelryStoreDataModels.Models;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using System.Xml.Linq;
|
||||||
|
|
||||||
|
namespace JewelryStoreFileImplement.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()));
|
||||||
|
}
|
||||||
|
}
|
95
JewelryStore/JewelryStoreFileImplement/Models/Order.cs
Normal file
95
JewelryStore/JewelryStoreFileImplement/Models/Order.cs
Normal file
@ -0,0 +1,95 @@
|
|||||||
|
using JewelryStoreContracts.BindingModels;
|
||||||
|
using JewelryStoreContracts.ViewModels;
|
||||||
|
using JewelryStoreDataModels.Enums;
|
||||||
|
using JewelryStoreDataModels.Models;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using System.Xml.Linq;
|
||||||
|
|
||||||
|
namespace JewelryStoreFileImplement.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;
|
||||||
|
}
|
||||||
|
JewelId = model.JewelId;
|
||||||
|
Count = model.Count;
|
||||||
|
Sum = model.Sum;
|
||||||
|
Status = model.Status;
|
||||||
|
DateCreate = model.DateCreate;
|
||||||
|
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()));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user