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 WorkshopStorage : IWorkshopStorage
|
|
|
|
|
{
|
|
|
|
|
public WorkshopViewModel? Delete(WorkshopBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new FactoryGoWorkDatabase();
|
|
|
|
|
var newWorkshop = context.Workshops.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
|
if (newWorkshop == null)
|
|
|
|
|
return null;
|
2024-04-30 21:51:55 +04:00
|
|
|
|
newWorkshop.UpdateWorkers(context, model);
|
2024-04-27 22:13:33 +04:00
|
|
|
|
context.Workshops.Remove(newWorkshop);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return newWorkshop.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public WorkshopViewModel? GetElement(WorkshopSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new FactoryGoWorkDatabase();
|
2024-04-30 21:51:55 +04:00
|
|
|
|
return context.Workshops.Include(x => x.Workers).ThenInclude(x => x.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<WorkshopViewModel> GetFilteredList(WorkshopSearchModel model)
|
|
|
|
|
{
|
2024-04-30 22:06:53 +04:00
|
|
|
|
if (!model.UserId.HasValue && !model.DetailId.HasValue)
|
2024-04-27 22:13:33 +04:00
|
|
|
|
{
|
|
|
|
|
return new();
|
|
|
|
|
}
|
|
|
|
|
using var context = new FactoryGoWorkDatabase();
|
2024-04-30 22:06:53 +04:00
|
|
|
|
if (model.DetailId.HasValue)
|
|
|
|
|
return context.Workshops.Include(x => x.Production).Where(x => x.Production.Details.FirstOrDefault(y => y.DetailId == model.DetailId) != null).Where(x => x.UserId == model.UserId).Select(x => x.GetViewModel).ToList();
|
|
|
|
|
else
|
|
|
|
|
return context.Workshops.Include(x => x.Workers).ThenInclude(x => x.Worker).Where(x => x.UserId == model.UserId).Select(x => x.GetViewModel).ToList();
|
2024-04-30 21:51:55 +04:00
|
|
|
|
}
|
2024-04-27 22:13:33 +04:00
|
|
|
|
|
|
|
|
|
public List<WorkshopViewModel> GetFullList()
|
|
|
|
|
{
|
|
|
|
|
using var context = new FactoryGoWorkDatabase();
|
2024-04-30 21:51:55 +04:00
|
|
|
|
return context.Workshops.Include(x => x.Workers).ThenInclude(x => x.Worker).Select(x => x.GetViewModel).ToList();
|
|
|
|
|
}
|
2024-04-27 22:13:33 +04:00
|
|
|
|
|
|
|
|
|
public WorkshopViewModel? Insert(WorkshopBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new FactoryGoWorkDatabase();
|
|
|
|
|
var newWorkshop = Workshop.Create(context, model);
|
|
|
|
|
if (newWorkshop == null)
|
|
|
|
|
return null;
|
|
|
|
|
context.Workshops.Add(newWorkshop);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return newWorkshop.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public WorkshopViewModel? Update(WorkshopBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new FactoryGoWorkDatabase();
|
|
|
|
|
var newWorkshop = context.Workshops.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
|
if (newWorkshop == null)
|
|
|
|
|
return null;
|
|
|
|
|
newWorkshop.Update(model);
|
2024-04-30 21:51:55 +04:00
|
|
|
|
newWorkshop.UpdateWorkers(context, model);
|
2024-04-27 22:13:33 +04:00
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return newWorkshop.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|