Coursach/Course/DatabaseImplement/Implements/WorkshopStorage.cs

71 lines
2.3 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)
{
2024-04-30 21:51:55 +04:00
if (!model.UserId.HasValue)
2024-04-27 22:13:33 +04:00
{
return new();
}
using var context = new FactoryGoWorkDatabase();
2024-04-30 21:51:55 +04:00
return context.Workshops.Include(x => x.Workers).ThenInclude(x => x.Worker).Where(x => x.UserId == model.UserId).Select(x => x.GetViewModel).ToList();
}
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;
}
}
}