diff --git a/SushiBar/Program.cs b/SushiBar/Program.cs index 6afbfbd..884a36f 100644 --- a/SushiBar/Program.cs +++ b/SushiBar/Program.cs @@ -4,7 +4,7 @@ using NLog.Extensions.Logging; using SushiBarBusinessLogic.BusinessLogic; using SushiBarContracts.BusinessLogicsContracts; using SushiBarContracts.StoragesContracts; -using SushiBarListImplements.Implements; +using SushiBarFileImplement.Implements; using SushiBarView; namespace SushiBar diff --git a/SushiBar/SushiBarView.csproj b/SushiBar/SushiBarView.csproj index fbd600f..8bedfbf 100644 --- a/SushiBar/SushiBarView.csproj +++ b/SushiBar/SushiBarView.csproj @@ -19,6 +19,7 @@ + diff --git a/SushiBarFileImplement/Implements/ComponentStorage.cs b/SushiBarFileImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..accedb3 --- /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; + } + } +} \ No newline at end of file diff --git a/SushiBarFileImplement/Implements/OrderStorage.cs b/SushiBarFileImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..d0cd47c --- /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; + } + } +} \ No newline at end of file diff --git a/SushiBarFileImplement/Implements/SushiStorage.cs b/SushiBarFileImplement/Implements/SushiStorage.cs new file mode 100644 index 0000000..2845db6 --- /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; + } + } +} \ No newline at end of file diff --git a/SushiBarFileImplement/SushiBarFileImplement.csproj b/SushiBarFileImplement/SushiBarFileImplement.csproj index 185d139..50a9ff9 100644 --- a/SushiBarFileImplement/SushiBarFileImplement.csproj +++ b/SushiBarFileImplement/SushiBarFileImplement.csproj @@ -6,10 +6,6 @@ enable - - - -