using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using UniversityContracts.BindingModels; using UniversityContracts.SearchModels; using UniversityContracts.StorageContracts; using UniversityContracts.ViewModels; using UniversityDatabaseImplement.Models; namespace UniversityDatabaseImplement.Implements { public class PlanOfStudyStorage : IPlanOfStudyStorage { public List GetFullList() { using var context = new UniversityDatabase(); return context.PlanOfStudys.Select(x => x.GetViewModel).ToList(); } public List 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(); } // Получаем список дисциплин, которые соответствуют условиям поиска в модели PlanOfStudySearchModel var disciplines = students .SelectMany(s => s.StudentDiscipline) .Distinct() .ToList(); if (disciplines == null) { return new List(); } // Преобразуем список дисциплин в список DisciplineViewModel и возвращаем его return disciplines .Select(d => d.Discipline.GetViewModel).ToList(); } public List GetFilteredList(PlanOfStudySearchModel model) { using var context = new UniversityDatabase(); var query = context.PlanOfStudys .Include(x => x.Students) .Where(x => x.Id == model.Id) .AsQueryable(); if (model.DateFrom.HasValue && model.DateTo.HasValue) { query = query.Where(x => model.DateFrom.Value <= x.Date && x.Date <= model.DateTo.Value); } return new(); } 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; } } }