88 lines
2.8 KiB
C#
88 lines
2.8 KiB
C#
using StudentPerformanceContracts.BindingModels;
|
|
using StudentPerformanceContracts.SearchModels;
|
|
using StudentPerformanceContracts.StorageContracts;
|
|
using StudentPerformanceContracts.ViewModels;
|
|
using StudentPerformanceDatabaseImplement.Models;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System.Security.Principal;
|
|
|
|
namespace StudentPerformanceDatabaseImplement.Implements
|
|
{
|
|
public class StudentStorage : IStudentStorage
|
|
{
|
|
public List<StudentViewModel> GetFullList()
|
|
{
|
|
using var context = new StudentsDatabase();
|
|
return context.Students
|
|
.Include(x => x.Format)
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
public List<StudentViewModel> GetFilteredList(StudentSearchModel model)
|
|
{
|
|
if (!model.Id.HasValue)
|
|
{
|
|
return new();
|
|
}
|
|
|
|
using var context = new StudentsDatabase();
|
|
return context.Students
|
|
.Include(x => x.Format)
|
|
.Where(x => x.Id == model.Id)
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
public StudentViewModel? GetElement(StudentSearchModel model)
|
|
{
|
|
if (!model.Id.HasValue)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
using var context = new StudentsDatabase();
|
|
return context.Students
|
|
.Include(x => x.Format)
|
|
.FirstOrDefault(x => x.Id == model.Id)
|
|
?.GetViewModel;
|
|
}
|
|
public StudentViewModel? Insert(StudentBindingModel model)
|
|
{
|
|
using var context = new StudentsDatabase();
|
|
var newOrder = Students.Create(context, model);
|
|
if (newOrder == null)
|
|
{
|
|
return null;
|
|
}
|
|
context.Students.Add(newOrder);
|
|
context.SaveChanges();
|
|
return newOrder.GetViewModel;
|
|
}
|
|
public StudentViewModel? Update(StudentBindingModel model)
|
|
{
|
|
using var context = new StudentsDatabase();
|
|
var Order = context.Students.FirstOrDefault(x => x.Id == model.Id);
|
|
if (Order == null)
|
|
{
|
|
return null;
|
|
}
|
|
Order.Update(model, context);
|
|
context.SaveChanges();
|
|
return Order.GetViewModel;
|
|
}
|
|
public StudentViewModel? Delete(StudentBindingModel model)
|
|
{
|
|
using var context = new StudentsDatabase();
|
|
var element = context.Students
|
|
.Include(x => x.Format)
|
|
.FirstOrDefault(rec => rec.Id == model.Id);
|
|
if (element != null)
|
|
{
|
|
context.Students.Remove(element);
|
|
context.SaveChanges();
|
|
return element.GetViewModel;
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
}
|