diff --git a/FoodOrders/FoodOrdersDatabaseImplement/Implements/ComponentStorage.cs b/FoodOrders/FoodOrdersDatabaseImplement/Implements/ComponentStorage.cs index cabf3f9..42bfaa2 100644 --- a/FoodOrders/FoodOrdersDatabaseImplement/Implements/ComponentStorage.cs +++ b/FoodOrders/FoodOrdersDatabaseImplement/Implements/ComponentStorage.cs @@ -37,8 +37,8 @@ namespace FoodOrdersDatabaseImplement.Implements } using var context = new FoodOrdersDatabase(); return context.Components - .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName == model.ComponentName) || - (model.Id.HasValue && x.Id == model.Id)) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName == model.ComponentName) || + (model.Id.HasValue && x.Id == model.Id)) ?.GetViewModel; } diff --git a/FoodOrders/FoodOrdersDatabaseImplement/Implements/OrderStorage.cs b/FoodOrders/FoodOrdersDatabaseImplement/Implements/OrderStorage.cs index 47cf51c..17de902 100644 --- a/FoodOrders/FoodOrdersDatabaseImplement/Implements/OrderStorage.cs +++ b/FoodOrders/FoodOrdersDatabaseImplement/Implements/OrderStorage.cs @@ -9,30 +9,13 @@ namespace FoodOrdersDatabaseImplement.Implements { public class OrderStorage : IOrderStorage { - public OrderViewModel? Delete(OrderBindingModel model) + public List GetFullList() { - using var context = new FoodOrdersDatabase(); - var element = context.Orders - .FirstOrDefault(rec => rec.Id == model.Id); - if (element != null) - { - context.Orders.Remove(element); - context.SaveChanges(); - return element.GetViewModel; - } - return null; - } - - public OrderViewModel? GetElement(OrderSearchModel model) - { - if (!model.Id.HasValue) - { - return null; - } using var context = new FoodOrdersDatabase(); return context.Orders - .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id) - ?.GetViewModel; + .Include(x => x.Dish) + .Select(x => x.GetViewModel) + .ToList(); } public List GetFilteredList(OrderSearchModel model) @@ -43,37 +26,23 @@ namespace FoodOrdersDatabaseImplement.Implements } using var context = new FoodOrdersDatabase(); return context.Orders + .Include(x => x.Dish) .Where(x => x.Id == model.Id) .Select(x => x.GetViewModel) .ToList(); } - private static OrderViewModel GetViewModel(Order order) - { - var viewModel = order.GetViewModel; - using var context = new FoodOrdersDatabase(); - var element = context.Dishes - .FirstOrDefault(x => x.Id == order.DishId); - viewModel.DishName = element.DishName; - return viewModel; - } - - public List GetFullList() + public OrderViewModel? GetElement(OrderSearchModel model) { + if (!model.Id.HasValue) + { + return null; + } using var context = new FoodOrdersDatabase(); return context.Orders - .Select(x => new OrderViewModel - { - Id = x.Id, - DishId = x.DishId, - Count = x.Count, - Sum = x.Sum, - Status = x.Status, - DateCreate = x.DateCreate, - DateImplement = x.DateImplement, - DishName = x.Dish.DishName - }) - .ToList(); + .Include(x => x.Dish) + .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id) + ?.GetViewModel; } public OrderViewModel? Insert(OrderBindingModel model) @@ -101,5 +70,17 @@ namespace FoodOrdersDatabaseImplement.Implements context.SaveChanges(); return order.GetViewModel; } + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new FoodOrdersDatabase(); + var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Orders.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } } } diff --git a/FoodOrders/FoodOrdersDatabaseImplement/Models/Dish.cs b/FoodOrders/FoodOrdersDatabaseImplement/Models/Dish.cs index 9bbcaf4..43eadf7 100644 --- a/FoodOrders/FoodOrdersDatabaseImplement/Models/Dish.cs +++ b/FoodOrders/FoodOrdersDatabaseImplement/Models/Dish.cs @@ -18,6 +18,7 @@ namespace FoodOrdersDatabaseImplement.Models private Dictionary? _dishComponents = null; + //?? [NotMapped] public Dictionary DishComponents { @@ -25,8 +26,7 @@ namespace FoodOrdersDatabaseImplement.Models { if (_dishComponents == null) { - _dishComponents = Components - .ToDictionary(recPC => recPC.ComponentId, recPC => (recPC.Component as IComponentModel, recPC.Count)); + _dishComponents = Components.ToDictionary(recPC => recPC.ComponentId, recPC => (recPC.Component as IComponentModel, recPC.Count)); } return _dishComponents; } @@ -68,7 +68,7 @@ namespace FoodOrdersDatabaseImplement.Models { var dishComponents = context.DishComponents.Where(rec => rec.DishId == model.Id).ToList(); if (dishComponents != null && dishComponents.Count > 0) - { // удалили те, которых нет в модели + { // удалили те в бд, которых нет в модели context.DishComponents.RemoveRange(dishComponents.Where(rec => !model.DishComponents.ContainsKey(rec.ComponentId))); context.SaveChanges(); // обновили количество у существующих записей @@ -80,13 +80,14 @@ namespace FoodOrdersDatabaseImplement.Models context.SaveChanges(); } var dish = context.Dishes.First(x => x.Id == Id); - foreach (var pc in model.DishComponents) + //добавляем в бд блюда которые есть в моделе, но ещё нет в бд + foreach (var dc in model.DishComponents) { context.DishComponents.Add(new DishComponent { Dish = dish, - Component = context.Components.First(x => x.Id == pc.Key), - Count = pc.Value.Item2 + Component = context.Components.First(x => x.Id == dc.Key), + Count = dc.Value.Item2 }); context.SaveChanges(); } diff --git a/FoodOrders/FoodOrdersDatabaseImplement/Models/Order.cs b/FoodOrders/FoodOrdersDatabaseImplement/Models/Order.cs index 5c694ef..9a5ac96 100644 --- a/FoodOrders/FoodOrdersDatabaseImplement/Models/Order.cs +++ b/FoodOrders/FoodOrdersDatabaseImplement/Models/Order.cs @@ -8,6 +8,8 @@ namespace FoodOrdersDatabaseImplement.Models { public class Order : IOrderModel { + public int Id { get; set; } + [Required] public int DishId { get; set; } @@ -27,8 +29,6 @@ namespace FoodOrdersDatabaseImplement.Models public DateTime? DateImplement { get; set; } - public int Id { get; set; } - public static Order? Create(OrderBindingModel? model) { if (model == null) @@ -65,7 +65,8 @@ namespace FoodOrdersDatabaseImplement.Models Sum = Sum, Status = Status, DateCreate = DateCreate, - DateImplement = DateImplement + DateImplement = DateImplement, + DishName = Dish.DishName }; } } \ No newline at end of file