ISEbd-21_Agliullov.D.A._Con.../ConfectionaryListImplement/StudentStorage.cs

64 lines
2.2 KiB
C#
Raw Permalink Normal View History

2024-02-02 11:10:52 +04:00
using ConfectioneryListImplement;
using EkzamenContracts.BindingModels;
using EkzamenContracts.SearchModels;
using EkzamenContracts.StoragesContract;
using EkzamenContracts.ViewModels;
namespace EkzamenListImplement
{
public class StudentStorage : IStudentStorage
{
private readonly DataListSingleton _source = DataListSingleton.GetInstance();
public List<StudentViewModel> GetFullList()
{
return _source.Students.Select(x => x.GetViewModel).ToList();
}
public List<StudentViewModel> GetFilteredList(StudentSearchModel model)
{
var result = new List<StudentViewModel>();
if (model.CreatedDateFrom is null || model.CreatedDateTo is null)
{
return result;
}
return _source.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;
}
return _source.Students.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
}
public StudentViewModel? Insert(StudentBindingModel model)
{
model.Id = _source.Students.Max(x => x.Id);
var newComponent = Student.Create(model);
if (newComponent == null)
{
return null;
}
_source.Students.Add(newComponent);
return newComponent.GetViewModel;
}
public StudentViewModel? Update(StudentBindingModel model)
{
var student = _source.Students.FirstOrDefault(x => x.Id == model.Id);
if (student != null)
{
student.Update(model);
}
return student?.GetViewModel;
}
public StudentViewModel? Delete(StudentBindingModel model)
{
var student = _source.Students.FirstOrDefault(x => x.Id == model.Id);
if (student != null)
{
_source.Students.Remove(student);
}
return student?.GetViewModel;
}
}
}