PIbd-22_Karamushko_M_K_Pizz.../Pizzeria/PizzeriaDatabaseImplement/Implements/PizzaStorage.cs
2023-03-14 08:45:31 +04:00

105 lines
3.4 KiB
C#

using PizzeriaContracts.BindingModels;
using PizzeriaContracts.SearchModels;
using PizzeriaContracts.StoragesContracts;
using PizzeriaContracts.ViewModels;
using PizzeriaDatabaseImplement.models;
using Microsoft.EntityFrameworkCore;
namespace PizzeriaDatabaseImplement.Implements
{
public class PizzaStorage : IPizzaStorage
{
public List<PizzaViewModel> GetFullList()
{
using var context = new PizzeriaDatabase();
return context.Pizzas
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<PizzaViewModel> GetFilteredList(PizzaSearchModel model)
{
if (string.IsNullOrEmpty(model.PizzaName))
{
return new();
}
using var context = new PizzeriaDatabase();
return context.Pizzas
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.Where(x => x.PizzaName.Contains(model.PizzaName))
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public PizzaViewModel? GetElement(PizzaSearchModel model)
{
if (string.IsNullOrEmpty(model.PizzaName) &&
!model.Id.HasValue)
{
return null;
}
using var context = new PizzeriaDatabase();
return context.Pizzas
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.PizzaName) &&
x.PizzaName == model.PizzaName) ||
(model.Id.HasValue && x.Id ==
model.Id))
?.GetViewModel;
}
public PizzaViewModel? Insert(PizzaBindingModel model)
{
using var context = new PizzeriaDatabase();
var newPizza = Pizza.Create(context, model);
if (newPizza == null)
{
return null;
}
context.Pizzas.Add(newPizza);
context.SaveChanges();
return newPizza.GetViewModel;
}
public PizzaViewModel? Update(PizzaBindingModel model)
{
using var context = new PizzeriaDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var pizza = context.Pizzas.FirstOrDefault(rec =>
rec.Id == model.Id);
if (pizza == null)
{
return null;
}
pizza.Update(model);
context.SaveChanges();
pizza.UpdateComponents(context, model);
transaction.Commit();
return pizza.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public PizzaViewModel? Delete(PizzaBindingModel model)
{
using var context = new PizzeriaDatabase();
var element = context.Pizzas
.Include(x => x.Components)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Pizzas.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}