using Microsoft.EntityFrameworkCore; using SchoolContracts.BindingModels; using SchoolContracts.SearchModel; using SchoolContracts.StoragesContracts; using SchoolContracts.ViewModels; using SchoolDatabaseImplement; using SchoolDatabaseImplement.Models; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SchoolDatabaseImplements.Implements { public class CircleLessonStorage : ICircleLessonStorage { public CircleLessonViewModel? Delete(CircleLessonBindingModel model) { using var context = new SchoolDatabase(); var circlelesson = context.CircleLessons.FirstOrDefault(rec => rec.Id == model.Id); if (circlelesson != null) { context.CircleLessons.Remove(circlelesson); context.SaveChanges(); return circlelesson.GetViewModel; } return null; } public CircleLessonViewModel? GetElement(CircleLessonSearchModel model) { if (model == null) { return null; } using var context = new SchoolDatabase(); if (model.Id.HasValue) { return context.CircleLessons.Include(x => x.Circle) .Include(x => x.Lesson) .FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; } return null; } public List GetFilteredList(CircleLessonSearchModel model) { using var context = new SchoolDatabase(); return context.CircleLessons .Where(x => x.Id == model.Id) .Include(x => x.Circle) .Include(x => x.Lesson) .Select(x => x.GetViewModel) .ToList(); } public List GetFullList() { using var context = new SchoolDatabase(); return context.CircleLessons .Include(x => x.Circle) .Include(x => x.Lesson) .Select(x => x.GetViewModel) .ToList(); } public CircleLessonViewModel? Insert(CircleLessonBindingModel model) { using var context = new SchoolDatabase(); var newCircleLesson = CircleLesson.Create(context, model); if (newCircleLesson == null) { return null; } context.CircleLessons.Add(newCircleLesson); context.SaveChanges(); return newCircleLesson.GetViewModel; } public CircleLessonViewModel? Update(CircleLessonBindingModel model) { using var context = new SchoolDatabase(); var circlelesson = context.CircleLessons.FirstOrDefault(x => x.Id == model.Id); if (circlelesson == null) { return null; } circlelesson.Update(model); context.SaveChanges(); return circlelesson.GetViewModel; } } }