From a55f771ecfc6a282ceba9e09bb5f4f37b9e9f106 Mon Sep 17 00:00:00 2001 From: tellsense Date: Sun, 17 Mar 2024 19:36:45 +0400 Subject: [PATCH] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D1=8F.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SushiBarFileImplement/OrderStorage.cs | 81 ++++++++--------- .../SushiBarFileImplement/SushiStorage.cs | 90 +++++++++---------- 2 files changed, 82 insertions(+), 89 deletions(-) diff --git a/SushiBar/SushiBarFileImplement/OrderStorage.cs b/SushiBar/SushiBarFileImplement/OrderStorage.cs index 2675d91..9cf83e3 100644 --- a/SushiBar/SushiBarFileImplement/OrderStorage.cs +++ b/SushiBar/SushiBarFileImplement/OrderStorage.cs @@ -11,76 +11,77 @@ using System.Threading.Tasks; namespace SushiBarFileImplement.Implements { - public class SushiStorage : ISushiStorage + public class OrderStorage : IOrderStorage { private readonly DataFileSingleton source; - public SushiStorage() + public OrderStorage() { source = DataFileSingleton.GetInstance(); } - public List GetFullList() - { - return source.Sushis.Select(x => x.GetViewModel).ToList(); - } + public List GetFullList() => source.Orders.Select(x => AttachSushiName(x.GetViewModel)).ToList(); - public List GetFilteredList(SushiSearchModel model) + public List GetFilteredList(OrderSearchModel model) { - if (string.IsNullOrEmpty(model.SushiName)) + if (!model.Id.HasValue) { return new(); } - return source.Sushis.Where(x => x.SushiName.Contains(model.SushiName)).Select(x => x.GetViewModel).ToList(); + return source.Orders.Where(x => x.Id == model.Id).Select(x => AttachSushiName(x.GetViewModel)).ToList(); } - public SushiViewModel? GetElement(SushiSearchModel model) + public OrderViewModel? GetElement(OrderSearchModel model) { - if (string.IsNullOrEmpty(model.SushiName) && !model.Id.HasValue) + if (!model.Id.HasValue) + { + return new(); + } + return AttachSushiName(source.Orders.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel); + } + + 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; } - return source.Sushis.FirstOrDefault(x => - (!string.IsNullOrEmpty(model.SushiName) && x.SushiName == model.SushiName) || - (model.Id.HasValue && x.Id == model.Id)) - ?.GetViewModel; + source.Orders.Add(newOrder); + source.SaveOrders(); + return AttachSushiName(newOrder.GetViewModel); } - public SushiViewModel? Insert(SushiBindingModel model) + public OrderViewModel? Update(OrderBindingModel model) { - model.Id = source.Sushis.Count > 0 ? source.Sushis.Max(x => x.Id) + 1 : 1; - var newSushi = Sushi.Create(model); - if (newSushi == null) + var order = source.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order == null) { return null; } - source.Sushis.Add(newSushi); - source.SaveSushis(); - return newSushi.GetViewModel; + order.Update(model); + source.SaveOrders(); + return AttachSushiName(order.GetViewModel); } - public SushiViewModel? Update(SushiBindingModel model) + public OrderViewModel? Delete(OrderBindingModel model) { - var Sushi = source.Sushis.FirstOrDefault(x => x.Id == model.Id); - if (Sushi == null) + var order = source.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order != null) { - return null; - } - Sushi.Update(model); - source.SaveSushis(); - return Sushi.GetViewModel; - } - - public SushiViewModel? Delete(SushiBindingModel model) - { - var Sushi = source.Sushis.FirstOrDefault(x => x.Id == model.Id); - if (Sushi != null) - { - source.Sushis.Remove(Sushi); - source.SaveSushis(); - return Sushi.GetViewModel; + source.Orders.Remove(order); + source.SaveOrders(); + return AttachSushiName(order.GetViewModel); } return null; } + + private OrderViewModel? AttachSushiName(OrderViewModel? model) + { + var sushi = source.Sushis.FirstOrDefault(x => x.Id == model?.SushiId); + model?.GetType().GetProperty("SushiName")?.SetValue(model, sushi?.SushiName); + return model; + } } } \ No newline at end of file diff --git a/SushiBar/SushiBarFileImplement/SushiStorage.cs b/SushiBar/SushiBarFileImplement/SushiStorage.cs index f6c6a2f..2675d91 100644 --- a/SushiBar/SushiBarFileImplement/SushiStorage.cs +++ b/SushiBar/SushiBarFileImplement/SushiStorage.cs @@ -11,84 +11,76 @@ using System.Threading.Tasks; namespace SushiBarFileImplement.Implements { - public class OrderStorage : IOrderStorage + public class SushiStorage : ISushiStorage { private readonly DataFileSingleton source; - public OrderStorage() + public SushiStorage() { source = DataFileSingleton.GetInstance(); } - public List GetFullList() => source.Orders.Select(x => AttachSushiName(x.GetViewModel)).ToList(); - - public List GetFilteredList(OrderSearchModel model) + public List GetFullList() { - if (!model.Id.HasValue) + return source.Sushis.Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(SushiSearchModel model) + { + if (string.IsNullOrEmpty(model.SushiName)) { return new(); } - return source.Orders.Where(x => x.Id == model.Id).Select(x => AttachSushiName(x.GetViewModel)).ToList(); + return source.Sushis.Where(x => x.SushiName.Contains(model.SushiName)).Select(x => x.GetViewModel).ToList(); } - public OrderViewModel? GetElement(OrderSearchModel model) + public SushiViewModel? GetElement(SushiSearchModel model) { - if (!model.Id.HasValue) - { - return new(); - } - return AttachSushiName(source.Orders.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel); - } - - 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) + if (string.IsNullOrEmpty(model.SushiName) && !model.Id.HasValue) { return null; } - source.Orders.Add(newOrder); - source.SaveOrders(); - return AttachSushiName(newOrder.GetViewModel); + return source.Sushis.FirstOrDefault(x => + (!string.IsNullOrEmpty(model.SushiName) && x.SushiName == model.SushiName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; } - public OrderViewModel? Update(OrderBindingModel model) + public SushiViewModel? Insert(SushiBindingModel model) { - var order = source.Orders.FirstOrDefault(x => x.Id == model.Id); - if (order == null) + model.Id = source.Sushis.Count > 0 ? source.Sushis.Max(x => x.Id) + 1 : 1; + var newSushi = Sushi.Create(model); + if (newSushi == null) { return null; } - order.Update(model); - source.SaveOrders(); - return AttachSushiName(order.GetViewModel); + source.Sushis.Add(newSushi); + source.SaveSushis(); + return newSushi.GetViewModel; } - public OrderViewModel? Delete(OrderBindingModel model) + public SushiViewModel? Update(SushiBindingModel model) { - var order = source.Orders.FirstOrDefault(x => x.Id == model.Id); - if (order != null) + var Sushi = source.Sushis.FirstOrDefault(x => x.Id == model.Id); + if (Sushi == null) { - source.Orders.Remove(order); - source.SaveOrders(); - return AttachSushiName(order.GetViewModel); + return null; + } + Sushi.Update(model); + source.SaveSushis(); + return Sushi.GetViewModel; + } + + public SushiViewModel? Delete(SushiBindingModel model) + { + var Sushi = source.Sushis.FirstOrDefault(x => x.Id == model.Id); + if (Sushi != null) + { + source.Sushis.Remove(Sushi); + source.SaveSushis(); + return Sushi.GetViewModel; } return null; } - - private OrderViewModel? AttachSushiName(OrderViewModel? model) - { - if (model == null) - { - return null; - } - var sushi = source.Sushis.FirstOrDefault(x => x.Id == model.SushiId); - if (sushi != null) - { - model.SushiName = sushi.SushiName; - } - return model; - } } } \ No newline at end of file