PIbd-21_BatylkinaAO_MusoevD.../Canteen/CanteenDatabaseImplement/Implements/LunchStorage.cs
2023-05-20 12:11:48 +04:00

156 lines
5.2 KiB
C#

using CanteenContracts.BindingModels;
using CanteenContracts.SearchModel;
using CanteenContracts.SearchModels;
using CanteenContracts.StoragesContracts;
using CanteenContracts.View;
using CanteenContracts.ViewModels;
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)
.Include(x => x.Orders)
.ThenInclude(x => x.Order)
.FirstOrDefault(x => (x.DateCreate >= model.DateFrom && x.DateImplement <= model.DateTo) || (x.Id == model.Id))?.GetViewModel;
}
public LunchOrderViewModel? GetLunchOrderElement(LunchOrderSearchModel model)
{
if (!model.Id.HasValue && !model.LunchId.HasValue && !model.OrderId.HasValue)
{
return null;
}
using var context = new CanteenDatabase();
return context.LunchOrder
.FirstOrDefault(x => (x.LunchId == model.LunchId && x.OrderId == model.OrderId) || (x.Id == model.Id))?.GetViewModel;
}
public List<LunchViewModel> GetFilteredList(LunchSearchModel model)
{
if (!model.VisitorId.HasValue)
{
return new();
}
using var context = new CanteenDatabase();
return context.Lunches
.Include(x => x.Orders)
.ThenInclude(x => x.Order)
.Include(x => x.Products)
.ThenInclude(x => x.Product)
.ThenInclude(x => x.Cooks)
.ThenInclude(x => x.Cook)
.Where(x =>
(x.DateCreate >= model.DateFrom && x.DateImplement <= model.DateTo) ||
(model.Id.HasValue && x.Id == model.Id) ||
(model.VisitorId.HasValue && model.VisitorId == x.VisitorId))
.Select(x => x.GetViewModel).ToList();
}
public List<LunchViewModel> GetFullList()
{
using var context = new CanteenDatabase();
return context.Lunches
.Include(x => x.Orders)
.ThenInclude(x => x.Order)
.Include(x => x.Products)
.ThenInclude(x => x.Product)
.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();
if (model.LunchProducts.Any()) lunch.UpdateProducts(context, model);
if (model.LunchOrders.Any()) lunch.UpdateOrders(context, model);
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;
}
}
}