2023-02-27 22:16:28 +04:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using SushiBarContracts.BindingModels;
|
|
|
|
|
using SushiBarContracts.SearchModels;
|
|
|
|
|
using SushiBarContracts.StoragesContracts;
|
|
|
|
|
using SushiBarContracts.ViewModels;
|
|
|
|
|
using SushiBarDatabaseImplement.Models;
|
|
|
|
|
|
|
|
|
|
namespace SushiBarDatabaseImplement.Implements
|
|
|
|
|
{
|
|
|
|
|
public class SushiStorage : ISushiStorage
|
|
|
|
|
{
|
|
|
|
|
public List<SushiViewModel> GetFullList()
|
|
|
|
|
{
|
|
|
|
|
using var context = new SushiBarDatabase();
|
2023-03-10 16:44:26 +04:00
|
|
|
|
return context.ListSushi
|
2023-02-27 22:16:28 +04:00
|
|
|
|
.Include(x => x.Ingredients)
|
|
|
|
|
.ThenInclude(x => x.Ingredient)
|
|
|
|
|
.ToList()
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<SushiViewModel> GetFilteredList(SushiSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(model.SushiName))
|
|
|
|
|
{
|
|
|
|
|
return new();
|
|
|
|
|
}
|
|
|
|
|
using var context = new SushiBarDatabase();
|
2023-03-10 16:44:26 +04:00
|
|
|
|
return context.ListSushi
|
2023-02-27 22:16:28 +04:00
|
|
|
|
.Include(x => x.Ingredients)
|
|
|
|
|
.ThenInclude(x => x.Ingredient)
|
|
|
|
|
.Where(x => x.SushiName.Contains(model.SushiName))
|
|
|
|
|
.ToList()
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SushiViewModel? GetElement(SushiSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(model.SushiName) && !model.Id.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
using var context = new SushiBarDatabase();
|
2023-03-10 16:44:26 +04:00
|
|
|
|
return context.ListSushi
|
2023-02-27 22:16:28 +04:00
|
|
|
|
.Include(x => x.Ingredients)
|
|
|
|
|
.ThenInclude(x => x.Ingredient)
|
|
|
|
|
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.SushiName) && x.SushiName == model.SushiName) ||
|
|
|
|
|
(model.Id.HasValue && x.Id == model.Id))
|
|
|
|
|
?.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SushiViewModel? Insert(SushiBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new SushiBarDatabase();
|
|
|
|
|
var newSushi = Sushi.Create(context, model);
|
|
|
|
|
if (newSushi == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2023-03-10 16:44:26 +04:00
|
|
|
|
context.ListSushi.Add(newSushi);
|
2023-02-27 22:16:28 +04:00
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return newSushi.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SushiViewModel? Update(SushiBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new SushiBarDatabase();
|
|
|
|
|
using var transaction = context.Database.BeginTransaction();
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-03-10 16:44:26 +04:00
|
|
|
|
var sushi = context.ListSushi.FirstOrDefault(rec => rec.Id == model.Id);
|
2023-02-27 22:16:28 +04:00
|
|
|
|
if (sushi == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
sushi.Update(model);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
sushi.UpdateIngredients(context, model);
|
|
|
|
|
transaction.Commit();
|
|
|
|
|
return sushi.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
transaction.Rollback();
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SushiViewModel? Delete(SushiBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new SushiBarDatabase();
|
2023-03-10 16:44:26 +04:00
|
|
|
|
var element = context.ListSushi
|
2023-02-27 22:16:28 +04:00
|
|
|
|
.Include(x => x.Ingredients)
|
2023-02-27 22:16:28 +04:00
|
|
|
|
.Include(x => x.Orders)
|
2023-02-27 22:16:28 +04:00
|
|
|
|
.FirstOrDefault(rec => rec.Id == model.Id);
|
|
|
|
|
if (element != null)
|
|
|
|
|
{
|
2023-03-10 16:44:26 +04:00
|
|
|
|
context.ListSushi.Remove(element);
|
2023-02-27 22:16:28 +04:00
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return element.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|