From 3112cfb639e5cd66c1c07e116913d4ee6f649337 Mon Sep 17 00:00:00 2001 From: "ns.potapov" Date: Sun, 7 Apr 2024 20:53:47 +0400 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0=D1=86?= =?UTF-8?q?=D0=B8=D1=8F=20=D1=85=D1=80=D0=B0=D0=BD=D0=B8=D0=BB=D0=B8=D1=89?= =?UTF-8?q?=D0=B0=20=D0=BC=D0=B0=D0=B3=D0=B0=D0=B7=D0=B8=D0=BD=D0=BE=D0=B2?= =?UTF-8?q?=20=D0=BD=D0=B0=20=D1=84=D0=B0=D0=B9=D0=BB=D0=B0=D1=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DataFileSingleton.cs | 4 + .../Implements/ShopStorage.cs | 129 ++++++++++++++++++ .../Models/Shop.cs | 2 +- 3 files changed, 134 insertions(+), 1 deletion(-) create mode 100644 SecuritySystem/SecuritySystemFileImplement/Implements/ShopStorage.cs diff --git a/SecuritySystem/SecuritySystemFileImplement/DataFileSingleton.cs b/SecuritySystem/SecuritySystemFileImplement/DataFileSingleton.cs index 735bec9..073d85d 100644 --- a/SecuritySystem/SecuritySystemFileImplement/DataFileSingleton.cs +++ b/SecuritySystem/SecuritySystemFileImplement/DataFileSingleton.cs @@ -9,9 +9,11 @@ namespace SecuritySystemFileImplement private readonly string ComponentFileName = "Component.xml"; private readonly string OrderFileName = "Order.xml"; private readonly string SecureFileName = "Secure.xml"; + private readonly string ShopFileName = "Shop.xml"; public List Components { get; private set; } public List Orders { get; private set; } public List Secures { get; private set; } + public List Shops { get; private set; } public static DataFileSingleton GetInstance() { if (instance == null) @@ -23,11 +25,13 @@ namespace SecuritySystemFileImplement public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement); public void SaveSecures() => SaveData(Secures, SecureFileName, "Secures", 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() { Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; Secures = LoadData(SecureFileName, "Secure", x => Secure.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/SecuritySystem/SecuritySystemFileImplement/Implements/ShopStorage.cs b/SecuritySystem/SecuritySystemFileImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..de7ac69 --- /dev/null +++ b/SecuritySystem/SecuritySystemFileImplement/Implements/ShopStorage.cs @@ -0,0 +1,129 @@ +using SecuritySystemContracts.BindingModels; +using SecuritySystemContracts.SearchModels; +using SecuritySystemContracts.StoragesContracts; +using SecuritySystemContracts.ViewModels; +using SecuritySystemDataModels.Models; +using SecuritySystemFileImplement.Models; + +namespace SecuritySystemFileImplement.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; + } + + public bool SellSecure(ISecureModel model, int count) + { + var secure = source.Secures.FirstOrDefault(x => x.Id == model.Id); + + if (secure == null) + { + return false; + } + var shopSecures = source.Shops.SelectMany(shop => shop.ShopSecures.Where(c => c.Value.Item1.Id == secure.Id)); + int countStore = 0; + + foreach (var shopSec in shopSecures) + countStore += shopSec.Value.Item2; + + if (count > countStore) + return false; + + foreach (var shop in source.Shops) + { + var secures = shop.ShopSecures; + + foreach (var sec in secures.Where(x => x.Value.Item1.Id == secure.Id)) + { + int min = Math.Min(sec.Value.Item2, count); + secures[sec.Value.Item1.Id] = (sec.Value.Item1, sec.Value.Item2 - min); + count -= min; + + if (count <= 0) + break; + } + + shop.Update(new ShopBindingModel + { + Id = shop.Id, + Name = shop.Name, + Address = shop.Address, + MaxSecuresCount = shop.MaxSecuresCount, + OpeningDate = shop.OpeningDate, + ShopSecures = secures + }); + + source.SaveShops(); + + if (count <= 0) + return true; + } + + return true; + } + } +} diff --git a/SecuritySystem/SecuritySystemFileImplement/Models/Shop.cs b/SecuritySystem/SecuritySystemFileImplement/Models/Shop.cs index 74a2363..d8164eb 100644 --- a/SecuritySystem/SecuritySystemFileImplement/Models/Shop.cs +++ b/SecuritySystem/SecuritySystemFileImplement/Models/Shop.cs @@ -4,7 +4,7 @@ using SecuritySystemDataModels.Models; using SecuritySystemFileImplement; using System.Xml.Linq; -namespace AutomobilePlantFileImplement.Models +namespace SecuritySystemFileImplement.Models { public class Shop : IShopModel {