78 lines
2.6 KiB
C#
78 lines
2.6 KiB
C#
using EkzamenContracts.BindingModels;
|
|
using EkzamenContracts.SearchModels;
|
|
using EkzamenContracts.StoragesContract;
|
|
using EkzamenContracts.ViewModels;
|
|
|
|
namespace EkzamenDatabaseImplement
|
|
{
|
|
public class StudentStorage : IStudentStorage
|
|
{
|
|
public List<StudentViewModel> GetFullList()
|
|
{
|
|
using var context = new ConfectioneryDatabase();
|
|
return context.Students
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
public List<StudentViewModel> GetFilteredList(StudentSearchModel model)
|
|
{
|
|
if (model.CreatedDateFrom is null || model.CreatedDateTo is null)
|
|
{
|
|
return new();
|
|
}
|
|
using var context = new ConfectioneryDatabase();
|
|
return context.Students
|
|
.Where(x => model.CreatedDateFrom <= x.DateEnrollment && x.DateEnrollment <= model.CreatedDateTo)
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
public StudentViewModel? GetElement(StudentSearchModel model)
|
|
{
|
|
if (!model.Id.HasValue)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new ConfectioneryDatabase();
|
|
return context.Students
|
|
.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)
|
|
?.GetViewModel;
|
|
}
|
|
public StudentViewModel? Insert(StudentBindingModel model)
|
|
{
|
|
var newComponent = Student.Create(model);
|
|
if (newComponent == null)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new ConfectioneryDatabase();
|
|
context.Students.Add(newComponent);
|
|
context.SaveChanges();
|
|
return newComponent.GetViewModel;
|
|
}
|
|
public StudentViewModel? Update(StudentBindingModel model)
|
|
{
|
|
using var context = new ConfectioneryDatabase();
|
|
var component = context.Students.FirstOrDefault(x => x.Id == model.Id);
|
|
if (component == null)
|
|
{
|
|
return null;
|
|
}
|
|
component.Update(model);
|
|
context.SaveChanges();
|
|
return component.GetViewModel;
|
|
}
|
|
public StudentViewModel? Delete(StudentBindingModel model)
|
|
{
|
|
using var context = new ConfectioneryDatabase();
|
|
var element = context.Students.FirstOrDefault(rec => rec.Id == model.Id);
|
|
if (element != null)
|
|
{
|
|
context.Students.Remove(element);
|
|
context.SaveChanges();
|
|
return element.GetViewModel;
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
}
|