83 lines
2.5 KiB
C#
83 lines
2.5 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 TeacherStorage : ITeacherStorage
|
|
{
|
|
public List<TeacherViewModel> GetFullList()
|
|
{
|
|
using var context = new SchoolScheduleDataBase();
|
|
return context.Teachers.Include(x => x.Grade)
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
public List<TeacherViewModel> GetFilteredList(TeacherSearchModel model)
|
|
{
|
|
using var context = new SchoolScheduleDataBase();
|
|
return context.Teachers.Include(x => x.Grade)
|
|
.Where(x => (!model.Id.HasValue || x.Id == model.Id) && (string.IsNullOrEmpty(model.FullName) || x.FullName == model.FullName))
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
public TeacherViewModel? GetElement(TeacherSearchModel model)
|
|
{
|
|
if (!model.Id.HasValue && string.IsNullOrEmpty(model.FullName))
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new SchoolScheduleDataBase();
|
|
return context.Teachers.Include(x => x.Grade)
|
|
.FirstOrDefault(x => (!model.Id.HasValue || x.Id == model.Id) && (string.IsNullOrEmpty(model.FullName) || x.FullName == model.FullName))
|
|
?.GetViewModel;
|
|
}
|
|
public TeacherViewModel? Insert(TeacherBindingModel model)
|
|
{
|
|
using var context = new SchoolScheduleDataBase();
|
|
var newComponent = Teacher.Create(context, model);
|
|
if (newComponent == null)
|
|
{
|
|
return null;
|
|
}
|
|
context.Teachers.Add(newComponent);
|
|
context.SaveChanges();
|
|
return newComponent.GetViewModel;
|
|
}
|
|
public TeacherViewModel? Update(TeacherBindingModel model)
|
|
{
|
|
using var context = new SchoolScheduleDataBase();
|
|
var component = context.Teachers.FirstOrDefault(x => x.Id ==
|
|
model.Id);
|
|
if (component == null)
|
|
{
|
|
return null;
|
|
}
|
|
component.Update(context, model);
|
|
context.SaveChanges();
|
|
return component.GetViewModel;
|
|
}
|
|
public TeacherViewModel? Delete(TeacherBindingModel model)
|
|
{
|
|
using var context = new SchoolScheduleDataBase();
|
|
var element = context.Teachers.FirstOrDefault(rec => rec.Id ==
|
|
model.Id);
|
|
if (element != null)
|
|
{
|
|
context.Teachers.Remove(element);
|
|
context.SaveChanges();
|
|
return element.GetViewModel;
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
}
|