Coursach/Course/DatabaseImplement/Implements/ProductionStorage.cs
Sergey Kozyrev 6da52a2500 BeforeChangeToList
Люди бесятся с водки, люди бесятся с жиру
Люди думают вечно одно
Люди тычут в спину, их пальцы горят
А в ботинки стекает дерьмо
2024-04-30 17:50:38 +04:00

70 lines
2.3 KiB
C#

using Contracts.BindingModels;
using Contracts.SearchModels;
using Contracts.StoragesContracts;
using Contracts.ViewModels;
using DatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
namespace DatabaseImplement.Implements
{
public class ProductionStorage : IProductionStorage
{
public ProductionViewModel? Delete(ProductionBindingModel model)
{
using var context = new FactoryGoWorkDatabase();
var newProduction = context.Productions.FirstOrDefault(x => x.Id == model.Id);
if (newProduction == null)
return null;
newProduction.UpdateDetails(context, model);
context.Productions.Remove(newProduction);
context.SaveChanges();
return newProduction.GetViewModel;
}
public ProductionViewModel? GetElement(ProductionSearchModel model)
{
using var context = new FactoryGoWorkDatabase();
return context.Productions.Include(x => x.Details).ThenInclude(x => x.Detail).FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && x.Name.Contains(model.Name)) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public List<ProductionViewModel> GetFilteredList(ProductionSearchModel model)
{
if (!model.UserId.HasValue)
{
return new();
}
using var context = new FactoryGoWorkDatabase();
return context.Productions.Include(x => x.Details).ThenInclude(x => x.Detail).Where(x => x.UserId == model.Id).Select(x => x.GetViewModel).ToList();
}
public List<ProductionViewModel> GetFullList()
{
using var context = new FactoryGoWorkDatabase();
return context.Productions.Include(x => x.Details).ThenInclude(x => x.Detail).Select(x => x.GetViewModel).ToList();
}
public ProductionViewModel? Insert(ProductionBindingModel model)
{
using var context = new FactoryGoWorkDatabase();
var newProduction = Production.Create(context, model);
if (newProduction == null)
return null;
context.Productions.Add(newProduction);
context.SaveChanges();
return newProduction.GetViewModel;
}
public ProductionViewModel? Update(ProductionBindingModel model)
{
using var context = new FactoryGoWorkDatabase();
var newProduction = context.Productions.FirstOrDefault(x => x.Id == model.Id);
if (newProduction == null)
return null;
newProduction.Update(model);
newProduction.UpdateDetails(context, model);
context.SaveChanges();
return newProduction.GetViewModel;
}
}
}