ISEbd-21_Tukaeva_A_A_School/School/SchoolDatabaseImplements/Implements/CircleLessonStorage.cs

95 lines
2.4 KiB
C#

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<CircleLessonViewModel> 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<CircleLessonViewModel> 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;
}
}
}