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();
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;
}

View File

@ -9,30 +9,13 @@ namespace FoodOrdersDatabaseImplement.Implements
{
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();
return context.Orders
.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)
?.GetViewModel;
.Include(x => x.Dish)
.Select(x => x.GetViewModel)
.ToList();
}
public List<OrderViewModel> 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<OrderViewModel> 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;
}
}
}

View File

@ -18,6 +18,7 @@ namespace FoodOrdersDatabaseImplement.Models
private Dictionary<int, (IComponentModel, int)>? _dishComponents = null;
//??
[NotMapped]
public Dictionary<int, (IComponentModel, int)> 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();
}

View File

@ -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
};
}
}