Coursach/Course/DatabaseImplement/Implements/WorkshopStorage.cs

76 lines
2.9 KiB
C#
Raw Normal View History

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)
{
if (!model.UserId.HasValue && !model.DateFrom.HasValue && !model.DateTo.HasValue)
{
return new();
}
using var context = new FactoryGoWorkDatabase();
2024-05-01 15:38:13 +04:00
if (model.DetailId.HasValue)
return context.Workshops.Where(x => x.ProductionId.HasValue).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 if (model.DateFrom.HasValue)
return context.Workshops.Where(x => x.UserId == model.Id).Where(x => x.DateCreate < model.DateTo && x.DateCreate > model.DateFrom).Select(x => x.GetViewModel).ToList();
else
return context.Workshops.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<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;
}
}
}