108 lines
3.7 KiB
C#
108 lines
3.7 KiB
C#
using DinerContracts.BindingModels;
|
|
using DinerContracts.SearchModels;
|
|
using DinerContracts.StoragesContracts;
|
|
using DinerContracts.ViewModels;
|
|
using DinerDataBaseImplement.Models;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.Metadata.Conventions;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace DinerDataBaseImplement.Implements
|
|
{
|
|
public class SnackStorage : ISnackStorage
|
|
{
|
|
public SnackViewModel? Delete(SnackBindingModel model)
|
|
{
|
|
using var context = new DinerDatabaseBy7Work();
|
|
var element = context.Snacks
|
|
.Include(x => x.Components)
|
|
.Include(x => x.Orders)
|
|
.FirstOrDefault(rec => rec.ID == model.ID);
|
|
if (element != null)
|
|
{
|
|
context.Snacks.Remove(element);
|
|
context.SaveChanges();
|
|
return element.GetViewModel;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public SnackViewModel? GetElement(SnackSearchModel model)
|
|
{
|
|
if (string.IsNullOrEmpty(model.ProductName) && !model.ID.HasValue) {
|
|
return null;
|
|
}
|
|
using var context = new DinerDatabaseBy7Work();
|
|
return context.Snacks
|
|
.Include(x => x.Components)
|
|
.ThenInclude(x => x.Component)
|
|
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ProductName) && x.ProductName == model.ProductName) ||
|
|
(model.ID.HasValue && x.ID == model.ID))
|
|
?.GetViewModel;
|
|
}
|
|
|
|
public List<SnackViewModel> GetFilteredList(SnackSearchModel model)
|
|
{
|
|
if (string.IsNullOrEmpty(model.ProductName)) {
|
|
return new();
|
|
}
|
|
using var context = new DinerDatabaseBy7Work();
|
|
return context.Snacks
|
|
.Include(x => x.Components)
|
|
.ThenInclude(x => x.Component)
|
|
.Where(x => x.ProductName.Contains(model.ProductName))
|
|
.ToList()
|
|
.Select(x => x.GetViewModel).ToList();
|
|
}
|
|
|
|
public List<SnackViewModel> GetFullList()
|
|
{
|
|
using var context = new DinerDatabaseBy7Work();
|
|
return context.Snacks
|
|
.Include(x => x.Components)
|
|
.ThenInclude(x => x.Component).ToList()
|
|
.Select(x => x.GetViewModel).ToList();
|
|
}
|
|
|
|
public SnackViewModel? Insert(SnackBindingModel model)
|
|
{
|
|
using var context = new DinerDatabaseBy7Work();
|
|
var newProduct = Snack.Create(context, model);
|
|
if (newProduct == null) {
|
|
return null;
|
|
}
|
|
context.Snacks.Add(newProduct);
|
|
context.SaveChanges();
|
|
return newProduct.GetViewModel;
|
|
}
|
|
|
|
public SnackViewModel? Update(SnackBindingModel model)
|
|
{
|
|
using var context = new DinerDatabaseBy7Work();
|
|
using var transcation = context.Database.BeginTransaction();
|
|
try
|
|
{
|
|
var product = context.Snacks.FirstOrDefault(rec => rec.ID == model.ID);
|
|
if (product == null) {
|
|
return null;
|
|
}
|
|
product.Update(model);
|
|
context.SaveChanges();
|
|
product.UpdateComponents(context, model);
|
|
transcation.Commit();
|
|
return product.GetViewModel;
|
|
}
|
|
catch
|
|
{
|
|
transcation.Rollback();
|
|
throw;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|