forked from DavidMakarov/StudentEnrollment
80 lines
2.9 KiB
C#
80 lines
2.9 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using StudentEnrollmentContracts.BindingModels;
|
|
using StudentEnrollmentContracts.SearchModels;
|
|
using StudentEnrollmentContracts.StorageContracts;
|
|
using StudentEnrollmentContracts.ViewModels;
|
|
using StudentEnrollmentDatabaseImplement.Models;
|
|
|
|
namespace StudentEnrollmentDatabaseImplement.Implements
|
|
{
|
|
public class ExamPointsStorage : IExamPointsStorage
|
|
{
|
|
public List<ExamPointsViewModel> GetFullList()
|
|
{
|
|
using var context = new StudentEnrollmentDatabase();
|
|
return context.exampoints
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
public List<ExamPointsViewModel> GetFilteredList(ExamPointsSearchModel model)
|
|
{
|
|
if (!model.exampoints_id.HasValue)
|
|
{
|
|
return new();
|
|
}
|
|
using var context = new StudentEnrollmentDatabase();
|
|
return context.exampoints
|
|
.Where(x => model.exampoints_id.HasValue && x.exampoints_id == model.exampoints_id)
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
public ExamPointsViewModel? GetElement(ExamPointsSearchModel model)
|
|
{
|
|
if (!model.exampoints_id.HasValue)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new StudentEnrollmentDatabase();
|
|
return context.exampoints
|
|
.FirstOrDefault(x => model.exampoints_id.HasValue && x.exampoints_id == model.exampoints_id)?
|
|
.GetViewModel;
|
|
}
|
|
public ExamPointsViewModel? Insert(ExamPointsBindingModel model)
|
|
{
|
|
using var context = new StudentEnrollmentDatabase();
|
|
var newExamPoints = ExamPoints.Create(model);
|
|
if (newExamPoints == null)
|
|
{
|
|
return null;
|
|
}
|
|
context.exampoints.Add(newExamPoints);
|
|
context.SaveChanges();
|
|
return newExamPoints.GetViewModel;
|
|
}
|
|
public ExamPointsViewModel? Update(ExamPointsBindingModel model)
|
|
{
|
|
using var context = new StudentEnrollmentDatabase();
|
|
var examPoints = context.exampoints.FirstOrDefault(x => x.exampoints_id == model.Id);
|
|
if (examPoints == null)
|
|
{
|
|
return null;
|
|
}
|
|
examPoints.Update(model);
|
|
context.SaveChanges();
|
|
return examPoints.GetViewModel;
|
|
}
|
|
public ExamPointsViewModel? Delete(ExamPointsBindingModel model)
|
|
{
|
|
using var context = new StudentEnrollmentDatabase();
|
|
var element = context.exampoints.FirstOrDefault(rec => rec.exampoints_id == model.Id);
|
|
if (element != null)
|
|
{
|
|
context.exampoints.Remove(element);
|
|
context.SaveChanges();
|
|
return element.GetViewModel;
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
}
|