diff --git a/SushiBar/Program.cs b/SushiBar/Program.cs index a0a80e2..0ace9a1 100644 --- a/SushiBar/Program.cs +++ b/SushiBar/Program.cs @@ -1,10 +1,11 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; + using SushiBarBusinessLogic.BusinessLogic; using SushiBarContracts.BusinessLogicsContracts; using SushiBarContracts.StoragesContracts; -using SushiBarListImplement.Implements; +using SushiBarFileImplement.Implements; using SushiBarView; namespace SushiBar @@ -28,7 +29,6 @@ namespace SushiBar ConfigureServices(services); _serviceProvider = services.BuildServiceProvider(); Application.Run(_serviceProvider.GetRequiredService()); - //Application.Run(new Form1()); } private static void ConfigureServices(ServiceCollection services) @@ -41,9 +41,11 @@ namespace SushiBar services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); diff --git a/SushiBar/SushiBarView.csproj b/SushiBar/SushiBarView.csproj index 4a36d5f..fadac92 100644 --- a/SushiBar/SushiBarView.csproj +++ b/SushiBar/SushiBarView.csproj @@ -34,6 +34,7 @@ + diff --git a/SushiBarFileImplement/DataFileSingleton.cs b/SushiBarFileImplement/DataFileSingleton.cs index 9ec90da..5e0dfa2 100644 --- a/SushiBarFileImplement/DataFileSingleton.cs +++ b/SushiBarFileImplement/DataFileSingleton.cs @@ -34,7 +34,7 @@ namespace SushiBarFileImplement { Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; Sushis = LoadData(SushiFileName, "Sushi", x => Sushi.Create(x)!)!; - Orders = new List(); + Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; } private static List? LoadData(string filename, string xmlNodeName, Func selectFunction) diff --git a/SushiBarFileImplement/Implements/ComponentStorage.cs b/SushiBarFileImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..a4f138a --- /dev/null +++ b/SushiBarFileImplement/Implements/ComponentStorage.cs @@ -0,0 +1,72 @@ +using SushiBarContracts.BindingModel; +using SushiBarContracts.SearchModel; +using SushiBarContracts.StoragesContracts; +using SushiBarContracts.ViewModels; +using SushiBarFileImplement.Models; + +namespace SushiBarFileImplement.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/SushiBarFileImplement/Implements/OrderStorage.cs b/SushiBarFileImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..375b2f7 --- /dev/null +++ b/SushiBarFileImplement/Implements/OrderStorage.cs @@ -0,0 +1,83 @@ +using SushiBarContracts.BindingModel; +using SushiBarContracts.SearchModel; +using SushiBarContracts.StoragesContracts; +using SushiBarContracts.ViewModels; +using SushiBarFileImplement.Models; + +namespace SushiBarFileImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + private readonly DataFileSingleton source; + + public OrderStorage() + { + source = DataFileSingleton.GetInstance(); + } + public List GetFullList() + { + return source.Orders.Select(x => GetViewModel(x)).ToList(); + } + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + return source.Orders.Where(x => x.Id == model.Id).Select(x => GetViewModel(x)).ToList(); + } + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + return GetViewModel(source.Orders.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)); + } + 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 GetViewModel(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 GetViewModel(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 GetViewModel(element); + } + return null; + } + + public OrderViewModel GetViewModel(Order order) + { + var viewModel = order.GetViewModel; + var sushi = source.Sushis.FirstOrDefault(x => x.Id == order.SushiId); + if(sushi != null) + { + viewModel.SushiName = sushi.SushiName; + } + return viewModel; + } + } +} diff --git a/SushiBarFileImplement/Implements/SushiStorage.cs b/SushiBarFileImplement/Implements/SushiStorage.cs new file mode 100644 index 0000000..956a984 --- /dev/null +++ b/SushiBarFileImplement/Implements/SushiStorage.cs @@ -0,0 +1,77 @@ +using SushiBarContracts.BindingModel; +using SushiBarContracts.SearchModel; +using SushiBarContracts.StoragesContracts; +using SushiBarContracts.ViewModels; +using SushiBarFileImplement.Models; + + +namespace SushiBarFileImplement.Implements +{ + public class SushiStorage : ISushiStorage + { + private readonly DataFileSingleton source; + public SushiStorage() + { + source = DataFileSingleton.GetInstance(); + } + + public List GetFullList() + { + return source.Sushis.Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(SushiSearchModel model) + { + if (string.IsNullOrEmpty(model.SushiName)) + { + return new(); + } + return source.Sushis.Where(x => x.SushiName.Contains(model.SushiName)).Select(x => x.GetViewModel).ToList(); + } + + public SushiViewModel? GetElement(SushiSearchModel model) + { + if (string.IsNullOrEmpty(model.SushiName) && !model.Id.HasValue) + { + return null; + } + return source.Sushis.FirstOrDefault(x => (!string.IsNullOrEmpty(model.SushiName) && + x.SushiName == model.SushiName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public SushiViewModel? Insert(SushiBindingModel model) + { + model.Id = source.Sushis.Count > 0 ? source.Sushis.Max(x => x.Id) + 1 : 1; + var newSushi = Sushi.Create(model); + if (newSushi == null) + { + return null; + } + source.Sushis.Add(newSushi); + source.SaveSushis(); + return newSushi.GetViewModel; + } + public SushiViewModel? Update(SushiBindingModel model) + { + var sushi = source.Sushis.FirstOrDefault(x => x.Id == model.Id); + if (sushi == null) + { + return null; + } + sushi.Update(model); + source.SaveSushis(); + return sushi.GetViewModel; + } + public SushiViewModel? Delete(SushiBindingModel model) + { + var element = source.Sushis.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + source.Sushis.Remove(element); + source.SaveSushis(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/SushiBarFileImplement/Models/Order.cs b/SushiBarFileImplement/Models/Order.cs index b3d6258..97b723b 100644 --- a/SushiBarFileImplement/Models/Order.cs +++ b/SushiBarFileImplement/Models/Order.cs @@ -11,11 +11,11 @@ namespace SushiBarFileImplement.Models public int Id { get; private set; } public int SushiId { get; private set; } public int Count { get; private set; } - public double Sum { get; set; } - public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; - public DateTime DateCreate { get; set; } = DateTime.Now; - public DateTime? DateImplement { get; set; } - public static Order? Create(OrderBindingModel model) + 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 static Order? Create(OrderBindingModel? model) { if (model == null) { @@ -28,7 +28,8 @@ namespace SushiBarFileImplement.Models Count = model.Count, Sum = model.Sum, Status = model.Status, - DateCreate = model.DateCreate + DateCreate = model.DateCreate, + DateImplement = model.DateImplement }; } public static Order? Create(XElement element) @@ -43,7 +44,7 @@ namespace SushiBarFileImplement.Models SushiId = Convert.ToInt32(element.Element("SushiId")!.Value), Count = Convert.ToInt32(element.Element("Count")!.Value), Sum = Convert.ToDouble(element.Element("Sum")!.Value), - Status = (OrderStatus)Convert.ToInt32(element.Element("Status")!.Value), + Status = (OrderStatus)Enum.Parse(typeof(OrderStatus),element.Element("Status")!.Value), DateCreate = Convert.ToDateTime(element.Element("DateCreate")!.Value), DateImplement = string.IsNullOrEmpty(element.Element("DateImplement")!.Value) ? null : Convert.ToDateTime(element.Element("DateImplement")!.Value) @@ -62,6 +63,7 @@ namespace SushiBarFileImplement.Models { Id = Id, SushiId = SushiId, + Count = Count, Sum = Sum, Status = Status, DateCreate = DateCreate, diff --git a/SushiBarFileImplement/Models/Sushi.cs b/SushiBarFileImplement/Models/Sushi.cs index f2aaae8..b5d914c 100644 --- a/SushiBarFileImplement/Models/Sushi.cs +++ b/SushiBarFileImplement/Models/Sushi.cs @@ -20,8 +20,10 @@ namespace SushiBarFileImplement.Models if (_sushiComponents == null) { var source = DataFileSingleton.GetInstance(); - _sushiComponents = Components.ToDictionary(x => x.Key, y => ((source.Components.FirstOrDefault(z => z.Id == y.Key) - as IComponentModel)!, y.Value)); + _sushiComponents = Components.ToDictionary(x => x.Key, y => + ((source.Components.FirstOrDefault(z => z.Id == y.Key) + as IComponentModel)!, y.Value) + ); } return _sushiComponents; } diff --git a/SushiBarFileImplement/SushiBarFileImplement.csproj b/SushiBarFileImplement/SushiBarFileImplement.csproj index 1716a8e..04be97c 100644 --- a/SushiBarFileImplement/SushiBarFileImplement.csproj +++ b/SushiBarFileImplement/SushiBarFileImplement.csproj @@ -12,8 +12,4 @@ - - - - diff --git a/SushiBarListImplement/Order.cs b/SushiBarListImplement/Order.cs index a872d8f..ad2c3b1 100644 --- a/SushiBarListImplement/Order.cs +++ b/SushiBarListImplement/Order.cs @@ -42,11 +42,7 @@ namespace SushiBarListImplement.Models { return; } - SushiId = model.SushiId; - Count = model.Count; - Sum = model.Sum; Status = model.Status; - DateCreate = model.DateCreate; DateImplement = model.DateImplement; }