diff --git a/FurnitureAssembly/FurnitureAssemFileImplement/DataFileSingleton.cs b/FurnitureAssembly/FurnitureAssemFileImplement/DataFileSingleton.cs new file mode 100644 index 0000000..3e1c4dc --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemFileImplement/DataFileSingleton.cs @@ -0,0 +1,60 @@ +using FurnitureAssemFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace FurnitureAssemFileImplement +{ + public class DataFileSingleton + { + private static DataFileSingleton? instance; + private readonly string ComponentFileName = "Component.xml"; + private readonly string OrderFileName = "Order.xml"; + private readonly string FurnitureFileName = "Furniture.xml"; + private readonly string ShopFileName = "Shop.xml"; + public List Components { get; private set; } + public List Orders { get; private set; } + public List Furnitures { get; private set; } + public List Shops { 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 SaveFurnitures() => SaveData(Furnitures, FurnitureFileName, "Furnitures", 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)!)!; + Furnitures = LoadData(FurnitureFileName, "Furniture", x => Furniture.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) + { + if (File.Exists(filename)) + { + return XDocument.Load(filename)?.Root?.Elements(xmlNodeName)?.Select(selectFunction)?.ToList(); + } + return new List(); + } + private static void SaveData(List data, string filename, + string xmlNodeName, Func selectFunction) + { + if (data != null) + { + new XDocument(new XElement(xmlNodeName, + data.Select(selectFunction).ToArray())).Save(filename); + } + } + } +} diff --git a/FurnitureAssembly/FurnitureAssemFileImplement/FurnitureAssemFileImplement.csproj b/FurnitureAssembly/FurnitureAssemFileImplement/FurnitureAssemFileImplement.csproj new file mode 100644 index 0000000..c0fa55b --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemFileImplement/FurnitureAssemFileImplement.csproj @@ -0,0 +1,14 @@ + + + + net6.0 + enable + enable + + + + + + + + diff --git a/FurnitureAssembly/FurnitureAssemFileImplement/Implements/ComponentStorage.cs b/FurnitureAssembly/FurnitureAssemFileImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..e2d3450 --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemFileImplement/Implements/ComponentStorage.cs @@ -0,0 +1,92 @@ +using FurnitureAssemblyContracts.BindingModels; +using FurnitureAssemblyContracts.SearchModels; +using FurnitureAssemblyContracts.StoragesContracts; +using FurnitureAssemblyContracts.ViewModels; +using FurnitureAssemFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FurnitureAssemFileImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + private readonly DataFileSingleton source; + public ComponentStorage() + { + source = DataFileSingleton.GetInstance(); + } + public List GetFullList() + { + return source.Components + .Select(x => x.GetViewModel) + .ToList(); + } + public List 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; + } + + } +} diff --git a/FurnitureAssembly/FurnitureAssemFileImplement/Implements/FurnitureStorage.cs b/FurnitureAssembly/FurnitureAssemFileImplement/Implements/FurnitureStorage.cs new file mode 100644 index 0000000..f9d26f2 --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemFileImplement/Implements/FurnitureStorage.cs @@ -0,0 +1,88 @@ +using FurnitureAssemblyContracts.BindingModels; +using FurnitureAssemblyContracts.SearchModels; +using FurnitureAssemblyContracts.StoragesContracts; +using FurnitureAssemblyContracts.ViewModels; +using FurnitureAssemFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FurnitureAssemFileImplement.Implements +{ + public class FurnitureStorage : IFurnitureStorage + { + private readonly DataFileSingleton source; + public FurnitureStorage() + { + source = DataFileSingleton.GetInstance(); + } + public FurnitureViewModel? GetElement(FurnitureSearchModel model) + { + if (string.IsNullOrEmpty(model.FurnitureName) && !model.Id.HasValue) + { + return null; + } + + return source.Furnitures.FirstOrDefault(x => + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public List GetFilteredList(FurnitureSearchModel model) + { + if (string.IsNullOrEmpty(model.FurnitureName)) + { + return new(); + } + return source.Furnitures + .Where(x => x.FurnitureName.Contains(model.FurnitureName)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + return source.Furnitures.Select(x => x.GetViewModel).ToList(); + } + + public FurnitureViewModel? Insert(FurnitureBindingModel model) + { + model.Id = source.Furnitures.Count > 0 ? source.Furnitures.Max(x => + x.Id) + 1 : 1; + var newFurniture = Furniture.Create(model); + if (newFurniture == null) + { + return null; + } + source.Furnitures.Add(newFurniture); + source.SaveFurnitures(); + return newFurniture.GetViewModel; + } + + public FurnitureViewModel? Update(FurnitureBindingModel model) + { + var furniture = source.Furnitures.FirstOrDefault(x => x.Id == model.Id); + if (furniture == null) + { + return null; + } + furniture.Update(model); + source.SaveFurnitures(); + return furniture.GetViewModel; + } + + public FurnitureViewModel? Delete(FurnitureBindingModel model) + { + var element = source.Furnitures.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + source.Furnitures.Remove(element); + source.SaveFurnitures(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/FurnitureAssembly/FurnitureAssemFileImplement/Implements/OrderStorage.cs b/FurnitureAssembly/FurnitureAssemFileImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..3875178 --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemFileImplement/Implements/OrderStorage.cs @@ -0,0 +1,103 @@ +using FurnitureAssemblyContracts.BindingModels; +using FurnitureAssemblyContracts.SearchModels; +using FurnitureAssemblyContracts.StoragesContracts; +using FurnitureAssemblyContracts.ViewModels; +using FurnitureAssemFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace FurnitureAssemFileImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + private readonly DataFileSingleton source; + public OrderStorage() + { + source = DataFileSingleton.GetInstance(); + } + + public List GetFullList() + { + return source.Orders.Select(x => GetOrderViewModel(x)).ToList(); + } + + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + return source.Orders + .Where(x => x.Id.Equals(model.Id)) + .Select(x => GetOrderViewModel(x)) + .ToList(); + } + + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + var order = source.Orders + .FirstOrDefault(x => + (model.Id.HasValue && x.Id == model.Id)); + return GetOrderViewModel(order); + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + model.Id = source.Orders.Count > 0 ? source.Orders.Max(x => x.Id) + 1 : 1; + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + source.Orders.Add(newOrder); + source.SaveOrders(); + return GetOrderViewModel(newOrder); + } + + public OrderViewModel? Update(OrderBindingModel model) + { + var order = source.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order == null) + { + return null; + } + order.Update(model); + source.SaveOrders(); + return GetOrderViewModel(order); + } + + public OrderViewModel? Delete(OrderBindingModel model) + { + var element = source.Orders.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + source.Orders.Remove(element); + source.SaveOrders(); + return GetOrderViewModel(element); + } + return null; + } + + private OrderViewModel GetOrderViewModel(Order order) + { + OrderViewModel orderViewModel = order.GetViewModel; + + foreach (var furniture in source.Furnitures) + { + if (furniture.Id == order.FurnitureId) + { + orderViewModel.FurnitureName = furniture.FurnitureName; + } + } + return orderViewModel; + } + } +} diff --git a/FurnitureAssembly/FurnitureAssemFileImplement/Implements/ShopStorage.cs b/FurnitureAssembly/FurnitureAssemFileImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..f90fe45 --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemFileImplement/Implements/ShopStorage.cs @@ -0,0 +1,145 @@ +using FurnitureAssemblyContracts.BindingModels; +using FurnitureAssemblyContracts.SearchModels; +using FurnitureAssemblyContracts.StoragesContracts; +using FurnitureAssemblyContracts.ViewModels; +using FurnitureAssemFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FurnitureAssemFileImplement.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.Equals(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 => + (model.Id.HasValue && x.Id == model.Id) || (!string.IsNullOrEmpty(model.ShopName) && x.ShopName == model.ShopName))?.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; + } + + private int GetCountFurnitureAllShops(FurnitureBindingModel furniture) + { + int count = 0; + foreach (var shop in source.Shops) + { + if (shop.Furnitures.ContainsKey(furniture.Id)) + { + count += shop.Furnitures[furniture.Id].Item2; + } + } + return count; + } + + + public bool Sell(FurnitureBindingModel furniture, int count) + { + if (GetCountFurnitureAllShops(furniture) < count) + { + return false; + } + + + foreach (var shop in source.Shops) + { + if (shop.Furnitures.ContainsKey(furniture.Id)) + { + if (shop.Furnitures[furniture.Id].Item2 > count) + { + shop.Furnitures[furniture.Id] = (shop.Furnitures[furniture.Id].Item1, shop.Furnitures[furniture.Id].Item2 - count); + shop.Update(new ShopBindingModel + { + Id = shop.Id, + ShopName = shop.ShopName, + MaxCount = shop.MaxCount, + Furnitures = shop.Furnitures, + Address = shop.Address, + DateOpening = shop.DateOpening, + }); + break; + } + count -= shop.Furnitures[furniture.Id].Item2; + shop.Furnitures[furniture.Id] = (shop.Furnitures[furniture.Id].Item1, 0); + shop.Update(new ShopBindingModel + { + Id = shop.Id, + ShopName = shop.ShopName, + MaxCount = shop.MaxCount, + Furnitures = shop.Furnitures, + Address = shop.Address, + DateOpening = shop.DateOpening, + }); + + } + } + source.SaveShops(); + return true; + } + } +} diff --git a/FurnitureAssembly/FurnitureAssemFileImplement/Models/Component.cs b/FurnitureAssembly/FurnitureAssemFileImplement/Models/Component.cs new file mode 100644 index 0000000..ce0dbca --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemFileImplement/Models/Component.cs @@ -0,0 +1,61 @@ +using FurnitureAssemblyContracts.BindingModels; +using FurnitureAssemblyContracts.ViewModels; +using FurnitureAssemblyDataModels.Models; +using System.Xml.Linq; + +namespace FurnitureAssemFileImplement.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())); + } +} diff --git a/FurnitureAssembly/FurnitureAssemFileImplement/Models/Furniture.cs b/FurnitureAssembly/FurnitureAssemFileImplement/Models/Furniture.cs new file mode 100644 index 0000000..6689a82 --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemFileImplement/Models/Furniture.cs @@ -0,0 +1,89 @@ +using FurnitureAssemblyContracts.BindingModels; +using FurnitureAssemblyContracts.ViewModels; +using FurnitureAssemblyDataModels.Models; +using System.Xml.Linq; + +namespace FurnitureAssemFileImplement.Models +{ + public class Furniture : IFurnitureModel + { + public int Id { get; private set; } + public string FurnitureName { get; private set; } = string.Empty; + public double Price { get; private set; } + public Dictionary Components { get; private set; } = new(); + private Dictionary? _furnitureComponents = null; + public Dictionary FurnitureComponents + { + get + { + if (_furnitureComponents == null) + { + var source = DataFileSingleton.GetInstance(); + _furnitureComponents = Components.ToDictionary(x => x.Key, y => + ((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!, + y.Value)); + } + return _furnitureComponents; + } + } + + public static Furniture? Create(FurnitureBindingModel model) + { + if (model == null) + { + return null; + } + return new Furniture() + { + Id = model.Id, + FurnitureName = model.FurnitureName, + Price = model.Price, + Components = model.FurnitureComponents.ToDictionary(x => x.Key, x => x.Value.Item2) + }; + } + + public static Furniture? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Furniture() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + FurnitureName = element.Element("FurnitureName")!.Value, + Price = Convert.ToDouble(element.Element("Price")!.Value), + Components = element.Element("FurnitureComponents")!.Elements("FurnitureComponent").ToDictionary( + x => Convert.ToInt32(x.Element("Key")?.Value), + x => Convert.ToInt32(x.Element("Value")?.Value)) + }; + } + + public void Update(FurnitureBindingModel model) + { + if (model == null) + { + return; + } + FurnitureName = model.FurnitureName; + Price = model.Price; + Components = model.FurnitureComponents.ToDictionary(x => x.Key, x => + x.Value.Item2); + _furnitureComponents = null; + } + + public FurnitureViewModel GetViewModel => new() + { + Id = Id, + FurnitureName = FurnitureName, + Price = Price, + FurnitureComponents = FurnitureComponents + }; + public XElement GetXElement => new("Furniture", + new XAttribute("Id", Id), + new XElement("FurnitureName", FurnitureName), + new XElement("Price", Price.ToString()), + new XElement("FurnitureComponents", Components.Select( + x => new XElement("FurnitureComponent", new XElement("Key", x.Key), new XElement("Value", x.Value))).ToArray())); + } +} diff --git a/FurnitureAssembly/FurnitureAssemFileImplement/Models/Order.cs b/FurnitureAssembly/FurnitureAssemFileImplement/Models/Order.cs new file mode 100644 index 0000000..184a929 --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemFileImplement/Models/Order.cs @@ -0,0 +1,109 @@ +using FurnitureAssemblyContracts.BindingModels; +using FurnitureAssemblyContracts.ViewModels; +using FurnitureAssemblyDataModels.Enums; +using FurnitureAssemblyDataModels.Models; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace FurnitureAssemFileImplement.Models +{ + public class Order : IOrderModel + { + public int FurnitureId { get; private set; } + + public string FurnitureName { get; private set; } = string.Empty; + + public int Count { get; private set; } + + public double Sum { get; private set; } + + public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен; + + public DateTime DateCreate { get; private set; } = DateTime.Now; + + public DateTime? DateImplement { get; private set; } + + public int Id { get; private set; } + + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + FurnitureId = model.FurnitureId, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement + }; + } + + public static Order? Create(XElement element) + { + if (element == null) + { + return null; + } + var order = new Order() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + FurnitureId = Convert.ToInt32(element.Element("FurnitureId")!.Value), + Count = Convert.ToInt32(element.Element("Count")!.Value), + Sum = Convert.ToDouble(element.Element("Sum")!.Value), + DateCreate = DateTime.ParseExact(element.Element("DateCreate")!.Value, "G", null), + }; + DateTime.TryParse(element.Element("DateImplement")!.Value, out DateTime dateImpl); + + order.DateImplement = dateImpl; + + if (!Enum.TryParse(element.Element("Status")!.Value, out OrderStatus status)) + { + return null; + } + order.Status = status; + return order; + } + + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + + Status = model.Status; + DateImplement = model.DateImplement; + } + + public OrderViewModel GetViewModel => new() + { + Id = Id, + FurnitureId = FurnitureId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement + }; + + public XElement GetXElement => new("Order", + new XAttribute("Id", Id), + new XElement("FurnitureId", FurnitureId), + new XElement("Count", Count.ToString()), + new XElement("Sum", Sum.ToString()), + new XElement("Status", Status.ToString()), + new XElement("DateCreate", DateCreate.ToString()), + new XElement("DateImplement", DateImplement.ToString())); + } +} diff --git a/FurnitureAssembly/FurnitureAssemFileImplement/Models/Shop.cs b/FurnitureAssembly/FurnitureAssemFileImplement/Models/Shop.cs new file mode 100644 index 0000000..aeb1cc6 --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemFileImplement/Models/Shop.cs @@ -0,0 +1,115 @@ +using FurnitureAssemblyContracts.BindingModels; +using FurnitureAssemblyDataModels.Models; +using FurnitureAssemblyContracts.ViewModels; +using System.Xml.Linq; +using System.Diagnostics; + +namespace FurnitureAssemFileImplement.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; } + + private Dictionary? _furnitures = null; + public Dictionary Furnitures + { + get + { + if (_furnitures == null) + { + var source = DataFileSingleton.GetInstance(); + _furnitures = CurrentCount.ToDictionary( + x => x.Key, + y => (source.Furnitures.FirstOrDefault(z => z.Id == y.Key)! as IFurnitureModel, y.Value)); + + } + return _furnitures; + } + private set { } + } + public int MaxCount { get; private set; } + + public Dictionary CurrentCount { get; private set; } = new(); + + public static Shop? Create(ShopBindingModel model) + { + if (model == null) + { + return null; + } + return new Shop() + { + Id = model.Id, + ShopName = model.ShopName, + Address = model.Address, + DateOpening = model.DateOpening, + MaxCount = model.MaxCount, + CurrentCount = model.Furnitures.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), + MaxCount = Convert.ToInt32(element.Element("MaxCount")!.Value), + CurrentCount = element.Element("CurrentCount")!.Elements("CurrentCountFurniture") + .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; + MaxCount = model.MaxCount; + DateOpening = model.DateOpening; + if (model.Furnitures.Count > 0) + { + CurrentCount = model.Furnitures.ToDictionary(x => x.Key, x => x.Value.Item2); + _furnitures = null; + } + } + + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Address = Address, + DateOpening = DateOpening, + MaxCount = MaxCount, + Furnitures = Furnitures + }; + + public XElement GetXElement => new("Shop", + new XAttribute("Id", Id), + new XElement("ShopName", ShopName), + new XElement("Address", Address), + new XElement("DateOpening", DateOpening), + new XElement("MaxCount", MaxCount), + new XElement("CurrentCount", + CurrentCount.Select( + x => new XElement("CurrentCountFurniture", new XElement("Key", x.Key), new XElement("Value", x.Value))).ToArray())); + } +} diff --git a/FurnitureAssembly/FurnitureAssembly.sln b/FurnitureAssembly/FurnitureAssembly.sln index da3aa5d..51e7896 100644 --- a/FurnitureAssembly/FurnitureAssembly.sln +++ b/FurnitureAssembly/FurnitureAssembly.sln @@ -11,7 +11,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FurnitureAssemblyContracts" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FurnitureAssemblyBusinessLogic", "FurnitureAssemblyBusinessLogic\FurnitureAssemblyBusinessLogic.csproj", "{B51952FD-5EB4-4A80-8598-7B1EC39CD34C}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FurnitureAssemblyListImplement", "FurnitureAssemblyListImplement\FurnitureAssemblyListImplement.csproj", "{6662252C-A676-4376-AA01-0ACC6AE5B217}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FurnitureAssemblyListImplement", "FurnitureAssemblyListImplement\FurnitureAssemblyListImplement.csproj", "{6662252C-A676-4376-AA01-0ACC6AE5B217}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FurnitureAssemFileImplement", "FurnitureAssemFileImplement\FurnitureAssemFileImplement.csproj", "{A6822E0E-DBF3-4DEA-A337-C6C64429C0DE}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -39,6 +41,10 @@ Global {6662252C-A676-4376-AA01-0ACC6AE5B217}.Debug|Any CPU.Build.0 = Debug|Any CPU {6662252C-A676-4376-AA01-0ACC6AE5B217}.Release|Any CPU.ActiveCfg = Release|Any CPU {6662252C-A676-4376-AA01-0ACC6AE5B217}.Release|Any CPU.Build.0 = Release|Any CPU + {A6822E0E-DBF3-4DEA-A337-C6C64429C0DE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A6822E0E-DBF3-4DEA-A337-C6C64429C0DE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A6822E0E-DBF3-4DEA-A337-C6C64429C0DE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A6822E0E-DBF3-4DEA-A337-C6C64429C0DE}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/FurnitureAssembly/FurnitureAssembly/FormMain.Designer.cs b/FurnitureAssembly/FurnitureAssembly/FormMain.Designer.cs index f81c725..7d13b88 100644 --- a/FurnitureAssembly/FurnitureAssembly/FormMain.Designer.cs +++ b/FurnitureAssembly/FurnitureAssembly/FormMain.Designer.cs @@ -40,6 +40,7 @@ this.изделияToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.магазиныToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.пополнениеМагазинаToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.buttonSell = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); this.menuStrip1.SuspendLayout(); this.SuspendLayout(); @@ -152,11 +153,22 @@ this.пополнениеМагазинаToolStripMenuItem.Text = "Пополнение магазина"; this.пополнениеМагазинаToolStripMenuItem.Click += new System.EventHandler(this.ReplenishmentToolStripMenuItem_Click); // + // buttonSell + // + this.buttonSell.Location = new System.Drawing.Point(902, 324); + this.buttonSell.Name = "buttonSell"; + this.buttonSell.Size = new System.Drawing.Size(150, 25); + this.buttonSell.TabIndex = 7; + this.buttonSell.Text = "Продажа мебели"; + this.buttonSell.UseVisualStyleBackColor = true; + this.buttonSell.Click += new System.EventHandler(this.buttonSell_Click); + // // FormMain // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(1065, 450); + this.Controls.Add(this.buttonSell); this.Controls.Add(this.ButtonRef); this.Controls.Add(this.ButtonIssuedOrder); this.Controls.Add(this.ButtonOrderReady); @@ -190,5 +202,6 @@ private ToolStripMenuItem изделияToolStripMenuItem; private ToolStripMenuItem магазиныToolStripMenuItem; private ToolStripMenuItem пополнениеМагазинаToolStripMenuItem; + private Button buttonSell; } } \ No newline at end of file diff --git a/FurnitureAssembly/FurnitureAssembly/FormMain.cs b/FurnitureAssembly/FurnitureAssembly/FormMain.cs index 2ec8027..7c72a8b 100644 --- a/FurnitureAssembly/FurnitureAssembly/FormMain.cs +++ b/FurnitureAssembly/FurnitureAssembly/FormMain.cs @@ -107,8 +107,8 @@ namespace FurnitureAssembly if (dataGridView.SelectedRows.Count == 1) { int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); - _logger.LogInformation("Заказ №{id}. Меняется статус на 'Готов'", - id); + _logger.LogInformation("Заказ №{id}. Меняется статус на 'Готов'", id); + try { var operationResult = _orderLogic.FinishOrder(new OrderBindingModel { Id = id }); @@ -116,6 +116,7 @@ namespace FurnitureAssembly { throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); } + LoadData(); } catch (Exception ex) @@ -132,8 +133,7 @@ namespace FurnitureAssembly if (dataGridView.SelectedRows.Count == 1) { int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); - _logger.LogInformation("Заказ №{id}. Меняется статус на 'Выдан'", - id); + _logger.LogInformation("Заказ №{id}. Меняется статус на 'Выдан'", id); try { var operationResult = _orderLogic.DeliveryOrder(new OrderBindingModel { Id = id }); @@ -174,5 +174,14 @@ namespace FurnitureAssembly form.ShowDialog(); } } + + private void buttonSell_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormSell)); + if (service is FormSell form) + { + form.ShowDialog(); + } + } } } diff --git a/FurnitureAssembly/FurnitureAssembly/FormSell.Designer.cs b/FurnitureAssembly/FurnitureAssembly/FormSell.Designer.cs new file mode 100644 index 0000000..90b68eb --- /dev/null +++ b/FurnitureAssembly/FurnitureAssembly/FormSell.Designer.cs @@ -0,0 +1,119 @@ +namespace FurnitureAssembly +{ + partial class FormSell + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.textBoxCount = new System.Windows.Forms.TextBox(); + this.buttonCancel = new System.Windows.Forms.Button(); + this.buttonSell = new System.Windows.Forms.Button(); + this.labelNum = new System.Windows.Forms.Label(); + this.labelFurniture = new System.Windows.Forms.Label(); + this.comboBoxFurniture = new System.Windows.Forms.ComboBox(); + this.SuspendLayout(); + // + // textBoxCount + // + this.textBoxCount.Location = new System.Drawing.Point(135, 84); + this.textBoxCount.Name = "textBoxCount"; + this.textBoxCount.Size = new System.Drawing.Size(160, 23); + this.textBoxCount.TabIndex = 14; + // + // buttonCancel + // + this.buttonCancel.Location = new System.Drawing.Point(332, 142); + this.buttonCancel.Name = "buttonCancel"; + this.buttonCancel.Size = new System.Drawing.Size(99, 29); + this.buttonCancel.TabIndex = 13; + this.buttonCancel.Text = "Отмена"; + this.buttonCancel.UseVisualStyleBackColor = true; + this.buttonCancel.Click += new System.EventHandler(this.buttonCancel_Click); + // + // buttonSell + // + this.buttonSell.Location = new System.Drawing.Point(196, 142); + this.buttonSell.Name = "buttonSell"; + this.buttonSell.Size = new System.Drawing.Size(99, 29); + this.buttonSell.TabIndex = 12; + this.buttonSell.Text = "Продать"; + this.buttonSell.UseVisualStyleBackColor = true; + this.buttonSell.Click += new System.EventHandler(this.buttonSell_Click); + // + // labelNum + // + this.labelNum.AutoSize = true; + this.labelNum.Location = new System.Drawing.Point(48, 87); + this.labelNum.Name = "labelNum"; + this.labelNum.Size = new System.Drawing.Size(72, 15); + this.labelNum.TabIndex = 11; + this.labelNum.Text = "Количество"; + // + // labelFurniture + // + this.labelFurniture.AutoSize = true; + this.labelFurniture.Location = new System.Drawing.Point(48, 46); + this.labelFurniture.Name = "labelFurniture"; + this.labelFurniture.Size = new System.Drawing.Size(53, 15); + this.labelFurniture.TabIndex = 10; + this.labelFurniture.Text = "Изделие"; + // + // comboBoxFurniture + // + this.comboBoxFurniture.FormattingEnabled = true; + this.comboBoxFurniture.Location = new System.Drawing.Point(135, 43); + this.comboBoxFurniture.Name = "comboBoxFurniture"; + this.comboBoxFurniture.Size = new System.Drawing.Size(296, 23); + this.comboBoxFurniture.TabIndex = 9; + // + // FormSell + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(479, 214); + this.Controls.Add(this.textBoxCount); + this.Controls.Add(this.buttonCancel); + this.Controls.Add(this.buttonSell); + this.Controls.Add(this.labelNum); + this.Controls.Add(this.labelFurniture); + this.Controls.Add(this.comboBoxFurniture); + this.Name = "FormSell"; + this.Text = "Продажа мебели"; + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private TextBox textBoxCount; + private Button buttonCancel; + private Button buttonSell; + private Label labelNum; + private Label labelFurniture; + private ComboBox comboBoxFurniture; + } +} \ No newline at end of file diff --git a/FurnitureAssembly/FurnitureAssembly/FormSell.cs b/FurnitureAssembly/FurnitureAssembly/FormSell.cs new file mode 100644 index 0000000..efe5584 --- /dev/null +++ b/FurnitureAssembly/FurnitureAssembly/FormSell.cs @@ -0,0 +1,117 @@ +using FurnitureAssemblyBusinessLogic; +using FurnitureAssemblyContracts.BusinessLogicsContarcts; +using FurnitureAssemblyContracts.ViewModels; +using FurnitureAssemblyDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace FurnitureAssembly +{ + public partial class FormSell : Form + { + private readonly IShopLogic _shopLogic; + private readonly List? _listFurnitures; + + public int Id + { + get + { + return Convert.ToInt32(comboBoxFurniture.SelectedValue); + } + set + { + comboBoxFurniture.SelectedValue = value; + } + } + + public IFurnitureModel? FurnitureModel + { + get + { + if (_listFurnitures == null) + { + return null; + } + foreach (var elem in _listFurnitures) + { + if (elem.Id == Id) + { + return elem; + } + } + return null; + } + } + + public int Count + { + get + { + return Convert.ToInt32(textBoxCount.Text); + } + set + { + textBoxCount.Text = value.ToString(); + } + } + + + + public FormSell(IShopLogic shopLogic, IFurnitureLogic furnitureLogic) + { + InitializeComponent(); + + _shopLogic = shopLogic; + + _listFurnitures = furnitureLogic.ReadList(null); + if (_listFurnitures != null) + { + comboBoxFurniture.DisplayMember = "FurnitureName"; + comboBoxFurniture.ValueMember = "Id"; + comboBoxFurniture.DataSource = _listFurnitures; + comboBoxFurniture.SelectedItem = null; + } + } + + private void buttonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); + } + + private void buttonSell_Click(object sender, EventArgs e) + { + if (FurnitureModel == null) + { + MessageBox.Show("Выберите изделие", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + + if (Count <= 0) + { + MessageBox.Show("Укажите количество изделия", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + + if (!_shopLogic.Sell(new() { Id = FurnitureModel.Id }, Count)) + { + MessageBox.Show("Не удалось продать изделие " + FurnitureModel.FurnitureName, "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + MessageBox.Show("Продажа прошла успешно", "Сообщение", + MessageBoxButtons.OK, MessageBoxIcon.Information); + DialogResult = DialogResult.OK; + Close(); + } + } +} diff --git a/FurnitureAssembly/FurnitureAssembly/FormSell.resx b/FurnitureAssembly/FurnitureAssembly/FormSell.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/FurnitureAssembly/FurnitureAssembly/FormSell.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/FurnitureAssembly/FurnitureAssembly/FormShop.Designer.cs b/FurnitureAssembly/FurnitureAssembly/FormShop.Designer.cs index d7dc3a8..ba32c9c 100644 --- a/FurnitureAssembly/FurnitureAssembly/FormShop.Designer.cs +++ b/FurnitureAssembly/FurnitureAssembly/FormShop.Designer.cs @@ -40,13 +40,15 @@ this.FurnitureId = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.FurnitureName = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.Count = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.textBoxMaxCount = new System.Windows.Forms.TextBox(); + this.labelMaxCount = new System.Windows.Forms.Label(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit(); this.SuspendLayout(); // // labelShopName // this.labelShopName.AutoSize = true; - this.labelShopName.Location = new System.Drawing.Point(37, 45); + this.labelShopName.Location = new System.Drawing.Point(35, 22); this.labelShopName.Name = "labelShopName"; this.labelShopName.Size = new System.Drawing.Size(113, 15); this.labelShopName.TabIndex = 0; @@ -54,14 +56,14 @@ // // textBoxName // - this.textBoxName.Location = new System.Drawing.Point(168, 42); + this.textBoxName.Location = new System.Drawing.Point(166, 19); this.textBoxName.Name = "textBoxName"; this.textBoxName.Size = new System.Drawing.Size(282, 23); this.textBoxName.TabIndex = 1; // // textBoxAddress // - this.textBoxAddress.Location = new System.Drawing.Point(168, 83); + this.textBoxAddress.Location = new System.Drawing.Point(166, 60); this.textBoxAddress.Name = "textBoxAddress"; this.textBoxAddress.Size = new System.Drawing.Size(282, 23); this.textBoxAddress.TabIndex = 3; @@ -69,7 +71,7 @@ // labelAddress // this.labelAddress.AutoSize = true; - this.labelAddress.Location = new System.Drawing.Point(37, 86); + this.labelAddress.Location = new System.Drawing.Point(35, 63); this.labelAddress.Name = "labelAddress"; this.labelAddress.Size = new System.Drawing.Size(40, 15); this.labelAddress.TabIndex = 2; @@ -78,7 +80,7 @@ // labelDate // this.labelDate.AutoSize = true; - this.labelDate.Location = new System.Drawing.Point(37, 128); + this.labelDate.Location = new System.Drawing.Point(35, 105); this.labelDate.Name = "labelDate"; this.labelDate.Size = new System.Drawing.Size(87, 15); this.labelDate.TabIndex = 4; @@ -86,7 +88,7 @@ // // dateTimePicker // - this.dateTimePicker.Location = new System.Drawing.Point(168, 122); + this.dateTimePicker.Location = new System.Drawing.Point(166, 99); this.dateTimePicker.Name = "dateTimePicker"; this.dateTimePicker.Size = new System.Drawing.Size(282, 23); this.dateTimePicker.TabIndex = 6; @@ -140,11 +142,29 @@ this.Count.HeaderText = "Количество"; this.Count.Name = "Count"; // + // textBoxMaxCount + // + this.textBoxMaxCount.Location = new System.Drawing.Point(166, 138); + this.textBoxMaxCount.Name = "textBoxMaxCount"; + this.textBoxMaxCount.Size = new System.Drawing.Size(282, 23); + this.textBoxMaxCount.TabIndex = 11; + // + // labelMaxCount + // + this.labelMaxCount.AutoSize = true; + this.labelMaxCount.Location = new System.Drawing.Point(35, 141); + this.labelMaxCount.Name = "labelMaxCount"; + this.labelMaxCount.Size = new System.Drawing.Size(80, 15); + this.labelMaxCount.TabIndex = 10; + this.labelMaxCount.Text = "Вместимость"; + // // FormShop // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(631, 530); + this.Controls.Add(this.textBoxMaxCount); + this.Controls.Add(this.labelMaxCount); this.Controls.Add(this.dataGridView1); this.Controls.Add(this.ButtonCancel); this.Controls.Add(this.ButtonSave); @@ -177,5 +197,7 @@ private DataGridViewTextBoxColumn FurnitureId; private DataGridViewTextBoxColumn FurnitureName; private DataGridViewTextBoxColumn Count; + private TextBox textBoxMaxCount; + private Label labelMaxCount; } } \ No newline at end of file diff --git a/FurnitureAssembly/FurnitureAssembly/FormShop.cs b/FurnitureAssembly/FurnitureAssembly/FormShop.cs index 5063508..7702155 100644 --- a/FurnitureAssembly/FurnitureAssembly/FormShop.cs +++ b/FurnitureAssembly/FurnitureAssembly/FormShop.cs @@ -44,6 +44,13 @@ namespace FurnitureAssembly MessageBoxButtons.OK, MessageBoxIcon.Error); return; } + + if (Convert.ToInt32(textBoxMaxCount.Text) <= 0) + { + MessageBox.Show("Некорректная вместимость", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } _logger.LogInformation("Сохранение магазина"); try { @@ -52,6 +59,7 @@ namespace FurnitureAssembly Id = _id ?? 0, ShopName = textBoxName.Text, Address = textBoxAddress.Text, + MaxCount = Convert.ToInt32(textBoxMaxCount.Text), DateOpening = dateTimePicker.Value }; var operationResult = _id.HasValue ? _logic.Update(model) : _logic.Create(model); @@ -94,6 +102,7 @@ namespace FurnitureAssembly textBoxName.Text = view.ShopName; textBoxAddress.Text = view.Address.ToString(); dateTimePicker.Value = view.DateOpening; + textBoxMaxCount.Text = view.MaxCount.ToString(); _shopFurnitures = view.Furnitures ?? new Dictionary(); LoadData(); diff --git a/FurnitureAssembly/FurnitureAssembly/FurnitureAssembly.csproj b/FurnitureAssembly/FurnitureAssembly/FurnitureAssembly.csproj index 243cc38..45d0618 100644 --- a/FurnitureAssembly/FurnitureAssembly/FurnitureAssembly.csproj +++ b/FurnitureAssembly/FurnitureAssembly/FurnitureAssembly.csproj @@ -28,6 +28,7 @@ + \ No newline at end of file diff --git a/FurnitureAssembly/FurnitureAssembly/Program.cs b/FurnitureAssembly/FurnitureAssembly/Program.cs index 2209e02..eb114b8 100644 --- a/FurnitureAssembly/FurnitureAssembly/Program.cs +++ b/FurnitureAssembly/FurnitureAssembly/Program.cs @@ -1,11 +1,10 @@ using FurnitureAssemblyBusinessLogic; using FurnitureAssemblyContracts.BusinessLogicsContarcts; using FurnitureAssemblyContracts.StoragesContracts; -using FurnitureAssemblyListImplement.Implements; +using FurnitureAssemFileImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; -using System.Drawing; namespace FurnitureAssembly { @@ -26,6 +25,7 @@ namespace FurnitureAssembly ConfigureServices(services); _serviceProvider = services.BuildServiceProvider(); Application.Run(_serviceProvider.GetRequiredService()); + } private static void ConfigureServices(ServiceCollection services) @@ -53,6 +53,7 @@ namespace FurnitureAssembly services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); } } } \ No newline at end of file diff --git a/FurnitureAssembly/FurnitureAssemblyBusinessLogic/OrderLogic.cs b/FurnitureAssembly/FurnitureAssemblyBusinessLogic/OrderLogic.cs index 9bd343e..b39df00 100644 --- a/FurnitureAssembly/FurnitureAssemblyBusinessLogic/OrderLogic.cs +++ b/FurnitureAssembly/FurnitureAssemblyBusinessLogic/OrderLogic.cs @@ -17,11 +17,13 @@ namespace FurnitureAssemblyBusinessLogic { private readonly ILogger _logger; private readonly IOrderStorage _orderStorage; + private readonly IShopLogic _shopLogic; - public OrderLogic(ILogger logger, IOrderStorage orderStorage) + public OrderLogic(ILogger logger, IOrderStorage orderStorage, IShopLogic shopLogic) { _logger = logger; _orderStorage = orderStorage; + _shopLogic = shopLogic; } private bool ChangeStatus(OrderBindingModel model, OrderStatus orderStatus) @@ -91,6 +93,13 @@ namespace FurnitureAssemblyBusinessLogic { return false; } + + if (!_shopLogic.AddFurnituresAtShops(new FurnitureBindingModel() { Id = model.FurnitureId}, model.Count)) + { + _logger.LogWarning("There are not empty places at shops. Replenishment is impossible"); + return false; + } + if (!ChangeStatus(model, OrderStatus.Выдан)) { _logger.LogWarning("Order's status is wrong"); @@ -101,7 +110,7 @@ namespace FurnitureAssemblyBusinessLogic return true; } - + public List? ReadList(OrderSearchModel? model) { _logger.LogInformation("ReadList. Id:{ Id}", model?.Id); diff --git a/FurnitureAssembly/FurnitureAssemblyBusinessLogic/ShopLogic.cs b/FurnitureAssembly/FurnitureAssemblyBusinessLogic/ShopLogic.cs index 41094d8..1aba06f 100644 --- a/FurnitureAssembly/FurnitureAssemblyBusinessLogic/ShopLogic.cs +++ b/FurnitureAssembly/FurnitureAssemblyBusinessLogic/ShopLogic.cs @@ -109,6 +109,10 @@ namespace FurnitureAssemblyBusinessLogic { throw new ArgumentNullException("Нет даты открытия магазина", nameof(model.DateOpening)); } + if (model.MaxCount <= 0) + { + throw new ArgumentNullException("Нет вместимости магазина", nameof(model.DateOpening)); + } _logger.LogInformation("Shop. ShopName:{ShopName}. Address:{ Address}. DateOpening:{ DateOpening}. Id: { Id}", model.ShopName, model.Address, model.DateOpening, model.Id); var element = _shopStorage.GetElement(new ShopSearchModel { ShopName = model.ShopName }); if (element != null && element.Id != model.Id) @@ -126,6 +130,12 @@ namespace FurnitureAssemblyBusinessLogic { return false; } + + if (GetCountFreePlaces(model.Id) < count) + { + return false; + } + if (shop.Furnitures.ContainsKey(furniture.Id)) { int prev_count = shop.Furnitures[furniture.Id].Item2; @@ -137,6 +147,7 @@ namespace FurnitureAssemblyBusinessLogic } model.Furnitures = shop.Furnitures; model.DateOpening = shop.DateOpening; + model.MaxCount = shop.MaxCount; model.Address = shop.Address; model.ShopName = shop.ShopName; if (_shopStorage.Update(model) == null) @@ -146,5 +157,56 @@ namespace FurnitureAssemblyBusinessLogic } return true; } + + private int GetCountFreePlaces(int ShopId) + { + var shop = ReadElement(new ShopSearchModel { Id = ShopId }); + if (shop == null) + return 0; + int count = shop.MaxCount; + foreach (var f in shop.Furnitures) + { + count -= f.Value.Item2; + if (count <= 0) + { + return 0; + } + } + return count; + } + + private int GetCountFreePlaces_All() + { + return _shopStorage.GetFullList().Select(x => GetCountFreePlaces(x.Id)).Sum(); + } + + public bool AddFurnituresAtShops(FurnitureBindingModel furnitureModel, int count) + { + if (GetCountFreePlaces_All() < count) + { + return false; + } + + foreach (var shop in _shopStorage.GetFullList()) + { + int countShop = GetCountFreePlaces(shop.Id); + if (countShop >= count) + { + AddFurniture(new() { Id = shop.Id }, furnitureModel, count); + break; + } + else + { + AddFurniture(new() { Id = shop.Id }, furnitureModel, countShop); + } + count -= countShop; + } + return true; + } + + public bool Sell(FurnitureBindingModel furnitureBindingModel, int count) + { + return _shopStorage.Sell(furnitureBindingModel, count); + } } } diff --git a/FurnitureAssembly/FurnitureAssemblyContracts/BindingModels/ShopBindingModel.cs b/FurnitureAssembly/FurnitureAssemblyContracts/BindingModels/ShopBindingModel.cs index 379850c..58f3782 100644 --- a/FurnitureAssembly/FurnitureAssemblyContracts/BindingModels/ShopBindingModel.cs +++ b/FurnitureAssembly/FurnitureAssemblyContracts/BindingModels/ShopBindingModel.cs @@ -16,6 +16,10 @@ namespace FurnitureAssemblyContracts.BindingModels public DateTime DateOpening { get; set; } - public Dictionary Furnitures { get; set; } = new(); + public Dictionary Furnitures { get; set; } = new(); + + public int MaxCount { get; set; } + + } } diff --git a/FurnitureAssembly/FurnitureAssemblyContracts/BusinessLogicsContarcts/IShopLogic.cs b/FurnitureAssembly/FurnitureAssemblyContracts/BusinessLogicsContarcts/IShopLogic.cs index d08b39d..f2a0151 100644 --- a/FurnitureAssembly/FurnitureAssemblyContracts/BusinessLogicsContarcts/IShopLogic.cs +++ b/FurnitureAssembly/FurnitureAssemblyContracts/BusinessLogicsContarcts/IShopLogic.cs @@ -17,5 +17,7 @@ namespace FurnitureAssemblyContracts.BusinessLogicsContarcts bool Update(ShopBindingModel model); bool Delete(ShopBindingModel model); bool AddFurniture(ShopBindingModel model, FurnitureBindingModel furnitureModel, int count); + bool AddFurnituresAtShops(FurnitureBindingModel furnitureModel, int count); + bool Sell(FurnitureBindingModel furnitureModel, int count); } } diff --git a/FurnitureAssembly/FurnitureAssemblyContracts/StoragesContracts/IShopStorage.cs b/FurnitureAssembly/FurnitureAssemblyContracts/StoragesContracts/IShopStorage.cs index cc4fa6a..5181245 100644 --- a/FurnitureAssembly/FurnitureAssemblyContracts/StoragesContracts/IShopStorage.cs +++ b/FurnitureAssembly/FurnitureAssemblyContracts/StoragesContracts/IShopStorage.cs @@ -12,6 +12,8 @@ namespace FurnitureAssemblyContracts.StoragesContracts ShopViewModel? GetElement(ShopSearchModel model); ShopViewModel? Insert(ShopBindingModel model); ShopViewModel? Update(ShopBindingModel model); - ShopViewModel? Delete(ShopBindingModel model); + ShopViewModel? Delete(ShopBindingModel model); + + bool Sell(FurnitureBindingModel furnitureBindingModel, int count); } } diff --git a/FurnitureAssembly/FurnitureAssemblyContracts/ViewModels/ShopViewModel.cs b/FurnitureAssembly/FurnitureAssemblyContracts/ViewModels/ShopViewModel.cs index 0a738fc..37982f1 100644 --- a/FurnitureAssembly/FurnitureAssemblyContracts/ViewModels/ShopViewModel.cs +++ b/FurnitureAssembly/FurnitureAssemblyContracts/ViewModels/ShopViewModel.cs @@ -18,6 +18,8 @@ namespace FurnitureAssemblyContracts.ViewModels [DisplayName("Дата открытия")] public DateTime DateOpening { get; set; } - public Dictionary Furnitures { get; set; } = new(); + public Dictionary Furnitures { get; set; } = new(); + [DisplayName("Вместимость")] + public int MaxCount { get; set; } } } diff --git a/FurnitureAssembly/FurnitureAssemblyDataModels/Models/IShopModel.cs b/FurnitureAssembly/FurnitureAssemblyDataModels/Models/IShopModel.cs index bd5efbf..b413694 100644 --- a/FurnitureAssembly/FurnitureAssemblyDataModels/Models/IShopModel.cs +++ b/FurnitureAssembly/FurnitureAssemblyDataModels/Models/IShopModel.cs @@ -12,5 +12,6 @@ namespace FurnitureAssemblyDataModels.Models string Address { get; } DateTime DateOpening { get; } Dictionary Furnitures { get; } + int MaxCount { get; } } } diff --git a/FurnitureAssembly/FurnitureAssemblyListImplement/Implements/ShopStorage.cs b/FurnitureAssembly/FurnitureAssemblyListImplement/Implements/ShopStorage.cs index 65e0e2b..cd324d0 100644 --- a/FurnitureAssembly/FurnitureAssemblyListImplement/Implements/ShopStorage.cs +++ b/FurnitureAssembly/FurnitureAssemblyListImplement/Implements/ShopStorage.cs @@ -109,5 +109,10 @@ namespace FurnitureAssemblyListImplement.Implements } return null; } + + public bool Sell(FurnitureBindingModel furnitureBindingModel, int count) + { + throw new NotImplementedException(); + } } } diff --git a/FurnitureAssembly/FurnitureAssemblyListImplement/Models/Shop.cs b/FurnitureAssembly/FurnitureAssemblyListImplement/Models/Shop.cs index 4b64a67..1a73183 100644 --- a/FurnitureAssembly/FurnitureAssemblyListImplement/Models/Shop.cs +++ b/FurnitureAssembly/FurnitureAssemblyListImplement/Models/Shop.cs @@ -21,6 +21,8 @@ namespace FurnitureAssemblyListImplement.Models public Dictionary Furnitures { get; private set; } = new(); + public int MaxCount { get; private set; } + public static Shop? Create(ShopBindingModel? model) { if (model == null) @@ -32,6 +34,7 @@ namespace FurnitureAssemblyListImplement.Models Id = model.Id, ShopName = model.ShopName, Address = model.Address, + MaxCount = model.MaxCount, DateOpening = model.DateOpening, Furnitures = model.Furnitures }; @@ -45,6 +48,7 @@ namespace FurnitureAssemblyListImplement.Models } ShopName = model.ShopName; Address = model.Address; + MaxCount = model.MaxCount; DateOpening = model.DateOpening; Furnitures = model.Furnitures; } @@ -54,6 +58,7 @@ namespace FurnitureAssemblyListImplement.Models Id = Id, ShopName = ShopName, Address = Address, + MaxCount = MaxCount, DateOpening = DateOpening, Furnitures = Furnitures };