PIbd-21_BatylkinaAO_MusoevD.../Canteen/CanteenDatabaseImplement/Implements/OrderStorage.cs

127 lines
3.8 KiB
C#
Raw Normal View History

2023-04-09 00:34:25 +04:00
using CanteenContracts.BindingModels;
using CanteenContracts.SearchModel;
2023-04-09 13:00:51 +04:00
using CanteenContracts.SearchModels;
2023-04-09 00:34:25 +04:00
using CanteenContracts.StoragesContracts;
using CanteenContracts.View;
2023-04-09 13:00:51 +04:00
using CanteenContracts.ViewModels;
2023-04-09 00:34:25 +04:00
using CanteenDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CanteenDatabaseImplement.Implements
{
public class OrderStorage : IOrderStorage
{
2023-04-09 13:00:51 +04:00
public OrderCookViewModel? GetOrderCookElement(OrderCookSearchModel model)
{
if (!model.Id.HasValue && (!model.OrderId.HasValue || !model.CookId.HasValue))
{
return null;
}
using var context = new CanteenDatabase();
return context.OrderCook
.FirstOrDefault(x => x.OrderId == model.OrderId && x.CookId == model.CookId)?.GetViewModel;
}
2023-04-09 00:34:25 +04:00
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new CanteenDatabase();
return context.Orders
.Include(x => x.Cooks)
2023-05-20 00:18:48 +04:00
.ThenInclude(x => x.Cook)
.Include(x => x.Tablewares)
.ThenInclude(x => x.Tableware)
.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)?
.GetViewModel;
2023-04-09 00:34:25 +04:00
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
if (!model.VisitorId.HasValue)
{
return new();
}
using var context = new CanteenDatabase();
return context.Orders
.Include(x => x.Cooks)
.ThenInclude(x => x.Cook)
2023-05-20 00:18:48 +04:00
.Include(x => x.Tablewares)
.ThenInclude(x => x.Tableware)
.Where(x => (model.Id.HasValue && x.Id == model.Id) || (model.VisitorId.HasValue && model.VisitorId == x.VisitorId)).Select(x => x.GetViewModel).ToList();
2023-04-09 00:34:25 +04:00
}
public List<OrderViewModel> GetFullList()
{
using var context = new CanteenDatabase();
return context.Orders
.Include(x => x.Cooks)
.ThenInclude(x => x.Cook)
.Select(x => x.GetViewModel).ToList();
}
public OrderViewModel? Insert(OrderBindingModel model)
{
var newOrder = Order.Create(model);
if (newOrder == null)
{
return null;
}
using var context = new CanteenDatabase();
context.Orders.Add(newOrder);
context.SaveChanges();
return newOrder.GetViewModel;
}
public OrderViewModel? Update(OrderBindingModel model)
{
using var context = new CanteenDatabase();
2023-05-20 00:18:48 +04:00
var order = context.Orders.FirstOrDefault(x => x.Id == model.Id);
if (order == null)
2023-04-09 00:34:25 +04:00
{
return null;
}
2023-05-20 00:18:48 +04:00
order.Update(model);
2023-04-09 00:34:25 +04:00
context.SaveChanges();
2023-05-20 00:18:48 +04:00
if (model.OrderTablewares.Any()) order.UpdateOrderTablewares(context, model);
if (model.OrderCooks.Any()) order.UpdateOrderCook(context, model);
2023-04-09 00:34:25 +04:00
2023-05-20 00:18:48 +04:00
return order.GetViewModel;
2023-04-09 00:34:25 +04:00
}
public OrderViewModel? Delete(OrderBindingModel model)
{
using var context = new CanteenDatabase();
var client = context.Orders.FirstOrDefault(x => x.Id == model.Id);
if (client == null)
{
return null;
}
context.Orders.Remove(client);
context.SaveChanges();
return client.GetViewModel;
}
}
}