122 lines
4.2 KiB
C#
122 lines
4.2 KiB
C#
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<LessonViewModel> 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<LessonViewModel> 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)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new SchoolDataBase();
|
|
return context.Lessons
|
|
.Include(x => x.Materials)
|
|
.ThenInclude(x => x.Material)
|
|
.FirstOrDefault(x => ((!string.IsNullOrEmpty(model.Title) && x.Title == model.Title) ||
|
|
(model.Id.HasValue && x.Id == model.Id)) && x.TeacherId == model.TeacherId)
|
|
?.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;
|
|
}
|
|
}
|
|
}
|