diff --git a/FlowerShopDatabaseImplement/Order.cs b/FlowerShopDatabaseImplement/Order.cs index 853a9d3..16bc754 100644 --- a/FlowerShopDatabaseImplement/Order.cs +++ b/FlowerShopDatabaseImplement/Order.cs @@ -27,9 +27,9 @@ namespace FlowerShopDatabaseImplement.Models public DateTime? DateImplement { get; private set; } [Required] public int FlowerId { get; private set; } - public virtual Flower Flower { get; private set; } + public virtual Flower? Flower { get; private set; } - public static Order? Create(FlowerShopDataBase context,OrderBindingModel model) + public static Order? Create(OrderBindingModel model) { if (model == null) { @@ -44,7 +44,7 @@ namespace FlowerShopDatabaseImplement.Models DateCreate = model.DateCreate, DateImplement = model.DateImplement, FlowerId = model.FlowerId, - Flower = context.Flowers.FirstOrDefault(x => x.Id == model.FlowerId) + //Flower = context.Flowers.FirstOrDefault(x => x.Id == model.FlowerId) }; } @@ -66,7 +66,7 @@ namespace FlowerShopDatabaseImplement.Models Status = Status, DateCreate = DateCreate, DateImplement = DateImplement, - FlowerName = Flower.FlowerName, + FlowerName = Flower?.FlowerName ?? String.Empty , Id = Id, }; diff --git a/FlowerShopDatabaseImplement/OrderStorage.cs b/FlowerShopDatabaseImplement/OrderStorage.cs index e76132b..e163d79 100644 --- a/FlowerShopDatabaseImplement/OrderStorage.cs +++ b/FlowerShopDatabaseImplement/OrderStorage.cs @@ -19,7 +19,7 @@ namespace FlowerShopDatabaseImplement.Implements { using var context = new FlowerShopDataBase(); return context.Orders.Include(x => x.Flower) - .Select(x => AccessFlowerStorage(x.GetViewModel)) + .Select(x => x.GetViewModel) .ToList(); } public List GetFilteredList(OrderSearchModel model) @@ -31,7 +31,7 @@ namespace FlowerShopDatabaseImplement.Implements using var context = new FlowerShopDataBase(); return context.Orders.Include(x => x.Flower) .Where(x => x.Id == model.Id) - .Select(x => AccessFlowerStorage(x.GetViewModel)) + .Select(x => x.GetViewModel) .ToList(); } public OrderViewModel? GetElement(OrderSearchModel model) @@ -41,19 +41,19 @@ namespace FlowerShopDatabaseImplement.Implements return null; } using var context = new FlowerShopDataBase(); - return AccessFlowerStorage(context.Orders.Include(x => x.Flower).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel); + return context.Orders.Include(x => x.Flower).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; } public OrderViewModel? Insert(OrderBindingModel model) { using var context = new FlowerShopDataBase(); - var newOrder = Order.Create(context, model); + var newOrder = Order.Create( model); if (newOrder == null) { return null; } context.Orders.Add(newOrder); context.SaveChanges(); - return AccessFlowerStorage(newOrder.GetViewModel); + return newOrder.GetViewModel; } public OrderViewModel? Update(OrderBindingModel model) { @@ -65,36 +65,35 @@ namespace FlowerShopDatabaseImplement.Implements } order.Update(model); context.SaveChanges(); - return AccessFlowerStorage(order.GetViewModel); + return order.GetViewModel; } public OrderViewModel? Delete(OrderBindingModel model) { using var context = new FlowerShopDataBase(); - var element = context.Orders.Include(x => x.Flower).FirstOrDefault(rec => rec.Id == - model.Id); + var element = context.Orders.Include(x => x.Flower).FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { context.Orders.Remove(element); context.SaveChanges(); - return AccessFlowerStorage(element.GetViewModel); + return element.GetViewModel; } return null; } - public static OrderViewModel AccessFlowerStorage(OrderViewModel model) - { - if (model == null) - return null; - using var context = new FlowerShopDataBase(); - foreach (var flower in context.Flowers) - { - if (flower.Id == model.FlowerId) - { - model.FlowerName = flower.FlowerName; - break; - } - } - return model; - } + //public static OrderViewModel AccessFlowerStorage(OrderViewModel model) + //{ + // if (model == null) + // return null; + // using var context = new FlowerShopDataBase(); + // foreach (var flower in context.Flowers) + // { + // if (flower.Id == model.FlowerId) + // { + // model.FlowerName = flower.FlowerName; + // break; + // } + // } + // return model; + //} } }