ISEbd-21_Agliullov.D.A._Con.../ConfectioneryDatabaseImplement/PastryStorage.cs

94 lines
3.2 KiB
C#

using ConfectioneryContracts.BindingModels;
using ConfectioneryContracts.SearchModels;
using ConfectioneryContracts.StoragesContract;
using ConfectioneryContracts.ViewModels;
using ConfectioneryDatabaseImplement;
using ConfectioneryDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
namespace ConfectioneryDatabaseImplement.Implements
{
public class PastryStorage : IPastryStorage
{
public PastryViewModel? Delete(PastryBindingModel model)
{
using var context = new ConfectioneryDatabase();
var element = context.Pastries.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
context.Pastries.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
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 List<PastryViewModel> GetFilteredList(PastrySearchModel model)
{
if (string.IsNullOrEmpty(model.PastryName))
{
return new();
}
using var context = new ConfectioneryDatabase();
return context.Pastries
.Select(x => x.GetViewModel)
.Where(x => x.PastryName.Contains(model.PastryName))
.ToList();
}
public List<PastryViewModel> GetFullList()
{
using var context = new ConfectioneryDatabase();
return context.Pastries
.Select(x => x.GetViewModel)
.ToList();
}
public PastryViewModel? Insert(PastryBindingModel model)
{
using var context = new ConfectioneryDatabase();
var newPastry = Pastry.Create(context, model);
if (newPastry == null)
{
return null;
}
newPastry = context.Pastries.Add(newPastry).Entity;
context.SaveChanges();
model.Id = newPastry.Id;
newPastry.UpdateComponents(context, model);
return newPastry.GetViewModel;
}
public PastryViewModel? Update(PastryBindingModel model)
{
using var context = new ConfectioneryDatabase();
var pastry = context.Pastries.FirstOrDefault(x => x.Id == model.Id);
if (pastry == null)
{
return null;
}
pastry.Update(model);
pastry.UpdateComponents(context, model);
context.Update(pastry);
context.SaveChanges();
return pastry.GetViewModel;
}
}
}