This commit is contained in:
Николай 2023-03-23 18:18:58 +04:00
parent 2ca13faec0
commit 611f227f4e
4 changed files with 38 additions and 55 deletions

View File

@ -37,8 +37,8 @@ namespace FoodOrdersDatabaseImplement.Implements
} }
using var context = new FoodOrdersDatabase(); using var context = new FoodOrdersDatabase();
return context.Components return context.Components
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName == model.ComponentName) || .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName == model.ComponentName) ||
(model.Id.HasValue && x.Id == model.Id)) (model.Id.HasValue && x.Id == model.Id))
?.GetViewModel; ?.GetViewModel;
} }

View File

@ -9,30 +9,13 @@ namespace FoodOrdersDatabaseImplement.Implements
{ {
public class OrderStorage : IOrderStorage public class OrderStorage : IOrderStorage
{ {
public OrderViewModel? Delete(OrderBindingModel model) public List<OrderViewModel> 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(); using var context = new FoodOrdersDatabase();
return context.Orders return context.Orders
.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id) .Include(x => x.Dish)
?.GetViewModel; .Select(x => x.GetViewModel)
.ToList();
} }
public List<OrderViewModel> GetFilteredList(OrderSearchModel model) public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
@ -43,37 +26,23 @@ namespace FoodOrdersDatabaseImplement.Implements
} }
using var context = new FoodOrdersDatabase(); using var context = new FoodOrdersDatabase();
return context.Orders return context.Orders
.Include(x => x.Dish)
.Where(x => x.Id == model.Id) .Where(x => x.Id == model.Id)
.Select(x => x.GetViewModel) .Select(x => x.GetViewModel)
.ToList(); .ToList();
} }
private static OrderViewModel GetViewModel(Order order) public OrderViewModel? GetElement(OrderSearchModel model)
{
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<OrderViewModel> GetFullList()
{ {
if (!model.Id.HasValue)
{
return null;
}
using var context = new FoodOrdersDatabase(); using var context = new FoodOrdersDatabase();
return context.Orders return context.Orders
.Select(x => new OrderViewModel .Include(x => x.Dish)
{ .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)
Id = x.Id, ?.GetViewModel;
DishId = x.DishId,
Count = x.Count,
Sum = x.Sum,
Status = x.Status,
DateCreate = x.DateCreate,
DateImplement = x.DateImplement,
DishName = x.Dish.DishName
})
.ToList();
} }
public OrderViewModel? Insert(OrderBindingModel model) public OrderViewModel? Insert(OrderBindingModel model)
@ -101,5 +70,17 @@ namespace FoodOrdersDatabaseImplement.Implements
context.SaveChanges(); context.SaveChanges();
return order.GetViewModel; 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;
}
} }
} }

View File

@ -18,6 +18,7 @@ namespace FoodOrdersDatabaseImplement.Models
private Dictionary<int, (IComponentModel, int)>? _dishComponents = null; private Dictionary<int, (IComponentModel, int)>? _dishComponents = null;
//??
[NotMapped] [NotMapped]
public Dictionary<int, (IComponentModel, int)> DishComponents public Dictionary<int, (IComponentModel, int)> DishComponents
{ {
@ -25,8 +26,7 @@ namespace FoodOrdersDatabaseImplement.Models
{ {
if (_dishComponents == null) if (_dishComponents == null)
{ {
_dishComponents = Components _dishComponents = Components.ToDictionary(recPC => recPC.ComponentId, recPC => (recPC.Component as IComponentModel, recPC.Count));
.ToDictionary(recPC => recPC.ComponentId, recPC => (recPC.Component as IComponentModel, recPC.Count));
} }
return _dishComponents; return _dishComponents;
} }
@ -68,7 +68,7 @@ namespace FoodOrdersDatabaseImplement.Models
{ {
var dishComponents = context.DishComponents.Where(rec => rec.DishId == model.Id).ToList(); var dishComponents = context.DishComponents.Where(rec => rec.DishId == model.Id).ToList();
if (dishComponents != null && dishComponents.Count > 0) if (dishComponents != null && dishComponents.Count > 0)
{ // удалили те, которых нет в модели { // удалили те в бд, которых нет в модели
context.DishComponents.RemoveRange(dishComponents.Where(rec => !model.DishComponents.ContainsKey(rec.ComponentId))); context.DishComponents.RemoveRange(dishComponents.Where(rec => !model.DishComponents.ContainsKey(rec.ComponentId)));
context.SaveChanges(); context.SaveChanges();
// обновили количество у существующих записей // обновили количество у существующих записей
@ -80,13 +80,14 @@ namespace FoodOrdersDatabaseImplement.Models
context.SaveChanges(); context.SaveChanges();
} }
var dish = context.Dishes.First(x => x.Id == Id); 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 context.DishComponents.Add(new DishComponent
{ {
Dish = dish, Dish = dish,
Component = context.Components.First(x => x.Id == pc.Key), Component = context.Components.First(x => x.Id == dc.Key),
Count = pc.Value.Item2 Count = dc.Value.Item2
}); });
context.SaveChanges(); context.SaveChanges();
} }

View File

@ -8,6 +8,8 @@ namespace FoodOrdersDatabaseImplement.Models
{ {
public class Order : IOrderModel public class Order : IOrderModel
{ {
public int Id { get; set; }
[Required] [Required]
public int DishId { get; set; } public int DishId { get; set; }
@ -27,8 +29,6 @@ namespace FoodOrdersDatabaseImplement.Models
public DateTime? DateImplement { get; set; } public DateTime? DateImplement { get; set; }
public int Id { get; set; }
public static Order? Create(OrderBindingModel? model) public static Order? Create(OrderBindingModel? model)
{ {
if (model == null) if (model == null)
@ -65,7 +65,8 @@ namespace FoodOrdersDatabaseImplement.Models
Sum = Sum, Sum = Sum,
Status = Status, Status = Status,
DateCreate = DateCreate, DateCreate = DateCreate,
DateImplement = DateImplement DateImplement = DateImplement,
DishName = Dish.DishName
}; };
} }
} }