2024-04-22 21:01:26 +04:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using System;
|
2024-04-19 19:30:16 +04:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2024-04-22 21:01:26 +04:00
|
|
|
|
using System.Reflection.Metadata;
|
2024-04-19 19:30:16 +04:00
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
2024-04-22 21:01:26 +04:00
|
|
|
|
using UniversityContracts.BindingModels;
|
|
|
|
|
using UniversityContracts.SearchModels;
|
|
|
|
|
using UniversityContracts.StorageContracts;
|
|
|
|
|
using UniversityContracts.ViewModels;
|
|
|
|
|
using UniversityDatabaseImplement.Models;
|
2024-04-19 19:30:16 +04:00
|
|
|
|
|
|
|
|
|
namespace UniversityDatabaseImplement.Implements
|
|
|
|
|
{
|
2024-04-22 21:01:26 +04:00
|
|
|
|
public class DisciplineStorage : IDisciplineStorage
|
2024-04-19 19:30:16 +04:00
|
|
|
|
{
|
2024-05-01 20:40:47 +04:00
|
|
|
|
public List<StudentViewModel> GetStudentsForDiscipline(DisciplineSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new UniversityDatabase();
|
|
|
|
|
|
|
|
|
|
var discipline = context.Disciplines
|
|
|
|
|
.Include(d => d.Students)
|
|
|
|
|
.ThenInclude(sd => sd.Student)
|
|
|
|
|
.FirstOrDefault(d => d.Id == model.Id);
|
|
|
|
|
|
|
|
|
|
if (discipline == null)
|
|
|
|
|
{
|
|
|
|
|
return new List<StudentViewModel>(); // Если дисциплина не найдена, возвращаем пустой список
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return discipline.Students
|
|
|
|
|
.Select(sd => sd.Student.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-22 21:01:26 +04:00
|
|
|
|
public DisciplineViewModel? Delete(DisciplineBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new UniversityDatabase();
|
2024-04-23 23:03:32 +04:00
|
|
|
|
var element = context.Disciplines.Include(x => x.Students).FirstOrDefault(rec => rec.Id == model.Id);
|
2024-04-22 21:01:26 +04:00
|
|
|
|
if (element != null)
|
|
|
|
|
{
|
|
|
|
|
context.Disciplines.Remove(element);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return element.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DisciplineViewModel? GetElement(DisciplineSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
using var context = new UniversityDatabase();
|
2024-04-23 23:03:32 +04:00
|
|
|
|
return context.Disciplines.Include(x => x.Students).ThenInclude(x => x.Student)
|
2024-04-22 21:01:26 +04:00
|
|
|
|
.Include(x => x.Teacher)
|
|
|
|
|
.FirstOrDefault(x =>
|
|
|
|
|
(!string.IsNullOrEmpty(model.Name) && x.Name == model.Name) || (model.Id.HasValue && x.Id == model.Id)) ?.GetViewModel;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-01 20:40:47 +04:00
|
|
|
|
|
|
|
|
|
//ешьте котиков в них витамин с
|
2024-04-22 21:01:26 +04:00
|
|
|
|
public List<DisciplineViewModel> GetFilteredList(DisciplineSearchModel model)
|
|
|
|
|
{
|
2024-05-01 20:40:47 +04:00
|
|
|
|
CheckSearchModel(model);
|
|
|
|
|
|
|
|
|
|
using var context = new UniversityDatabase();
|
|
|
|
|
var query = context.Disciplines
|
|
|
|
|
.Include(x => x.Students)
|
|
|
|
|
.ThenInclude(x => x.Student)
|
|
|
|
|
.Include(x => x.Teacher)
|
|
|
|
|
.AsQueryable();
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(model.Name))
|
2024-04-22 21:01:26 +04:00
|
|
|
|
{
|
2024-05-01 20:40:47 +04:00
|
|
|
|
query = query.Where(x => x.Name.Contains(model.Name));
|
2024-04-22 21:01:26 +04:00
|
|
|
|
}
|
2024-05-01 20:40:47 +04:00
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(model.Description))
|
|
|
|
|
{
|
|
|
|
|
query = query.Where(x => x.Description.Contains(model.Description));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (model.Id.HasValue)
|
|
|
|
|
{
|
|
|
|
|
query = query.Where(x => x.Id == model.Id.Value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (model.TeacherId.HasValue)
|
|
|
|
|
{
|
|
|
|
|
query = query.Where(x => x.TeacherId == model.TeacherId.Value);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-30 00:09:06 +04:00
|
|
|
|
|
|
|
|
|
|
2024-05-30 01:50:55 +04:00
|
|
|
|
if (model.DateFrom.HasValue && model.DateTo.HasValue)
|
2024-05-01 20:40:47 +04:00
|
|
|
|
{
|
2024-05-30 02:30:33 +04:00
|
|
|
|
//query = query.Where(x => model.DateFrom.Value <= x.Date && x.Date <= model.DateTo.Value);
|
2024-05-30 01:50:55 +04:00
|
|
|
|
|
2024-05-30 00:09:06 +04:00
|
|
|
|
|
|
|
|
|
var x = query.Select(x => x.GetViewModel).ToList();
|
|
|
|
|
var res = new List<DisciplineViewModel>();
|
|
|
|
|
|
|
|
|
|
foreach (var item in x) {
|
|
|
|
|
if (item.Date > model.DateFrom && item.Date < model.DateTo)
|
|
|
|
|
{
|
|
|
|
|
res.Add(item);
|
|
|
|
|
}
|
2024-05-01 20:40:47 +04:00
|
|
|
|
}
|
2024-05-30 00:09:06 +04:00
|
|
|
|
return res;
|
2024-05-30 01:50:55 +04:00
|
|
|
|
}
|
|
|
|
|
return query.Select(x => x.GetViewModel).ToList();
|
2024-04-22 21:01:26 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-01 20:40:47 +04:00
|
|
|
|
|
2024-04-22 21:01:26 +04:00
|
|
|
|
public List<DisciplineViewModel> GetFullList()
|
|
|
|
|
{
|
|
|
|
|
using var context = new UniversityDatabase();
|
|
|
|
|
return context.Disciplines
|
2024-04-23 23:03:32 +04:00
|
|
|
|
.Include(x => x.Students)
|
|
|
|
|
.ThenInclude(x => x.Student)
|
2024-05-30 01:50:55 +04:00
|
|
|
|
.Include(x => x.Teacher)
|
|
|
|
|
.ToList()
|
2024-04-22 21:01:26 +04:00
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DisciplineViewModel? Insert(DisciplineBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new UniversityDatabase();
|
2024-04-23 23:03:32 +04:00
|
|
|
|
var newDiscipline = Discipline.Create(context, model);
|
2024-04-22 21:01:26 +04:00
|
|
|
|
if (newDiscipline == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
context.Disciplines.Add(newDiscipline);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return newDiscipline.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DisciplineViewModel? Update(DisciplineBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new UniversityDatabase();
|
2024-04-23 23:03:32 +04:00
|
|
|
|
using var transaction = context.Database.BeginTransaction();
|
|
|
|
|
try
|
2024-04-22 21:01:26 +04:00
|
|
|
|
{
|
2024-04-23 23:03:32 +04:00
|
|
|
|
var discipline = context.Disciplines.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
|
if (discipline == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
discipline.Update(model);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
discipline.UpdateStudents(context, model);
|
|
|
|
|
transaction.Commit();
|
|
|
|
|
return discipline.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
transaction.Rollback();
|
|
|
|
|
throw;
|
2024-04-22 21:01:26 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-05-01 20:40:47 +04:00
|
|
|
|
|
|
|
|
|
private void CheckSearchModel(DisciplineSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
throw new ArgumentNullException("Передаваемая модель пуста", nameof(model));
|
|
|
|
|
if (model.DateFrom.HasValue != model.DateTo.HasValue)
|
|
|
|
|
throw new ArgumentException($"Не указано начало {model.DateFrom} или конец {model.DateTo} периода.");
|
|
|
|
|
}
|
2024-04-19 19:30:16 +04:00
|
|
|
|
}
|
|
|
|
|
}
|