Coursach/Course/DatabaseImplement/Implements/ProductionStorage.cs

73 lines
2.7 KiB
C#
Raw Normal View History

 using Contracts.BindingModels;
2024-04-27 21:07:45 +04:00
using Contracts.SearchModels;
using Contracts.StoragesContracts;
using Contracts.ViewModels;
using DatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
2024-04-27 21:07:45 +04:00
namespace DatabaseImplement.Implements
{
public class ProductionStorage : IProductionStorage
2024-04-27 21:07:45 +04:00
{
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);
2024-04-27 21:07:45 +04:00
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;
2024-04-27 21:07:45 +04:00
}
public List<ProductionViewModel> GetFilteredList(ProductionSearchModel model)
{
if (!model.UserId.HasValue && !model.DetailId.HasValue)
2024-04-27 21:07:45 +04:00
{
return new();
}
using var context = new FactoryGoWorkDatabase();
if (model.DetailId.HasValue)
return context.Productions.Include(x => x.Details).ThenInclude(x => x.Detail).Where(x => x.UserId == model.UserId).Where(x => x.Details.FirstOrDefault(y => y.DetailId == model.DetailId) != null).Include(x => x.User).Select(x => x.GetViewModel).ToList();
else
return context.Productions.Include(x => x.Details).ThenInclude(x => x.Detail).Where(x => x.UserId == model.UserId).Select(x => x.GetViewModel).ToList();
2024-04-27 21:07:45 +04:00
}
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();
2024-04-27 21:07:45 +04:00
}
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);
2024-04-27 21:07:45 +04:00
context.SaveChanges();
return newProduction.GetViewModel;
}
}
}