PIbd-22_Chubykina_P.P._Conf.../Confectionery/ConfectioneryDatabaseImplement/Implements/PastryStorage.cs

112 lines
3.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ConfectioneryContracts.BindingModels;
using ConfectioneryContracts.SearchModels;
using ConfectioneryContracts.StoragesContracts;
using ConfectioneryContracts.ViewModels;
using ConfectioneryDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
namespace ConfectioneryDatabaseImplement.Implements
{
public class PastryStorage : IPastryStorage
{
public List<PastryViewModel> GetFullList()
{
using var context = new ConfectioneryDatabase();
return context.Pastries
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<PastryViewModel> GetFilteredList(PastrySearchModel model)
{
if (string.IsNullOrEmpty(model.PastryName))
{
return new();
}
using var context = new ConfectioneryDatabase();
return context.Pastries
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.Where(x => x.PastryName.Contains(model.PastryName))
.ToList()
.Select(x => x.GetViewModel).ToList();
}
public PastryViewModel? GetElement(PastrySearchModel model)
{
if (string.IsNullOrEmpty(model.PastryName) &&
!model.Id.HasValue)
{
return null;
}
using var context = new ConfectioneryDatabase();
return context.Pastries
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.PastryName) &&
x.PastryName == model.PastryName) ||
(model.Id.HasValue && x.Id ==
model.Id))
?.GetViewModel;
}
public PastryViewModel? Insert(PastryBindingModel model)
{
using var context = new ConfectioneryDatabase();
var newPastry = Pastry.Create(context, model);
if (newPastry == null)
{
return null;
}
context.Pastries.Add(newPastry);
context.SaveChanges();
return newPastry.GetViewModel;
}
public PastryViewModel? Update(PastryBindingModel model)
{
using var context = new ConfectioneryDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var product = context.Pastries.FirstOrDefault(rec =>
rec.Id == model.Id);
if (product == null)
{
return null;
}
product.Update(model);
context.SaveChanges();
product.UpdateComponents(context, model);
transaction.Commit();
return product.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public PastryViewModel? Delete(PastryBindingModel model)
{
using var context = new ConfectioneryDatabase();
var element = context.Pastries
.Include(x => x.Components)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Pastries.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}