ISEbd-22_Alimova_M.S._Confe.../Confectionery/ConfectioneryDatabaseImplement/PastryStorage.cs

112 lines
3.6 KiB
C#
Raw Normal View History

2024-03-29 00:00:37 +04:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ConfectioneryContracts.BindingModels;
using ConfectioneryContracts.SearchModels;
using ConfectioneryContracts.StoragesContracts;
using ConfectioneryContracts.ViewModels;
using ConfectioneryDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
namespace ConfectioneryDatabaseImplement.Implements
{
public class PastryStorage : IPastryStorage
{
public List<PastryViewModel> GetFullList()
{
using var context = new ConfectioneryDatabase();
return context.Pastrys
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<PastryViewModel> GetFilteredList(PastrySearchModel model)
{
if (string.IsNullOrEmpty(model.PastryName))
{
return new();
}
using var context = new ConfectioneryDatabase();
return context.Pastrys
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.Where(x => x.PastryName.Contains(model.PastryName))
.ToList()
.Select(x => x.GetViewModel).ToList();
}
public PastryViewModel? GetElement(PastrySearchModel model)
{
if (string.IsNullOrEmpty(model.PastryName) &&
!model.Id.HasValue)
{
return null;
}
using var context = new ConfectioneryDatabase();
return context.Pastrys
.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 PastryViewModel? Insert(PastryBindingModel model)
{
using var context = new ConfectioneryDatabase();
var newPastry = Pastry.Create(context, model);
if (newPastry == null)
{
return null;
}
context.Pastrys.Add(newPastry);
context.SaveChanges();
return newPastry.GetViewModel;
}
public PastryViewModel? Update(PastryBindingModel model)
{
using var context = new ConfectioneryDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
2024-04-27 12:38:39 +04:00
var pastry = context.Pastrys.FirstOrDefault(rec =>
2024-03-29 00:00:37 +04:00
rec.Id == model.Id);
2024-04-27 12:38:39 +04:00
if (pastry == null)
2024-03-29 00:00:37 +04:00
{
return null;
}
2024-04-27 12:38:39 +04:00
pastry.Update(model);
2024-03-29 00:00:37 +04:00
context.SaveChanges();
2024-04-27 12:38:39 +04:00
pastry.UpdateComponents(context, model);
2024-03-29 00:00:37 +04:00
transaction.Commit();
2024-04-27 12:38:39 +04:00
return pastry.GetViewModel;
2024-03-29 00:00:37 +04:00
}
catch
{
transaction.Rollback();
throw;
}
}
public PastryViewModel? Delete(PastryBindingModel model)
{
using var context = new ConfectioneryDatabase();
var element = context.Pastrys
.Include(x => x.Components)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Pastrys.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}