PIbd-21. Ivanov I. I. SecondWork #2

Closed
eegov wants to merge 1 commits from LabWork02 into LabWork01
11 changed files with 458 additions and 5 deletions

View File

@ -5,13 +5,15 @@ VisualStudioVersion = 17.2.32616.157
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbstractShopView", "AbstractShopView\AbstractShopView.csproj", "{30272FA6-ABB7-4436-AFC6-50478DF2B878}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AbstractShopDataModels", "AbstractShopDataModels\AbstractShopDataModels.csproj", "{E47BFE5D-69E3-4A3F-85BE-251F10EAFA02}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbstractShopDataModels", "AbstractShopDataModels\AbstractShopDataModels.csproj", "{E47BFE5D-69E3-4A3F-85BE-251F10EAFA02}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AbstractShopContracts", "AbstractShopContracts\AbstractShopContracts.csproj", "{9AD66AC2-51DF-4E16-83E9-35C1CA19E774}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbstractShopContracts", "AbstractShopContracts\AbstractShopContracts.csproj", "{9AD66AC2-51DF-4E16-83E9-35C1CA19E774}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AbstractShopListImplement", "AbstractShopListImplement\AbstractShopListImplement.csproj", "{74283CE4-7CE6-42AC-AA8D-EE4497E08D61}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbstractShopListImplement", "AbstractShopListImplement\AbstractShopListImplement.csproj", "{74283CE4-7CE6-42AC-AA8D-EE4497E08D61}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AbstractShopBusinessLogic", "AbstractShopBusinessLogic\AbstractShopBusinessLogic.csproj", "{9A10E59F-081B-4C4D-AB16-15A777B66502}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbstractShopBusinessLogic", "AbstractShopBusinessLogic\AbstractShopBusinessLogic.csproj", "{9A10E59F-081B-4C4D-AB16-15A777B66502}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AbstractShopFileImplement", "AbstractShopFileImplement\AbstractShopFileImplement.csproj", "{3ECD0091-7E52-48C4-9529-D0BE6D6EF1DE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -39,6 +41,10 @@ Global
{9A10E59F-081B-4C4D-AB16-15A777B66502}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9A10E59F-081B-4C4D-AB16-15A777B66502}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9A10E59F-081B-4C4D-AB16-15A777B66502}.Release|Any CPU.Build.0 = Release|Any CPU
{3ECD0091-7E52-48C4-9529-D0BE6D6EF1DE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3ECD0091-7E52-48C4-9529-D0BE6D6EF1DE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3ECD0091-7E52-48C4-9529-D0BE6D6EF1DE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3ECD0091-7E52-48C4-9529-D0BE6D6EF1DE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

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

View File

@ -0,0 +1,61 @@
using AbstractShopFileImplement.Models;
using System.Xml.Linq;
namespace AbstractShopFileImplement
{
internal class DataFileSingleton
{
private static DataFileSingleton? instance;
private readonly string ComponentFileName = "Component.xml";
private readonly string OrderFileName = "Order.xml";
private readonly string ProductFileName = "Product.xml";
public List<Component> Components { get; private set; }
public List<Order> Orders { get; private set; }
public List<Product> Products { 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 SaveProducts() => SaveData(Products, ProductFileName, "Products", x => x.GetXElement);
public void SaveOrders() { }
private DataFileSingleton()
{
Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!;
Products = LoadData(ProductFileName, "Product", x => Product.Create(x)!)!;
Orders = new List<Order>();
}
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,86 @@
using AbstractShopContracts.BindingModels;
using AbstractShopContracts.SearchModels;
using AbstractShopContracts.StoragesContracts;
using AbstractShopContracts.ViewModels;
using AbstractShopFileImplement.Models;
namespace AbstractShopFileImplement.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,40 @@
using AbstractShopContracts.BindingModels;
using AbstractShopContracts.SearchModels;
using AbstractShopContracts.StoragesContracts;
using AbstractShopContracts.ViewModels;
namespace AbstractShopFileImplement.Implements
{
public class OrderStorage : IOrderStorage
{
public OrderViewModel? Delete(OrderBindingModel model)
{
throw new NotImplementedException();
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
throw new NotImplementedException();
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
throw new NotImplementedException();
}
public List<OrderViewModel> GetFullList()
{
throw new NotImplementedException();
}
public OrderViewModel? Insert(OrderBindingModel model)
{
throw new NotImplementedException();
}
public OrderViewModel? Update(OrderBindingModel model)
{
throw new NotImplementedException();
}
}
}

View File

@ -0,0 +1,40 @@
using AbstractShopContracts.BindingModels;
using AbstractShopContracts.SearchModels;
using AbstractShopContracts.StoragesContracts;
using AbstractShopContracts.ViewModels;
namespace AbstractShopFileImplement.Implements
{
public class ProductStorage : IProductStorage
{
public ProductViewModel? Delete(ProductBindingModel model)
{
throw new NotImplementedException();
}
public ProductViewModel? GetElement(ProductSearchModel model)
{
throw new NotImplementedException();
}
public List<ProductViewModel> GetFilteredList(ProductSearchModel model)
{
throw new NotImplementedException();
}
public List<ProductViewModel> GetFullList()
{
throw new NotImplementedException();
}
public ProductViewModel? Insert(ProductBindingModel model)
{
throw new NotImplementedException();
}
public ProductViewModel? Update(ProductBindingModel model)
{
throw new NotImplementedException();
}
}
}

View File

@ -0,0 +1,66 @@
using AbstractShopContracts.BindingModels;
using AbstractShopContracts.ViewModels;
using AbstractShopDataModels.Models;
using System.Xml.Linq;
namespace AbstractShopFileImplement.Models
{
internal 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,46 @@
using AbstractShopContracts.BindingModels;
using AbstractShopContracts.ViewModels;
using AbstractShopDataModels.Enums;
using AbstractShopDataModels.Models;
using System.Xml.Linq;
namespace AbstractShopFileImplement.Models
{
internal class Order : IOrderModel
{
public int ProductId => throw new NotImplementedException();
public int Count => throw new NotImplementedException();
public double Sum => throw new NotImplementedException();
public OrderStatus Status => throw new NotImplementedException();
public DateTime DateCreate => throw new NotImplementedException();
public DateTime? DateImplement => throw new NotImplementedException();
public int Id => throw new NotImplementedException();
public static Order? Create(OrderBindingModel? model)
{
return new Order();
}
public static Order? Create(XElement element)
{
return new Order();
}
public void Update(OrderBindingModel? model)
{
}
public OrderViewModel GetViewModel => new()
{
};
public XElement GetXElement => new("Order");
}
}

View File

@ -0,0 +1,93 @@
using AbstractShopContracts.BindingModels;
using AbstractShopContracts.ViewModels;
using AbstractShopDataModels.Models;
using System.Xml.Linq;
namespace AbstractShopFileImplement.Models
{
internal class Product : IProductModel
{
public int Id { get; private set; }
public string ProductName { 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)>? _productComponents = null;
public Dictionary<int, (IComponentModel, int)> ProductComponents
{
get
{
if (_productComponents == null)
{
var source = DataFileSingleton.GetInstance();
_productComponents = Components.ToDictionary(x => x.Key, y => ((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!, y.Value));
}
return _productComponents;
}
}
public static Product? Create(ProductBindingModel? model)
{
if (model == null)
{
return null;
}
return new Product()
{
Id = model.Id,
ProductName = model.ProductName,
Price = model.Price,
Components = model.ProductComponents.ToDictionary(x => x.Key, x => x.Value.Item2)
};
}
public static Product? Create(XElement element)
{
if (element == null)
{
return null;
}
return new Product()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
ProductName = element.Element("ProductName")!.Value,
Price = Convert.ToDouble(element.Element("Price")!.Value),
Components = element.Element("ProductComponents")!.Elements("ProductComponent")
.ToDictionary(x => Convert.ToInt32(x.Element("Key")?.Value), x => Convert.ToInt32(x.Element("Value")?.Value))
};
}
public void Update(ProductBindingModel? model)
{
if (model == null)
{
return;
}
ProductName = model.ProductName;
Price = model.Price;
Components = model.ProductComponents.ToDictionary(x => x.Key, x => x.Value.Item2);
_productComponents = null;
}
public ProductViewModel GetViewModel => new()
{
Id = Id,
ProductName = ProductName,
Price = Price,
ProductComponents = ProductComponents
};
public XElement GetXElement => new("Product",
new XAttribute("Id", Id),
new XElement("ProductName", ProductName),
new XElement("Price", Price.ToString()),
new XElement("ProductComponents", Components.Select(x => new XElement("ProductComponent",
new XElement("Key", x.Key),
new XElement("Value", x.Value)))
.ToArray()));
}
}

View File

@ -28,6 +28,7 @@
<ItemGroup>
<ProjectReference Include="..\AbstractShopBusinessLogic\AbstractShopBusinessLogic.csproj" />
<ProjectReference Include="..\AbstractShopContracts\AbstractShopContracts.csproj" />
<ProjectReference Include="..\AbstractShopFileImplement\AbstractShopFileImplement.csproj" />
<ProjectReference Include="..\AbstractShopListImplement\AbstractShopListImplement.csproj" />
</ItemGroup>

View File

@ -1,7 +1,7 @@
using AbstractShopBusinessLogic.BusinessLogics;
using AbstractShopContracts.BusinessLogicsContracts;
using AbstractShopContracts.StoragesContracts;
using AbstractShopListImplement.Implements;
using AbstractShopFileImplement.Implements;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging;