CourseWork/University/UniversityDatabaseImplement/Implements/PlanOfStudyStorage.cs

128 lines
4.4 KiB
C#
Raw Normal View History

using Microsoft.EntityFrameworkCore;
using System;
2024-04-18 20:20:50 +04:00
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UniversityContracts.BindingModels;
using UniversityContracts.SearchModels;
2024-04-18 20:20:50 +04:00
using UniversityContracts.StorageContracts;
using UniversityContracts.ViewModels;
using UniversityDatabaseImplement.Models;
2024-04-18 20:20:50 +04:00
namespace UniversityDatabaseImplement.Implements
{
public class PlanOfStudyStorage : IPlanOfStudyStorage
{
public List<PlanOfStudyViewModel> GetFullList()
{
using var context = new UniversityDatabase();
return context.PlanOfStudys.Select(x => x.GetViewModel).ToList();
}
2024-05-02 17:22:43 +04:00
public List<DisciplineViewModel> GetDisciplineFromStudentsFromPlanOfStudys(PlanOfStudySearchModel model)
{
using var context = new UniversityDatabase();
var students = context.Students
.Where(s => s.PlanOfStudyId == model.Id)
.Include(s => s.StudentDiscipline)
.ToList();
if(students == null)
{
return new List<DisciplineViewModel>();
}
// Получаем список дисциплин, которые соответствуют условиям поиска в модели PlanOfStudySearchModel
var disciplines = students
.SelectMany(s => s.StudentDiscipline)
.Distinct()
.ToList();
if (disciplines == null)
{
return new List<DisciplineViewModel>();
}
// Преобразуем список дисциплин в список DisciplineViewModel и возвращаем его
return disciplines
.Select(d => d.Discipline.GetViewModel).ToList();
}
public List<PlanOfStudyViewModel> GetFilteredList(PlanOfStudySearchModel model)
{
if(model == null)
{
return new();
}
using var context = new UniversityDatabase();
2024-05-02 17:22:43 +04:00
var query = context.PlanOfStudys
.Include(x => x.Students)
.Include(x => x.User)
2024-05-02 17:22:43 +04:00
.AsQueryable();
if (model.Id.HasValue)
{
query = query.Where(x => x.Id == model.Id.Value);
}
2024-05-02 17:22:43 +04:00
if (model.DateFrom.HasValue && model.DateTo.HasValue)
{
2024-05-02 17:22:43 +04:00
query = query.Where(x => model.DateFrom.Value <= x.Date && x.Date <= model.DateTo.Value);
};
return query.Select(x => x.GetViewModel).ToList();
}
public PlanOfStudyViewModel? GetElement(PlanOfStudySearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new UniversityDatabase();
return context.PlanOfStudys.Include(x => x.Profile).FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public PlanOfStudyViewModel? Insert(PlanOfStudyBindingModel model)
{
using var context = new UniversityDatabase();
var newAttestation = PlanOfStudy.Create(context, model);
if (newAttestation == null)
{
return null;
}
context.PlanOfStudys.Add(newAttestation);
context.SaveChanges();
return newAttestation.GetViewModel;
}
public PlanOfStudyViewModel? Update(PlanOfStudyBindingModel model)
{
using var context = new UniversityDatabase();
var order = context.PlanOfStudys.FirstOrDefault(x => x.Id == model.Id);
if (order == null)
{
return null;
}
order.Update(model);
context.SaveChanges();
return context.PlanOfStudys.Include(x => x.Profile).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
}
public PlanOfStudyViewModel? Delete(PlanOfStudyBindingModel model)
{
using var context = new UniversityDatabase();
var element = context.PlanOfStudys.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.PlanOfStudys.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
2024-04-18 20:20:50 +04:00
}
}