113 lines
3.8 KiB
C#
113 lines
3.8 KiB
C#
using ConfectioneryContracts.BindingModels;
|
|
using ConfectioneryContracts.SearchModels;
|
|
using ConfectioneryContracts.StoragesContracts;
|
|
using ConfectioneryContracts.ViewModels;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using ConfectioneryDatabaseImplement.Models;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace ConfectioneryDatabaseImplement.Implements
|
|
{
|
|
public class SweetsStorage : ISweetsStorage
|
|
{
|
|
public List<SweetsViewModel> GetFullList()
|
|
{
|
|
using var context = new ConfectioneryDatabase();
|
|
return context.ListSweets
|
|
.Include(x => x.Ingredients)
|
|
.ThenInclude(x => x.Ingredient)
|
|
.ToList()
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
public List<SweetsViewModel> GetFilteredList(SweetsSearchModel model)
|
|
{
|
|
if (string.IsNullOrEmpty(model.SweetsName))
|
|
{
|
|
return new();
|
|
}
|
|
using var context = new ConfectioneryDatabase();
|
|
return context.ListSweets
|
|
.Include(x => x.Ingredients)
|
|
.ThenInclude(x => x.Ingredient)
|
|
.Where(x => x.SweetsName.Contains(model.SweetsName))
|
|
.ToList()
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
public SweetsViewModel? GetElement(SweetsSearchModel model)
|
|
{
|
|
if (string.IsNullOrEmpty(model.SweetsName) && !model.Id.HasValue)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new ConfectioneryDatabase();
|
|
return context.ListSweets
|
|
.Include(x => x.Ingredients)
|
|
.ThenInclude(x => x.Ingredient)
|
|
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.SweetsName) && x.SweetsName == model.SweetsName) ||
|
|
(model.Id.HasValue && x.Id == model.Id))
|
|
?.GetViewModel;
|
|
}
|
|
|
|
public SweetsViewModel? Insert(SweetsBindingModel model)
|
|
{
|
|
using var context = new ConfectioneryDatabase();
|
|
var newSweets = Sweets.Create(context, model);
|
|
if (newSweets == null)
|
|
{
|
|
return null;
|
|
}
|
|
context.ListSweets.Add(newSweets);
|
|
context.SaveChanges();
|
|
return newSweets.GetViewModel;
|
|
}
|
|
|
|
public SweetsViewModel? Update(SweetsBindingModel model)
|
|
{
|
|
using var context = new ConfectioneryDatabase();
|
|
using var transaction = context.Database.BeginTransaction();
|
|
try
|
|
{
|
|
var sweets = context.ListSweets.FirstOrDefault(rec => rec.Id == model.Id);
|
|
if (sweets == null)
|
|
{
|
|
return null;
|
|
}
|
|
sweets.Update(model);
|
|
context.SaveChanges();
|
|
sweets.UpdateIngredients(context, model);
|
|
transaction.Commit();
|
|
return sweets.GetViewModel;
|
|
}
|
|
catch
|
|
{
|
|
transaction.Rollback();
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public SweetsViewModel? Delete(SweetsBindingModel model)
|
|
{
|
|
using var context = new ConfectioneryDatabase();
|
|
var element = context.ListSweets
|
|
.Include(x => x.Ingredients)
|
|
.Include(x => x.Orders)
|
|
.FirstOrDefault(rec => rec.Id == model.Id);
|
|
if (element != null)
|
|
{
|
|
context.ListSweets.Remove(element);
|
|
context.SaveChanges();
|
|
return element.GetViewModel;
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
}
|