Принятая лаба.

This commit is contained in:
Артем Харламов 2023-04-03 10:00:54 +04:00
parent f718fd533d
commit 3c6b43efd8

View File

@ -14,16 +14,13 @@ namespace AbstractFoodOrdersDatabaseImplement.Implements
{
public class OrderStorage : IOrderStorage
{
public OrderViewModel? GetElement(OrderSearchModel model)
public List<OrderViewModel> GetFullList()
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new AbstractFoodOrdersDatabase();
return context.Orders.Include(x => x.Dish).FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
return context.Orders
.Select(x => AccessDishStorage(x.GetViewModel, context))
.ToList();
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
if (!model.Id.HasValue)
@ -33,17 +30,20 @@ namespace AbstractFoodOrdersDatabaseImplement.Implements
using var context = new AbstractFoodOrdersDatabase();
return context.Orders
.Where(x => x.Id == model.Id)
.Include(x => x.Dish)
.Select(x => x.GetViewModel)
.Select(x => AccessDishStorage(x.GetViewModel, context))
.ToList();
}
public List<OrderViewModel> GetFullList()
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return new();
}
using var context = new AbstractFoodOrdersDatabase();
return context.Orders.Include(x => x.Dish).Select(x => x.GetViewModel).ToList();
return AccessDishStorage(context.Orders
.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)
?.GetViewModel, context);
}
public OrderViewModel? Insert(OrderBindingModel model)
{
var newOrder = Order.Create(model);
@ -54,9 +54,8 @@ namespace AbstractFoodOrdersDatabaseImplement.Implements
using var context = new AbstractFoodOrdersDatabase();
context.Orders.Add(newOrder);
context.SaveChanges();
return context.Orders.Include(x => x.Dish).FirstOrDefault(x => x.Id == newOrder.Id)?.GetViewModel;
return AccessDishStorage(newOrder.GetViewModel, context);
}
public OrderViewModel? Update(OrderBindingModel model)
{
using var context = new AbstractFoodOrdersDatabase();
@ -67,7 +66,7 @@ namespace AbstractFoodOrdersDatabaseImplement.Implements
}
order.Update(model);
context.SaveChanges();
return context.Orders.Include(x => x.Dish).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
return AccessDishStorage(order.GetViewModel, context);
}
public OrderViewModel? Delete(OrderBindingModel model)
{
@ -77,9 +76,16 @@ namespace AbstractFoodOrdersDatabaseImplement.Implements
{
context.Orders.Remove(element);
context.SaveChanges();
return element.GetViewModel;
return AccessDishStorage(element.GetViewModel, context);
}
return null;
}
static OrderViewModel AccessDishStorage(OrderViewModel model, AbstractFoodOrdersDatabase context)
{
if (model == null) return model;
string? dishName = context.Dishes.FirstOrDefault(x => x.Id == model.DishId)?.DishName;
if (dishName != null) model.DishName = dishName;
return model;
}
}
}