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

153 lines
6.3 KiB
C#
Raw Normal View History

2024-05-01 17:54:58 +04:00
using System.Security.Cryptography.X509Certificates;
using SchoolContracts.BindingModels;
using SchoolContracts.SearchModels;
using SchoolContracts.StoragesContracts;
using SchoolContracts.ViewModels;
using SchoolDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
namespace SchoolDatabaseImplement.Implements
{
public class DisciplineStorage : IDisciplineStorage
{
private void CheckSearchModel(DisciplineSearchModel model)
{
if (model == null)
throw new ArgumentNullException("Передаваемая модель для поиска равна нулю", nameof(model));
if (!model.Id.HasValue && !model.ImplementerId.HasValue && !model.DateFrom.HasValue && !model.DateTo.HasValue && model.ClientsIds == null)
throw new ArgumentException("Все передаваемые поля поисковой модели оказались пусты или равны null");
if (model.DateFrom.HasValue != model.DateTo.HasValue)
throw new ArgumentException($"Не указано начало {model.DateFrom} или конец {model.DateTo} периода для поиска по дате.");
}
public DisciplineViewModel? Delete(DisciplineBindingModel model)
{
using var context = new SchoolDB();
var element = context.Disciplines.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
context.Disciplines.Remove(element);
context.SaveChanges();
return element;
}
return null;
}
/// <summary>
/// Получение списка счетов по клиенту и дисциплине, для получения полной и оплаченной стоимости в бизнес логике
/// </summary>
public List<AccountViewModel> GetAccountsFromDisciplineAndClient(DisciplineSearchModel modelDiscipline, StudentSearchModel modelStudent)
{
if (!modelDiscipline.Id.HasValue)
{
throw new ArgumentNullException(nameof(modelDiscipline), "Получена поисковая модель без Id");
}
if (!modelStudent.Id.HasValue)
{
throw new ArgumentNullException(nameof(modelStudent), "Получена поисковая модель без Id");
}
using var context = new SchoolDB();
var studentByDiscipline = context.StudentsByDisciplines
.Include(x => x.Accounts)
.FirstOrDefault(x => x.ClientId == modelStudent.Id && x.DisciplineId == modelDiscipline.Id);
if (studentByDiscipline?.Accounts == null)
{
throw new InvalidOperationException(
$"Не существует связи между данным учеников(Id={modelStudent.Id}) и (Id={modelDiscipline.Id})");
}
return studentByDiscipline.Accounts.Select(account => (AccountViewModel)account).ToList();
}
public DisciplineViewModel? GetElement(DisciplineSearchModel model)
{
using var context = new SchoolDB();
if (!model.Id.HasValue)
{
return null;
}
return context.Disciplines
.Include(x => x.Student)
.Include(x => x.Students)
.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id);
}
public List<DisciplineViewModel> GetFilteredList(DisciplineSearchModel model)
{
CheckSearchModel(model);
if (model.Id.HasValue)
{
var res = GetElement(model);
return res != null ? new() { res } : new();
}
using var context = new SchoolDB();
var query = context.Disciplines.Include(x => x.Student);
IQueryable<Discipline>? resultQuery = null;
if (model.ImplementerId.HasValue)
{
return query
.Where(x => model.ImplementerId == x.ImplementerId)
.Select(x => (DisciplineViewModel)x)
.ToList();
}
if (model.DateTo.HasValue)
resultQuery = query
.Include(x => x.Students)
.ThenInclude(x => x.Student)
.Include(x => x.Requirements)
.ThenInclude(x => x.Requirement)
.Where(x => model.DateFrom <= x.DateOfReceipt && x.DateOfReceipt <= model.DateTo);
else if (model.StudentsIds != null)
resultQuery = query
.Include(x => x.Students)
.ThenInclude(x => x.Student)
.Where(x => x.Clients.Any(x => model.StudentsIds.Contains(x.StudentId)));
return resultQuery?
.Select(x => (DisciplineViewModel)x)
.ToList() ?? new();
}
public List<DisciplineViewModel> GetFullList()
{
using var context = new SchoolDB();
return context.Disciplines
.Include(x => x.Student)
.Include(x => x.Students)
.Select(x => (DisciplineViewModel)x)
.ToList();
}
public DisciplineViewModel? Insert(DisciplineBindingModel model)
{
var newDiscipline = Discipline.Create(model);
if (newDiscipline == null)
{
return null;
}
using var context = new SchoolDB();
context.Disciplines.Add(newDiscipline);
context.SaveChanges();
newDiscipline.UpdateClients(context, model);
context.SaveChanges();
return newDiscipline;
}
public DisciplineViewModel? Update(DisciplineBindingModel model)
{
using var context = new SchoolDB();
var disciplinevisit = context.Disciplines.FirstOrDefault(x => x.Id == model.Id);
if (disciplinevisit == null)
{
return null;
}
disciplinevisit.Update(model);
disciplinevisit.UpdateClients(context, model);
context.SaveChanges();
return disciplinevisit;
}
}
}