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

138 lines
4.1 KiB
C#
Raw Normal View History

2023-04-09 00:34:25 +04:00
using CanteenContracts.BindingModels;
using CanteenContracts.SearchModel;
using CanteenContracts.StoragesContracts;
using CanteenContracts.View;
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 DishStorage : IDishStorage
{
public DishViewModel? GetElement(DishSearchModel model)
{
2023-05-17 17:02:26 +04:00
if (!model.Id.HasValue)
2023-04-09 00:34:25 +04:00
{
return null;
}
using var context = new CanteenDatabase();
return context.Dishes
.Include(x => x.Products)
.ThenInclude(x => x.Product)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.DishName) && x.DishName == model.DishName) || (model.Id.HasValue && x.Id == model.Id)
)?.GetViewModel;
}
public List<DishViewModel> GetFilteredList(DishSearchModel model)
{
2023-05-18 01:40:11 +04:00
if (string.IsNullOrEmpty(model.DishName) && !model.ManagerId.HasValue)
2023-04-09 00:34:25 +04:00
{
return new();
}
using var context = new CanteenDatabase();
return context.Dishes
.Include(x => x.Products)
.ThenInclude(x => x.Product)
2023-05-20 00:18:48 +04:00
.Where(x => x.DishName == model.DishName || (model.ManagerId.HasValue && model.ManagerId == x.ManagerId))
.Select(x => x.GetViewModel).ToList();
2023-04-09 00:34:25 +04:00
}
public List<DishViewModel> GetFullList()
{
using var context = new CanteenDatabase();
2023-05-20 00:18:48 +04:00
return context.Dishes
.Include(x => x.Products)
.ThenInclude(x => x.Product)
.Select(x => x.GetViewModel)
.ToList();
2023-04-09 00:34:25 +04:00
}
public DishViewModel? Insert(DishBindingModel model)
{
using var context = new CanteenDatabase();
using var transaction = context.Database.BeginTransaction();
{
try
{
var newDish = Dish.Create(context, model);
if (newDish == null)
{
transaction.Rollback();
return null;
}
context.Dishes.Add(newDish);
context.SaveChanges();
context.Database.CommitTransaction();
return newDish.GetViewModel;
}
catch (Exception)
{
transaction.Rollback();
return null;
}
}
}
public DishViewModel? Update(DishBindingModel model)
{
using var context = new CanteenDatabase();
using var transaction = context.Database.BeginTransaction();
{
try
{
var dish = context.Dishes.FirstOrDefault(x => x.Id == model.Id);
if (dish == null)
{
transaction.Rollback();
return null;
}
dish.Update(model);
context.SaveChanges();
2023-05-20 00:18:48 +04:00
if (model.DishProducts.Any()) dish.UpdateDishProduct(context, model);
2023-04-09 00:34:25 +04:00
context.Database.CommitTransaction();
return dish.GetViewModel;
}
catch (Exception)
{
transaction.Rollback();
return null;
}
}
}
public DishViewModel? Delete(DishBindingModel model)
{
using var context = new CanteenDatabase();
var element = context.Dishes.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Dishes.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}