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 LunchStorage : ILunchStorage
|
|
|
|
|
{
|
|
|
|
|
public LunchViewModel? GetElement(LunchSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (!model.Id.HasValue && !model.DateFrom.HasValue && !model.DateTo.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using var context = new CanteenDatabase();
|
|
|
|
|
|
|
|
|
|
return context.Lunches
|
|
|
|
|
.Include(x => x.Products)
|
|
|
|
|
.ThenInclude(x => x.Product)
|
2023-05-20 00:18:48 +04:00
|
|
|
|
.Include(x => x.Orders)
|
|
|
|
|
.ThenInclude(x => x.Order)
|
2023-04-09 13:00:51 +04:00
|
|
|
|
.FirstOrDefault(x => (x.DateCreate >= model.DateFrom && x.DateImplement <= model.DateTo) || (x.Id == model.Id))?.GetViewModel;
|
2023-04-09 00:34:25 +04:00
|
|
|
|
}
|
2023-04-09 13:00:51 +04:00
|
|
|
|
public LunchOrderViewModel? GetLunchOrderElement(LunchOrderSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (!model.Id.HasValue && !model.LunchId.HasValue && !model.OrderId.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2023-04-09 00:34:25 +04:00
|
|
|
|
|
2023-04-09 13:00:51 +04:00
|
|
|
|
using var context = new CanteenDatabase();
|
|
|
|
|
|
|
|
|
|
return context.LunchOrder
|
|
|
|
|
.FirstOrDefault(x => (x.LunchId == model.LunchId && x.OrderId == model.OrderId) || (x.Id == model.Id))?.GetViewModel;
|
|
|
|
|
}
|
2023-04-09 00:34:25 +04:00
|
|
|
|
public List<LunchViewModel> GetFilteredList(LunchSearchModel model)
|
|
|
|
|
{
|
2023-05-20 00:18:48 +04:00
|
|
|
|
if (!model.VisitorId.HasValue)
|
2023-04-09 00:34:25 +04:00
|
|
|
|
{
|
|
|
|
|
return new();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using var context = new CanteenDatabase();
|
|
|
|
|
|
|
|
|
|
return context.Lunches
|
2023-05-20 00:18:48 +04:00
|
|
|
|
.Include(x => x.Orders)
|
|
|
|
|
.ThenInclude(x => x.Order)
|
2023-05-20 12:11:48 +04:00
|
|
|
|
.Include(x => x.Products)
|
|
|
|
|
.ThenInclude(x => x.Product)
|
|
|
|
|
.ThenInclude(x => x.Cooks)
|
|
|
|
|
.ThenInclude(x => x.Cook)
|
2023-05-16 19:13:19 +04:00
|
|
|
|
.Where(x =>
|
|
|
|
|
(x.DateCreate >= model.DateFrom && x.DateImplement <= model.DateTo) ||
|
|
|
|
|
(model.Id.HasValue && x.Id == model.Id) ||
|
|
|
|
|
(model.VisitorId.HasValue && model.VisitorId == x.VisitorId))
|
2023-04-09 00:34:25 +04:00
|
|
|
|
.Select(x => x.GetViewModel).ToList();
|
|
|
|
|
}
|
|
|
|
|
public List<LunchViewModel> GetFullList()
|
|
|
|
|
{
|
|
|
|
|
using var context = new CanteenDatabase();
|
|
|
|
|
return context.Lunches
|
2023-05-20 00:18:48 +04:00
|
|
|
|
.Include(x => x.Orders)
|
|
|
|
|
.ThenInclude(x => x.Order)
|
2023-05-20 12:11:48 +04:00
|
|
|
|
.Include(x => x.Products)
|
|
|
|
|
.ThenInclude(x => x.Product)
|
2023-04-09 00:34:25 +04:00
|
|
|
|
.Select(x => x.GetViewModel).ToList();
|
|
|
|
|
}
|
|
|
|
|
public LunchViewModel? Insert(LunchBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new CanteenDatabase();
|
|
|
|
|
|
|
|
|
|
using var transaction = context.Database.BeginTransaction();
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var newLunch = Lunch.Create(context, model);
|
|
|
|
|
if (newLunch == null)
|
|
|
|
|
{
|
|
|
|
|
transaction.Rollback();
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
context.Lunches.Add(newLunch);
|
|
|
|
|
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
context.Database.CommitTransaction();
|
|
|
|
|
|
|
|
|
|
return newLunch.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
transaction.Rollback();
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public LunchViewModel? Update(LunchBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new CanteenDatabase();
|
|
|
|
|
|
|
|
|
|
using var transaction = context.Database.BeginTransaction();
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var lunch = context.Lunches.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
|
if (lunch == null)
|
|
|
|
|
{
|
|
|
|
|
transaction.Rollback();
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lunch.Update(model);
|
|
|
|
|
|
|
|
|
|
context.SaveChanges();
|
2023-05-20 00:18:48 +04:00
|
|
|
|
if (model.LunchProducts.Any()) lunch.UpdateProducts(context, model);
|
|
|
|
|
if (model.LunchOrders.Any()) lunch.UpdateOrders(context, model);
|
2023-04-09 00:34:25 +04:00
|
|
|
|
context.Database.CommitTransaction();
|
|
|
|
|
|
|
|
|
|
return lunch.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
transaction.Rollback();
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public LunchViewModel? Delete(LunchBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new CanteenDatabase();
|
|
|
|
|
|
|
|
|
|
var element = context.Lunches.FirstOrDefault(rec => rec.Id == model.Id);
|
|
|
|
|
if (element != null)
|
|
|
|
|
{
|
|
|
|
|
context.Lunches.Remove(element);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
|
|
|
|
|
return element.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|