2024-04-27 22:13:33 +04:00
|
|
|
|
using Contracts.BindingModels;
|
|
|
|
|
using Contracts.SearchModels;
|
|
|
|
|
using Contracts.StoragesContracts;
|
|
|
|
|
using Contracts.ViewModels;
|
|
|
|
|
using DatabaseImplement.Models;
|
2024-04-30 21:51:55 +04:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2024-04-27 22:13:33 +04:00
|
|
|
|
|
|
|
|
|
namespace DatabaseImplement.Implements
|
|
|
|
|
{
|
|
|
|
|
public class MachineStorage : IMachineStorage
|
|
|
|
|
{
|
|
|
|
|
public MachineViewModel? Delete(MachineBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new FactoryGoWorkDatabase();
|
|
|
|
|
var newMachine = context.Machines.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
|
if (newMachine == null)
|
|
|
|
|
return null;
|
2024-04-30 21:51:55 +04:00
|
|
|
|
newMachine.UpdateWorkers(context, model);
|
2024-04-27 22:13:33 +04:00
|
|
|
|
context.Machines.Remove(newMachine);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return newMachine.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public MachineViewModel? GetElement(MachineSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new FactoryGoWorkDatabase();
|
2024-04-30 21:51:55 +04:00
|
|
|
|
return context.Machines.Include(p => p.Workers).ThenInclude(p => p.Worker).FirstOrDefault(x => (!string.IsNullOrEmpty(model.Title) && x.Title.Contains(model.Title)) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
|
|
|
|
|
}
|
2024-04-27 22:13:33 +04:00
|
|
|
|
|
|
|
|
|
public List<MachineViewModel> GetFilteredList(MachineSearchModel model)
|
|
|
|
|
{
|
2024-04-30 23:42:05 +04:00
|
|
|
|
if (!model.UserId.HasValue && !model.DateFrom.HasValue && !model.DateTo.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return new();
|
|
|
|
|
}
|
|
|
|
|
using var context = new FactoryGoWorkDatabase();
|
|
|
|
|
if (model.DateFrom.HasValue)
|
|
|
|
|
return context.Machines.Where(x => x.UserId == model.Id).Where(x => x.DateCreate < model.DateTo && x.DateCreate > model.DateFrom).Select(x => x.GetViewModel).ToList();
|
|
|
|
|
else
|
|
|
|
|
return context.Machines.Where(x => x.UserId == model.Id).Select(x => x.GetViewModel).ToList();
|
2024-04-30 21:51:55 +04:00
|
|
|
|
}
|
2024-04-27 22:13:33 +04:00
|
|
|
|
|
|
|
|
|
public List<MachineViewModel> GetFullList()
|
|
|
|
|
{
|
|
|
|
|
using var context = new FactoryGoWorkDatabase();
|
2024-04-30 21:51:55 +04:00
|
|
|
|
return context.Machines.Include(p => p.Workers).ThenInclude(p => p.Worker).Select(x => x.GetViewModel).ToList();
|
|
|
|
|
}
|
2024-04-27 22:13:33 +04:00
|
|
|
|
|
|
|
|
|
public MachineViewModel? Insert(MachineBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new FactoryGoWorkDatabase();
|
|
|
|
|
var newMachine = Machine.Create(model, context);
|
|
|
|
|
if (newMachine == null)
|
|
|
|
|
return null;
|
|
|
|
|
context.Machines.Add(newMachine);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return newMachine.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public MachineViewModel? Update(MachineBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new FactoryGoWorkDatabase();
|
|
|
|
|
var newMachine = context.Machines.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
|
if (newMachine == null)
|
|
|
|
|
return null;
|
|
|
|
|
newMachine.Update(model);
|
2024-04-30 21:51:55 +04:00
|
|
|
|
newMachine.UpdateWorkers(context, model);
|
2024-04-27 22:13:33 +04:00
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return newMachine.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|