This commit is contained in:
GokaPek 2024-03-12 21:53:02 +04:00
parent d8e8a7d2c4
commit 4cc264503a
7 changed files with 342 additions and 2 deletions

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

View File

@ -0,0 +1,62 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using AbstractLawFirmFileImplement.Models;
namespace AbstractLawFirmFileImpliment
{
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<Document> Documents { 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)!)!;
Documents = LoadData(ProductFileName, "Product", x =>
Document.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,90 @@
using AbstractLawFirmContracts.BindingModels.BindingModels;
using AbstractLawFirmContracts.SearchModels;
using AbstractLawFirmContracts.ViewModels;
using AbstractLawFirmFileImplement.Models;
using AbstractLawFirmFileImpliment;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AbstractLawFirmFileImplement.Implements
{
public class ComponentStorage
{
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,64 @@
using AbstractLawFirmContracts.BindingModels.BindingModels;
using AbstractLawFirmContracts.ViewModels;
using AbstractLawFirmDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace AbstractLawFirmFileImplement.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,103 @@
using AbstractLawFirmContracts.BindingModels;
using AbstractLawFirmContracts.ViewModels;
using AbstractLawFirmDataModels.Models;
using AbstractLawFirmFileImpliment;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace AbstractLawFirmFileImplement.Models
{
public class Document : IDocumentModel
{
public int Id { get; private set; }
public string DocumentName { 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 Document? Create(DocumentBindingModel model)
{
if (model == null)
{
return null;
}
return new Document()
{
Id = model.Id,
DocumentName = model.DocumentName,
Price = model.Price,
Components = model.DocumentComponents.ToDictionary(x => x.Key, x
=> x.Value.Item2)
};
}
public static Document? Create(XElement element)
{
if (element == null)
{
return null;
}
return new Document()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
DocumentName = 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(DocumentBindingModel model)
{
if (model == null)
{
return;
}
DocumentName = model.DocumentName;
Price = model.Price;
Components = model.DocumentComponents.ToDictionary(x => x.Key, x =>
x.Value.Item2);
_productComponents = null;
}
public DocumentViewModel GetViewModel => new()
{
Id = Id,
DocumentName = DocumentName,
Price = Price,
DocumentComponents = ProductComponents
};
public XElement GetXElement => new("Product",
new XAttribute("Id", Id),
new XElement("ProductName", DocumentName),
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()));
public Dictionary<int, (IComponentModel, int)> DocumentComponents => throw new NotImplementedException();
}
}

View File

@ -9,9 +9,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbstractLawFirmDataModels",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbstractLawFirmContracts", "AbstractLawFirmContracts\AbstractLawFirmContracts\AbstractLawFirmContracts.csproj", "{C0155B2E-5974-4835-996B-6FDF0F5BC06B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AbstractLawFirmBusinessLogic", "AbstractLawFirmBusinessLogic\AbstractLawFirmBusinessLogic.csproj", "{E48808B1-5381-4774-97B6-CDB107DCF98C}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbstractLawFirmBusinessLogic", "AbstractLawFirmBusinessLogic\AbstractLawFirmBusinessLogic.csproj", "{E48808B1-5381-4774-97B6-CDB107DCF98C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AbstractLawFirmListImplement", "AbstractLawFirmListImplement\AbstractLawFirmListImplement.csproj", "{86D5FC6F-B262-44A0-9D30-970F9EA93E0A}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbstractLawFirmListImplement", "AbstractLawFirmListImplement\AbstractLawFirmListImplement.csproj", "{86D5FC6F-B262-44A0-9D30-970F9EA93E0A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AbstractLawFirmFileImplement", "AbstractLawFirmFileImpliment\AbstractLawFirmFileImplement.csproj", "{8AF960C2-208F-4B7B-9F8B-36B63446B6B7}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -39,6 +41,10 @@ Global
{86D5FC6F-B262-44A0-9D30-970F9EA93E0A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{86D5FC6F-B262-44A0-9D30-970F9EA93E0A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{86D5FC6F-B262-44A0-9D30-970F9EA93E0A}.Release|Any CPU.Build.0 = Release|Any CPU
{8AF960C2-208F-4B7B-9F8B-36B63446B6B7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8AF960C2-208F-4B7B-9F8B-36B63446B6B7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8AF960C2-208F-4B7B-9F8B-36B63446B6B7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8AF960C2-208F-4B7B-9F8B-36B63446B6B7}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -15,6 +15,7 @@
<ItemGroup>
<ProjectReference Include="..\AbstractLawFirmBusinessLogic\AbstractLawFirmBusinessLogic.csproj" />
<ProjectReference Include="..\AbstractLawFirmFileImpliment\AbstractLawFirmFileImplement.csproj" />
<ProjectReference Include="..\AbstractLawFirmListImplement\AbstractLawFirmListImplement.csproj" />
</ItemGroup>