using Microsoft.EntityFrameworkCore; using SchoolContracts.BindingModel; using SchoolContracts.SearchModel; using SchoolContracts.StoragesContracts; using SchoolContracts.ViewModels; using SchoolDatabaseImplement.Models; using System; using System.Collections.Generic; using System.Linq; using System.Runtime.ConstrainedExecution; using System.Text; using System.Threading.Tasks; namespace SchoolDatabaseImplement.Implements { public class CircleStorage : ICircleStorage { public List GetFullList() { using var context = new SchoolDatabase(); return context.Circles .Include(x => x.Client) .Select(x => x.GetViewModel) .ToList(); } public List GetFilteredList(CircleSearchModel model) { using var context = new SchoolDatabase(); return context.Circles .Where(x => x.Id == model.Id) .Include(x => x.Client) .Select(x => x.GetViewModel) .ToList(); } public CircleViewModel? GetElement(CircleSearchModel model) { if (model == null) { return null; } using var context = new SchoolDatabase(); if(model.Id.HasValue) { return context.Circles.Include(x => x.Client) .FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; } return null; } public CircleViewModel? Insert(CircleBindingModel model) { using var context = new SchoolDatabase(); var newCircle = Circle.Create(context, model); if (newCircle == null) { return null; } context.Circles.Add(newCircle); context.SaveChanges(); return newCircle.GetViewModel; } public CircleViewModel? Update(CircleBindingModel model) { using var context = new SchoolDatabase(); var circle = context.Circles.FirstOrDefault(x => x.Id == model.Id); if (circle == null) { return null; } circle.Update(model); context.SaveChanges(); return circle.GetViewModel; } public CircleViewModel? Delete(CircleBindingModel model) { using var context = new SchoolDatabase(); var circle = context.Circles.FirstOrDefault(rec => rec.Id == model.Id); if (circle != null) { context.Circles.Remove(circle); context.SaveChanges(); return circle.GetViewModel; } return null; } /// /// Получение списка занятий по выбранному кружку /// /// /// public List GetLessonCircles(CircleSearchModel model) { if (model.SelectedCirclesIds == null) { return new(); } using var context = new SchoolDatabase(); return context.Circles .Where(x => model.SelectedCirclesIds.Contains(x.Id)) .Select(x => new ReportCircleLessonsViewModel() { CircleId= x.Id, Count = x.CircleLessons.Count, Lessons = context.CircleLessons .Include(x => x.Lesson) .ThenInclude(x => x.Employee) .Include(x => x.Lesson) .ThenInclude (x => x.CircleLessons) .Where(x => x.CircleId == x.Id) .Select(x => new ReportLessonCirclesViewModel() { LessonId = x.Circle.Id, EmployeeName = x.Lesson.Employee.EmployeeName, Sum = x.Lesson.LessonPrice * x.Count }) .ToList() }) .ToList(); } } }