From faef920c5018c3d68de6d4036aed45da1ea69973 Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Sun, 26 Feb 2023 23:20:43 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=B0=D1=87=D0=B0=D1=82=D0=B0=20=D1=84?= =?UTF-8?q?=D0=B0=D0=B9=D0=BB=D0=BE=D0=B2=D0=B0=D1=8F=20=D1=80=D0=B5=D0=B0?= =?UTF-8?q?=D0=BB=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D1=8F=20=D0=BC=D0=B0=D0=B3?= =?UTF-8?q?=D0=B0=D0=B7=D0=B8=D0=BD=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BindingModels/ShopBindingModel.cs | 2 + .../ViewModels/ShopViewModel.cs | 3 + .../LawFirmDataModels/Models/IShopModel.cs | 1 + .../LawFirmFileImplement/DataFileSingleton.cs | 5 + .../Implements/BlankStorage.cs | 2 +- .../Implements/DocumentStorage.cs | 3 +- .../Implements/OrderStorage.cs | 2 +- .../Implements/ShopStorage.cs | 84 ++++++++++++ .../LawFirmFileImplement/Models/Document.cs | 2 +- LawFirm/LawFirmFileImplement/Models/Shop.cs | 121 ++++++++++++++++++ 10 files changed, 221 insertions(+), 4 deletions(-) create mode 100644 LawFirm/LawFirmFileImplement/Implements/ShopStorage.cs create mode 100644 LawFirm/LawFirmFileImplement/Models/Shop.cs diff --git a/LawFirm/LawFirmContracts/BindingModels/ShopBindingModel.cs b/LawFirm/LawFirmContracts/BindingModels/ShopBindingModel.cs index 167e727..462e6ed 100644 --- a/LawFirm/LawFirmContracts/BindingModels/ShopBindingModel.cs +++ b/LawFirm/LawFirmContracts/BindingModels/ShopBindingModel.cs @@ -18,5 +18,7 @@ namespace LawFirmContracts.BindingModels public Dictionary ShopDocuments { get; set; } = new(); public int Id { get; set; } + + public int MaxCountDocuments { get; set; } } } diff --git a/LawFirm/LawFirmContracts/ViewModels/ShopViewModel.cs b/LawFirm/LawFirmContracts/ViewModels/ShopViewModel.cs index 8912255..a58ba7a 100644 --- a/LawFirm/LawFirmContracts/ViewModels/ShopViewModel.cs +++ b/LawFirm/LawFirmContracts/ViewModels/ShopViewModel.cs @@ -20,5 +20,8 @@ namespace LawFirmContracts.ViewModels public Dictionary ShopDocuments { get; set; } = new(); public int Id { get; set; } + + [DisplayName("Макс. документов в магазине")] + public int MaxCountDocuments { get; set; } } } diff --git a/LawFirm/LawFirmDataModels/Models/IShopModel.cs b/LawFirm/LawFirmDataModels/Models/IShopModel.cs index 9f7b436..0b00d18 100644 --- a/LawFirm/LawFirmDataModels/Models/IShopModel.cs +++ b/LawFirm/LawFirmDataModels/Models/IShopModel.cs @@ -12,5 +12,6 @@ namespace LawFirmDataModels.Models String Adress { get; } DateTime OpeningDate { get; } Dictionary ShopDocuments { get; } + int MaxCountDocuments { get; } } } diff --git a/LawFirm/LawFirmFileImplement/DataFileSingleton.cs b/LawFirm/LawFirmFileImplement/DataFileSingleton.cs index baa110a..564e833 100644 --- a/LawFirm/LawFirmFileImplement/DataFileSingleton.cs +++ b/LawFirm/LawFirmFileImplement/DataFileSingleton.cs @@ -15,9 +15,12 @@ namespace LawFirmFileImplement private readonly string BlankFileName = "Blank.xml"; private readonly string OrderFileName = "Order.xml"; private readonly string DocumentFileName = "Document.xml"; + private readonly string ShopFileName = "Shop.xml"; public List Blanks { get; private set; } public List Orders { get; private set; } public List Documents { get; private set; } + public List Shops { get; private set; } + public static DataFileSingleton GetInstance() { if (instance == null) @@ -29,11 +32,13 @@ namespace LawFirmFileImplement public void SaveBlanks() => SaveData(Blanks, BlankFileName, "Blanks", x => x.GetXElement); public void SaveDocuments() => SaveData(Documents, DocumentFileName, "Documents", x => x.GetXElement); public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); + public void SaveShops() => SaveData(Shops, ShopFileName, "Shops", x => x.GetXElement); private DataFileSingleton() { Blanks = LoadData(BlankFileName, "Blank", x => Blank.Create(x)!)!; Documents = LoadData(DocumentFileName, "Document", x => Document.Create(x)!)!; Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; + Shops = LoadData(ShopFileName, "Shop", x => Shop.Create(x)!)!; } private static List? LoadData(string filename, string xmlNodeName, Func selectFunction) { diff --git a/LawFirm/LawFirmFileImplement/Implements/BlankStorage.cs b/LawFirm/LawFirmFileImplement/Implements/BlankStorage.cs index 47b885b..bb10978 100644 --- a/LawFirm/LawFirmFileImplement/Implements/BlankStorage.cs +++ b/LawFirm/LawFirmFileImplement/Implements/BlankStorage.cs @@ -79,7 +79,7 @@ namespace LawFirmFileImplement.Implements { return null; } - blank.Update(model); + source.Blanks.Remove(blank); source.SaveBlanks(); return blank.GetViewModel; } diff --git a/LawFirm/LawFirmFileImplement/Implements/DocumentStorage.cs b/LawFirm/LawFirmFileImplement/Implements/DocumentStorage.cs index 7629dfa..601bcd2 100644 --- a/LawFirm/LawFirmFileImplement/Implements/DocumentStorage.cs +++ b/LawFirm/LawFirmFileImplement/Implements/DocumentStorage.cs @@ -8,6 +8,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Xml.Linq; namespace LawFirmFileImplement.Implements { @@ -78,7 +79,7 @@ namespace LawFirmFileImplement.Implements { return null; } - document.Update(model); + source.Documents.Remove(document); source.SaveDocuments(); return document.GetViewModel; } diff --git a/LawFirm/LawFirmFileImplement/Implements/OrderStorage.cs b/LawFirm/LawFirmFileImplement/Implements/OrderStorage.cs index baee8c5..46a8d01 100644 --- a/LawFirm/LawFirmFileImplement/Implements/OrderStorage.cs +++ b/LawFirm/LawFirmFileImplement/Implements/OrderStorage.cs @@ -78,7 +78,7 @@ namespace LawFirmFileImplement.Implements { return null; } - order.Update(model); + source.Orders.Remove(order); source.SaveOrders(); return order.GetViewModel; } diff --git a/LawFirm/LawFirmFileImplement/Implements/ShopStorage.cs b/LawFirm/LawFirmFileImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..7463c78 --- /dev/null +++ b/LawFirm/LawFirmFileImplement/Implements/ShopStorage.cs @@ -0,0 +1,84 @@ +using LawFirmContracts.BindingModels; +using LawFirmContracts.SearchModels; +using LawFirmContracts.StorageContracts; +using LawFirmContracts.ViewModels; +using LawFirmFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LawFirmFileImplement.Implements +{ + public class ShopStorage : IShopStorage + { + private readonly DataFileSingleton source; + public ShopStorage() + { + source = DataFileSingleton.GetInstance(); + } + public ShopViewModel? GetElement(ShopSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + return source.Shops.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)?.GetViewModel; + } + + public List GetFilteredList(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.Name)) + { + return new(); + } + return source.Shops + .Select(x => x.GetViewModel) + .Where(x => x.Name.Contains(model.Name ?? string.Empty)) + .ToList(); + } + + public List GetFullList() + { + return source.Shops.Select(shop => shop.GetViewModel).ToList(); + } + + public ShopViewModel? Insert(ShopBindingModel model) + { + model.Id = source.Shops.Count > 0 ? source.Shops.Max(x => x.Id) + 1 : 1; + var newShop = Shop.Create(model); + if (newShop == null) + { + return null; + } + source.Shops.Add(newShop); + source.SaveShops(); + return newShop.GetViewModel; + } + + public ShopViewModel? Update(ShopBindingModel model) + { + var shop = source.Shops.FirstOrDefault(x => x.Id == model.Id); + if (shop == null) + { + return null; + } + shop.Update(model); + source.SaveShops(); + return shop.GetViewModel; + } + public ShopViewModel? Delete(ShopBindingModel model) + { + var shop = source.Shops.FirstOrDefault(x => x.Id == model.Id); + if (shop == null) + { + return null; + } + source.Shops.Remove(shop); + source.SaveShops(); + return shop.GetViewModel; + } + + } +} diff --git a/LawFirm/LawFirmFileImplement/Models/Document.cs b/LawFirm/LawFirmFileImplement/Models/Document.cs index 6484a59..30ce0bb 100644 --- a/LawFirm/LawFirmFileImplement/Models/Document.cs +++ b/LawFirm/LawFirmFileImplement/Models/Document.cs @@ -92,7 +92,7 @@ namespace LawFirmFileImplement.Models }; public XElement GetXElement => new( - "Document", + "Document", new XAttribute("Id", Id), new XElement("DocumentName", DocumentName), new XElement("Price", Price.ToString()), diff --git a/LawFirm/LawFirmFileImplement/Models/Shop.cs b/LawFirm/LawFirmFileImplement/Models/Shop.cs new file mode 100644 index 0000000..f3e0b1f --- /dev/null +++ b/LawFirm/LawFirmFileImplement/Models/Shop.cs @@ -0,0 +1,121 @@ +using LawFirmContracts.BindingModels; +using LawFirmContracts.ViewModels; +using LawFirmDataModels.Models; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace LawFirmFileImplement.Models +{ + public class Shop : IShopModel + { + public int Id { get; private set; } + + public string Name { get; private set; } = string.Empty; + + public string Adress { get; private set; } = string.Empty; + + public int MaxCountDocuments { get; private set; } + + public DateTime OpeningDate { get; private set; } + + public Dictionary Documents { get; private set; } = new(); + + private Dictionary? _shopDocuments = null; + + public Dictionary ShopDocuments + { + get + { + if (_shopDocuments == null) + { + var source = DataFileSingleton.GetInstance(); + _shopDocuments = Documents.ToDictionary( + x => x.Key, + y => ((source.Documents.FirstOrDefault(z => z.Id == y.Key) as IDocumentModel)!, y.Value) + ); + } + return _shopDocuments; + } + } + + public static Shop? Create(ShopBindingModel? model) + { + if (model == null) + { + return null; + } + return new Shop() + { + Id = model.Id, + Name = model.Name, + Adress = model.Adress, + MaxCountDocuments = model.MaxCountDocuments, + OpeningDate = model.OpeningDate, + Documents = model.ShopDocuments.ToDictionary(x => x.Key, x => x.Value.Item2) + }; + } + + public static Shop? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Shop() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + Name = element.Element("Name")!.Value, + Adress = element.Element("Address")!.Value, + MaxCountDocuments = Convert.ToInt32(element.Element("MaxCountDocuments")!.Value), + OpeningDate = Convert.ToDateTime(element.Element("OpeningDate")!.Value), + Documents= element.Element("ShopDocuments")!.Elements("ShopDocument").ToDictionary( + x => Convert.ToInt32(x.Element("Key")?.Value), + x => Convert.ToInt32(x.Element("Value")?.Value) + ) + }; + } + + + public void Update(ShopBindingModel? model) + { + if (model == null) + { + return; + } + Name = model.Name; + Adress = model.Adress; + OpeningDate = model.OpeningDate; + MaxCountDocuments = model.MaxCountDocuments; + Documents = model.ShopDocuments.ToDictionary(x => x.Key, x => x.Value.Item2); + _shopDocuments = null; + } + public ShopViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + Adress = Adress, + OpeningDate = OpeningDate, + ShopDocuments = ShopDocuments, + MaxCountDocuments = MaxCountDocuments + }; + + public XElement GetXElement => new( + "Shop", + new XAttribute("Id", Id), + new XElement("Name", Name), + new XElement("Address", Adress), + new XElement("MaxCountDocuments", MaxCountDocuments.ToString()), + new XElement("OpeningDate", OpeningDate.ToString()), + new XElement("ShopDocuments", Documents.Select (x => + new XElement("ShopDocument", + new XElement("Key", x.Key), + new XElement("Value", x.Value))) + .ToArray())); + } +} +