2024-04-27 21:07:45 +04:00
|
|
|
|
using Contracts.BindingModels;
|
|
|
|
|
using Contracts.SearchModels;
|
|
|
|
|
using Contracts.StoragesContracts;
|
|
|
|
|
using Contracts.ViewModels;
|
|
|
|
|
using DatabaseImplement.Models;
|
2024-04-30 17:03:49 +04:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2024-04-27 21:07:45 +04:00
|
|
|
|
|
|
|
|
|
namespace DatabaseImplement.Implements
|
|
|
|
|
{
|
|
|
|
|
public class ProductStorage : IProductStorage
|
|
|
|
|
{
|
|
|
|
|
public ProductViewModel? Delete(ProductBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new FactoryGoWorkDatabase();
|
|
|
|
|
var newProduct = context.Products.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
|
if (newProduct == null)
|
|
|
|
|
return null;
|
2024-04-30 17:03:49 +04:00
|
|
|
|
newProduct.UpdateDetails(context, model);
|
2024-04-27 21:07:45 +04:00
|
|
|
|
context.Products.Remove(newProduct);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return newProduct.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ProductViewModel? GetElement(ProductSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new FactoryGoWorkDatabase();
|
2024-05-26 23:23:24 +04:00
|
|
|
|
return context.Products.Include(p => p.Details).ThenInclude(p => p.Detail).Include(p => p.Machine).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<ProductViewModel> GetFilteredList(ProductSearchModel model)
|
|
|
|
|
{
|
2024-05-01 15:44:39 +04:00
|
|
|
|
if (!model.UserId.HasValue && !model.WorkerId.HasValue && !model.DetailId.HasValue)
|
2024-04-27 21:07:45 +04:00
|
|
|
|
{
|
|
|
|
|
return new();
|
|
|
|
|
}
|
|
|
|
|
using var context = new FactoryGoWorkDatabase();
|
2024-05-01 15:44:39 +04:00
|
|
|
|
if (model.DetailId.HasValue)
|
2024-05-26 23:23:24 +04:00
|
|
|
|
return context.Products.Include(p => p.Details).ThenInclude(p => p.Detail).Include(p => p.Machine).Where(x => x.UserId == model.UserId).Where(x => x.Details.FirstOrDefault(y => y.DetailId == model.DetailId) != null).Select(x => x.GetViewModel).ToList();
|
2024-05-01 15:44:39 +04:00
|
|
|
|
else if (model.WorkerId.HasValue)
|
|
|
|
|
return context.Products.Where(p => p.MachineId.HasValue).Include(p => p.Machine).ThenInclude(p => p.Workers).Where(x => x.Machine.Workers.FirstOrDefault(y => y.WorkerId == model.WorkerId) != null).Select(x => x.GetViewModel).ToList();
|
2024-04-30 21:51:49 +04:00
|
|
|
|
else
|
2024-05-26 23:23:24 +04:00
|
|
|
|
return context.Products.Include(p => p.Details).ThenInclude(p => p.Detail).Include(p => p.Machine).Where(x => x.UserId == model.UserId).Select(x => x.GetViewModel).ToList();
|
2024-04-27 21:07:45 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<ProductViewModel> GetFullList()
|
|
|
|
|
{
|
|
|
|
|
using var context = new FactoryGoWorkDatabase();
|
2024-04-30 17:03:49 +04:00
|
|
|
|
return context.Products.Include(p => p.Details).ThenInclude(p => p.Detail).Select(x => x.GetViewModel).ToList();
|
2024-04-27 21:07:45 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ProductViewModel? Insert(ProductBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new FactoryGoWorkDatabase();
|
|
|
|
|
var newProduct = Product.Create(context, model);
|
|
|
|
|
if (newProduct == null)
|
|
|
|
|
return null;
|
|
|
|
|
context.Products.Add(newProduct);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return newProduct.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ProductViewModel? Update(ProductBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new FactoryGoWorkDatabase();
|
|
|
|
|
var newProduct = context.Products.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
|
if (newProduct == null)
|
|
|
|
|
return null;
|
|
|
|
|
newProduct.Update(model);
|
2024-04-30 17:03:49 +04:00
|
|
|
|
newProduct.UpdateDetails(context, model);
|
2024-04-27 21:07:45 +04:00
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return newProduct.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|