diff --git a/FishFactory/FishFactoryFileImplement/DataFileSingleton.cs b/FishFactory/FishFactoryFileImplement/DataFileSingleton.cs index ed8b81b..889077f 100644 --- a/FishFactory/FishFactoryFileImplement/DataFileSingleton.cs +++ b/FishFactory/FishFactoryFileImplement/DataFileSingleton.cs @@ -14,9 +14,11 @@ namespace FishFactoryFileImplement private readonly string ComponentFileName = "Component.xml"; private readonly string OrderFileName = "Order.xml"; private readonly string CannedFileName = "Canned.xml"; + private readonly string ShopFileName = "Shop.xml"; public List Components { get; private set; } public List Orders { get; private set; } public List Canneds { get; private set; } + public List Shops { get; private set; } public static DataFileSingleton GetInstance() { if (instance == null) @@ -28,11 +30,13 @@ namespace FishFactoryFileImplement public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement); public void SaveCanneds() => SaveData(Canneds, CannedFileName, "Canneds", 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)!)!; Canneds = LoadData(CannedFileName, "Canned", x => Canned.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/FishFactory/FishFactoryFileImplement/Implements/ShopStorage.cs b/FishFactory/FishFactoryFileImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..e33e2c5 --- /dev/null +++ b/FishFactory/FishFactoryFileImplement/Implements/ShopStorage.cs @@ -0,0 +1,106 @@ +using FishFactoryContracts.BindingModels; +using FishFactoryContracts.SearchModels; +using FishFactoryContracts.StoragesContracts; +using FishFactoryContracts.ViewModels; +using FishFactoryDataModels.Models; +using FishFactoryFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FishFactoryFileImplement.Implements +{ + public class ShopStorage : IShopStorage + { + private readonly DataFileSingleton source; + public ShopStorage() + { + source = DataFileSingleton.GetInstance(); + } + public List GetFullList() + { + return source.Shops.Select(x => x.GetViewModel).ToList(); + } + public List GetFilteredList(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName)) + { + return new(); + } + return source.Shops.Where(x => x.ShopName.Contains(model.ShopName)).Select(x => x.GetViewModel).ToList(); + } + public ShopViewModel? GetElement(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName) && !model.Id.HasValue) + { + return null; + } + return source.Shops.FirstOrDefault(x => + (!string.IsNullOrEmpty(model.ShopName) && x.ShopName == + model.ShopName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + 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 element = source.Shops.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + source.Shops.Remove(element); + source.SaveShops(); + return element.GetViewModel; + } + return null; + } + + public bool SellCanneds(ICannedModel model, int count) + { + if (source.Shops.Select(x => x.ListCanneds.FirstOrDefault(y => y.Key == model.Id).Value.Item2).Sum() < count) + { + return false; + } + foreach (var shop in source.Shops.Where(x => x.ListCanneds.ContainsKey(model.Id))) + { + int countInCurrentShop = shop.ListCanneds[model.Id].Item2; + if (countInCurrentShop <= count) + { + shop.ListCanneds.Remove(model.Id); + count -= countInCurrentShop; + } + else + { + shop.ListCanneds[model.Id] = (shop.ListCanneds[model.Id].Item1, countInCurrentShop - count); + count = 0; + } + if (count == 0) + { + return true; + } + } + return false; + } + } +} diff --git a/FishFactory/FishFactoryFileImplement/Models/Order.cs b/FishFactory/FishFactoryFileImplement/Models/Order.cs index 9555347..94d47fb 100644 --- a/FishFactory/FishFactoryFileImplement/Models/Order.cs +++ b/FishFactory/FishFactoryFileImplement/Models/Order.cs @@ -67,11 +67,7 @@ namespace FishFactoryFileImplement.Models { return; } - CannedId = model.CannedId; - Count = model.Count; - Sum = model.Sum; Status = model.Status; - DateCreate = model.DateCreate; DateImplement = model.DateImplement; } public OrderViewModel GetViewModel => new() diff --git a/FishFactory/FishFactoryFileImplement/Models/Shop.cs b/FishFactory/FishFactoryFileImplement/Models/Shop.cs new file mode 100644 index 0000000..9a90d8e --- /dev/null +++ b/FishFactory/FishFactoryFileImplement/Models/Shop.cs @@ -0,0 +1,104 @@ +using FishFactoryContracts.BindingModels; +using FishFactoryContracts.ViewModels; +using FishFactoryDataModels; +using FishFactoryDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace FishFactoryFileImplement.Models +{ + public class Shop : IShopModel + { + public int Id { get; private set; } + public string ShopName { get; private set; } = string.Empty; + public string Address { get; private set; } = string.Empty; + public DateTime DateOpening { get; private set; } + public int MaxCountCanneds { get; private set; } + + private Dictionary? _shopCanned = null; + public Dictionary CountCanned { get; private set; } = new(); + + public Dictionary ListCanneds { + get + { + if (_shopCanned == null) + { + var source = DataFileSingleton.GetInstance(); + _shopCanned = CountCanned.ToDictionary(x => x.Key, y => ((source.Canneds.FirstOrDefault(z => z.Id == y.Key) as ICannedModel)!, y.Value)); + } + return _shopCanned; + } + } + public static Shop? Create(ShopBindingModel? model) + { + if (model == null) + { + return null; + } + return new Shop() + { + Id = model.Id, + ShopName = model.ShopName, + Address = model.Address, + MaxCountCanneds = model.MaxCountCanneds, + DateOpening = model.DateOpening, + CountCanned = model.ListCanneds.ToDictionary(x => x.Key, x => x.Value.Item2), + }; + } + public static Shop? Create(XElement element) + { + if (element == null) + { + return null; + } + return new() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + ShopName = element.Element("ShopName")!.Value, + Address = element.Element("Address")!.Value, + DateOpening = Convert.ToDateTime(element.Element("DateOpening")!.Value), + MaxCountCanneds = Convert.ToInt32(element.Element("MaxCountCanneds")!.Value), + CountCanned = element.Element("Canneds")!.Elements("Canned").ToDictionary( + x => Convert.ToInt32(x.Element("Key")?.Value), + x => Convert.ToInt32(x.Element("Value")?.Value)) + }; + } + public void Update(ShopBindingModel? model) + { + if (model == null) + { + return; + } + ShopName = model.ShopName; + Address = model.Address; + DateOpening = model.DateOpening; + MaxCountCanneds = model.MaxCountCanneds; + CountCanned = model.ListCanneds.ToDictionary(x => x.Key, x => x.Value.Item2); + _shopCanned = null; + } + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Address = Address, + ListCanneds = ListCanneds, + DateOpening = DateOpening, + MaxCountCanneds = MaxCountCanneds, + }; + public XElement GetXElement => new("Shop", + new XAttribute("Id", Id), + new XElement("ShopName", ShopName), + new XElement("Address", Address), + new XElement("DateOpening", DateOpening), + new XElement("MaxCountCanneds", MaxCountCanneds), + new XElement("Canneds", CountCanned + .Select(x => new XElement("Canned", + new XElement("Key", x.Key), + new XElement("Value", x.Value)) + ).ToArray())); + } +} diff --git a/FishFactory/FishFactoryListImplement/Implements/ShopStorage.cs b/FishFactory/FishFactoryListImplement/Implements/ShopStorage.cs index a7bbe9d..3a42e65 100644 --- a/FishFactory/FishFactoryListImplement/Implements/ShopStorage.cs +++ b/FishFactory/FishFactoryListImplement/Implements/ShopStorage.cs @@ -2,6 +2,7 @@ using FishFactoryContracts.SearchModels; using FishFactoryContracts.StoragesContracts; using FishFactoryContracts.ViewModels; +using FishFactoryDataModels.Models; using FishFactoryListImplement.Models; using System; using System.Collections.Generic; @@ -103,5 +104,10 @@ namespace FishFactoryListImplement.Implements } return null; } + + public bool SellCanneds(ICannedModel model, int count) + { + throw new NotImplementedException(); + } } } diff --git a/FishFactory/FishFactoryListImplement/Models/Shop.cs b/FishFactory/FishFactoryListImplement/Models/Shop.cs index f3d75c6..0ae5500 100644 --- a/FishFactory/FishFactoryListImplement/Models/Shop.cs +++ b/FishFactory/FishFactoryListImplement/Models/Shop.cs @@ -55,5 +55,7 @@ namespace FishFactoryListImplement.Models ListCanneds = ListCanneds, DateOpening = DateOpening, }; + + public int MaxCountCanneds => throw new NotImplementedException(); } }