using Microsoft.EntityFrameworkCore; using SchoolAgainStudyContracts.BindingModel; using SchoolAgainStudyContracts.SearchModel; using SchoolAgainStudyContracts.StorageContracts; using SchoolAgainStudyContracts.ViewModel; using SchoolAgainStudyDataBaseImplements.Models; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SchoolAgainStudyDataBaseImplements.Implements { public class LessonStorage : ILessonStorage { public List GetFullList() { using var context = new SchoolDataBase(); return context.Lessons .Include(x => x.Materials) .ThenInclude(x => x.Material) .ToList() .Select(x => x.GetViewModel) .ToList(); } public List GetFilteredList(LessonSearchModel model) { if (!model.TeacherId.HasValue) { return new(); } using var context = new SchoolDataBase(); if (model.TeacherId.HasValue && model.DateFrom.HasValue && model.DateTo.HasValue) { return context.Lessons .Include(x => x.Materials) .ThenInclude(x => x.Material) .Where(x => x.DateEvent >= model.DateFrom && x.DateEvent <= model.DateTo && x.TeacherId == model.TeacherId) .ToList() .Select(x => x.GetViewModel) .ToList(); } return context.Lessons .Include(x => x.Materials) .ThenInclude(x => x.Material) .Where(x => x.TeacherId == model.TeacherId) .ToList() .Select(x => x.GetViewModel) .ToList(); } public LessonViewModel? GetElement(LessonSearchModel model) { if (string.IsNullOrEmpty(model.Title) && !model.Id.HasValue && !model.TeacherId.HasValue && !model.DateCreate.HasValue) { return null; } using var context = new SchoolDataBase(); if(!string.IsNullOrEmpty(model.Title)&& model.TeacherId.HasValue && model.DateCreate.HasValue) return context.Lessons .Include(x => x.Materials) .ThenInclude(x => x.Material) .FirstOrDefault(x => x.Title.Equals(model.Title) && x.DateEvent == model.DateCreate && x.TeacherId == model.TeacherId) ?.GetViewModel; return context.Lessons .Include(x => x.Materials) .ThenInclude(x => x.Material) .FirstOrDefault(x => x.Id == model.Id) ?.GetViewModel; } public LessonViewModel? Insert(LessonBindingModel model) { using var context = new SchoolDataBase(); var newLesson = Lesson.Create(context, model); if (newLesson == null) { return null; } context.Lessons.Add(newLesson); context.SaveChanges(); return newLesson.GetViewModel; } public LessonViewModel? Update(LessonBindingModel model) { using var context = new SchoolDataBase(); using var transaction = context.Database.BeginTransaction(); try { var lesson = context.Lessons.FirstOrDefault(rec => rec.Id == model.Id); if (lesson == null) { return null; } lesson.Update(model); context.SaveChanges(); lesson.UpdateMaterials(context, model); transaction.Commit(); return lesson.GetViewModel; } catch { transaction.Rollback(); throw; } } public LessonViewModel? Delete(LessonBindingModel model) { using var context = new SchoolDataBase(); var element = context.Lessons .Include(x => x.Materials) .FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { context.Lessons.Remove(element); context.SaveChanges(); return element.GetViewModel; } return null; } } }