SUBD_SchoolSchedule/SchoolSchedule/SchoolScheduleDataBaseImplement/Implements/LessonStorage.cs
2024-04-08 22:02:19 +04:00

84 lines
3.0 KiB
C#

using Microsoft.EntityFrameworkCore;
using SchoolScheduleContracts.BindingModels;
using SchoolScheduleContracts.SearchModels;
using SchoolScheduleContracts.StoragesContracts;
using SchoolScheduleContracts.ViewModels;
using SchoolScheduleDataBaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SchoolScheduleDataBaseImplement.Implements
{
public class LessonStorage : ILessonStorage
{
public List<LessonViewModel> GetFullList()
{
using var context = new SchoolScheduleDataBase();
return context.Lessons.Include(x => x.Grade).Include(x => x.Subject).Include(x => x.SchedulePlace).Include(x => x.Teacher)
.Select(x => x.GetViewModel)
.ToList();
}
public List<LessonViewModel> GetFilteredList(LessonSearchModel model)
{
using var context = new SchoolScheduleDataBase();
return context.Lessons.Include(x => x.Grade)
.Where(x => (!model.Id.HasValue || x.Id == model.Id) && (!model.GradeId.HasValue || x.GradeId == model.GradeId)
&& (!model.TeacherId.HasValue || x.TeacherId == model.TeacherId) && (!model.SubjectId.HasValue || x.TeacherId == model.SubjectId)
&& (!model.Date.HasValue || x.Date == model.Date) && (!model.SchedulePlaceId.HasValue || x.SchedulePlaceId == model.SchedulePlaceId)
&& (!model.DateFrom.HasValue || x.Date >= model.DateFrom) && (!model.DateTo.HasValue || x.Date <= model.DateTo))
.Select(x => x.GetViewModel)
.ToList();
}
public LessonViewModel? GetElement(LessonSearchModel model)
{
using var context = new SchoolScheduleDataBase();
return context.Lessons.Include(x => x.Teacher)
.FirstOrDefault(x => (!model.Id.HasValue || x.Id == model.Id) && (!model.GradeId.HasValue || x.GradeId == model.GradeId)
&& (!model.TeacherId.HasValue || x.TeacherId == model.TeacherId) && (!model.SubjectId.HasValue || x.TeacherId == model.SubjectId)
&& (!model.Date.HasValue || x.Date == model.Date) && (!model.SchedulePlaceId.HasValue || x.SchedulePlaceId == model.SchedulePlaceId))
?.GetViewModel;
}
public LessonViewModel? Insert(LessonBindingModel model)
{
using var context = new SchoolScheduleDataBase();
var newComponent = Lesson.Create(context, model);
if (newComponent == null)
{
return null;
}
context.Lessons.Add(newComponent);
context.SaveChanges();
return newComponent.GetViewModel;
}
public LessonViewModel? Update(LessonBindingModel model)
{
using var context = new SchoolScheduleDataBase();
var component = context.Lessons.FirstOrDefault(x => x.Id ==
model.Id);
if (component == null)
{
return null;
}
component.Update(context, model);
context.SaveChanges();
return component.GetViewModel;
}
public LessonViewModel? Delete(LessonBindingModel model)
{
using var context = new SchoolScheduleDataBase();
var element = context.Lessons.FirstOrDefault(rec => rec.Id ==
model.Id);
if (element != null)
{
context.Lessons.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}