108 lines
3.6 KiB
C#
Raw Normal View History

2024-04-03 16:55:56 +04:00
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 DinerDataBase();
2024-05-11 22:14:52 +04:00
var element = context.Snacks
2024-04-03 16:55:56 +04:00
.Include(x => x.Components)
2024-05-11 22:14:52 +04:00
.Include(x => x.Orders)
2024-04-03 16:55:56 +04:00
.FirstOrDefault(rec => rec.ID == model.ID);
if (element != null)
{
2024-05-11 22:14:52 +04:00
context.Snacks.Remove(element);
2024-04-03 16:55:56 +04:00
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 DinerDataBase();
2024-05-11 22:14:52 +04:00
return context.Snacks
2024-04-03 16:55:56 +04:00
.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))
2024-05-11 22:14:52 +04:00
?.GetViewModel;
2024-04-03 16:55:56 +04:00
}
public List<SnackViewModel> GetFilteredList(SnackSearchModel model)
{
if (string.IsNullOrEmpty(model.ProductName)) {
return new();
}
using var context = new DinerDataBase();
2024-05-11 22:14:52 +04:00
return context.Snacks
2024-04-03 16:55:56 +04:00
.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 DinerDataBase();
2024-05-11 22:14:52 +04:00
return context.Snacks
2024-04-03 16:55:56 +04:00
.Include(x => x.Components)
.ThenInclude(x => x.Component).ToList()
.Select(x => x.GetViewModel).ToList();
}
public SnackViewModel? Insert(SnackBindingModel model)
{
using var context = new DinerDataBase();
var newProduct = Snack.Create(context, model);
if (newProduct == null) {
return null;
}
2024-05-11 22:14:52 +04:00
context.Snacks.Add(newProduct);
2024-04-03 16:55:56 +04:00
context.SaveChanges();
return newProduct.GetViewModel;
}
public SnackViewModel? Update(SnackBindingModel model)
{
using var context = new DinerDataBase();
using var transcation = context.Database.BeginTransaction();
try
{
2024-05-11 22:14:52 +04:00
var product = context.Snacks.FirstOrDefault(rec => rec.ID == model.ID);
2024-04-03 16:55:56 +04:00
if (product == null) {
return null;
}
product.Update(model);
context.SaveChanges();
product.UpdateComponents(context, model);
transcation.Commit();
return product.GetViewModel;
}
catch
{
transcation.Rollback();
throw;
}
}
}
}