ISEbd-22_CourseWork_School/School/SchoolDataBaseImplement/Implements/RequirementStorage.cs

96 lines
3.8 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using SchoolContracts.BindingModels;
using SchoolContracts.SearchModels;
using SchoolContracts.StoragesContracts;
using SchoolContracts.ViewModels;
using SchoolDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
namespace SchoolDatabaseImplement.Implements
{
public class RequirementStorage : IRequirementStorage
{
private void CheckSearchModel(RequirementSearchModel model)
{
if (model == null)
throw new ArgumentNullException("Передаваемая модель для поиска равна нулю", nameof(model));
if (!model.Id.HasValue && !model.DirectorId.HasValue)
throw new ArgumentException("Все передаваемые поля поисковой модели оказались пусты или равны null");
}
public RequirementViewModel? Delete(RequirementBindingModel model)
{
using var context = new SchoolDB();
var element = context.Requirements.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
context.Requirements.Remove(element);
context.SaveChanges();
return element;
}
return null;
}
public RequirementViewModel? GetElement(RequirementSearchModel model)
{
CheckSearchModel(model);
using var context = new SchoolDB();
if (!model.Id.HasValue)
{
return null;
}
return context.Requirements
.Include(requirement => requirement.Executor)
.Include(requirement => requirement.Disciplines).ThenInclude(requirementByDiscipline => requirementByDiscipline.Discipline)
.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id);
}
public List<RequirementViewModel> GetFilteredList(RequirementSearchModel model)
{
CheckSearchModel(model);
using var context = new SchoolDB();
return context.Requirements
.Include(requirement => requirement.Executor)
.Include(requirement => requirement.Disciplines).ThenInclude(requirementByDiscipline => requirementByDiscipline.Discipline)
.Where(x => x.DirectorId == model.ExecutorId)
.Select(x => (RequirementViewModel)x)
.ToList();
}
public List<RequirementViewModel> GetFullList()
{
using var context = new SchoolDB();
return context.Requirements
.Include(requirement => requirement.Executor)
.Include(requirement => requirement.Disciplines).ThenInclude(requirementByDiscipline => requirementByDiscipline.Discipline)
.Select(x => (RequirementViewModel)x)
.ToList();
}
public RequirementViewModel? Insert(RequirementBindingModel model)
{
var newRequirement = Requirement.Create(model);
if (newRequirement == null)
{
return null;
}
using var context = new SchoolDB();
context.Requirements.Add(newRequirement);
context.SaveChanges();
return newRequirement;
}
public RequirementViewModel? Update(RequirementBindingModel model)
{
using var context = new SchoolDB();
var requirement = context.Requirements.FirstOrDefault(x => x.Id == model.Id);
if (requirement == null)
{
return null;
}
requirement.Update(model);
context.SaveChanges();
requirement.UpdateDisciplines(context, model);
context.SaveChanges();
return requirement;
}
}
}