refactoring and add date filter

This commit is contained in:
MaxKarme 2023-04-09 00:18:16 +04:00
parent 7c1c32175a
commit cc04070287
5 changed files with 32 additions and 25 deletions

View File

@ -3,6 +3,7 @@ using UniversityContracts.SearchModels;
using UniversityContracts.StoragesContracts;
using UniversityContracts.ViewModels;
using UniversityDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
namespace UniversityDatabaseImplement.Implements
{
@ -11,14 +12,21 @@ namespace UniversityDatabaseImplement.Implements
public List<ExaminationResultViewModel> GetFullList()
{
using var context = new UniversityDatabase();
return context.ExaminationResults.Select(x => x.GetViewModel).ToList();
return context.ExaminationResults
.Include(x => x.StudentExaminationResults)
.ThenInclude(x => x.Student)
.Select(x => x.GetViewModel).ToList();
}
public List<ExaminationResultViewModel> GetFilteredList(ExaminationResultSearchModel model)
{
using var context = new UniversityDatabase();
return context.ExaminationResults
.Include(x => x.StudentExaminationResults)
.ThenInclude(x => x.Student)
.Where(x => (
(!model.Id.HasValue || x.Id == model.Id)
(!model.Id.HasValue || x.Id == model.Id) &&
(!model.From.HasValue || x.Date >= model.From) &&
(!model.To.HasValue || x.Date <= model.To)
)
)
.Select(x => x.GetViewModel)
@ -32,7 +40,9 @@ namespace UniversityDatabaseImplement.Implements
}
using var context = new UniversityDatabase();
return context.ExaminationResults
.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
.Include(x => x.StudentExaminationResults)
.ThenInclude(x => x.Student)
.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public ExaminationResultViewModel? Insert(ExaminationResultBindingModel model)
{

View File

@ -21,7 +21,9 @@ namespace UniversityDatabaseImplement.Implements
using var context = new UniversityDatabase();
return context.Statements
.Where(x => (
(!model.Id.HasValue || x.Id == model.Id)
(!model.Id.HasValue || x.Id == model.Id) &&
(!model.From.HasValue || x.Date >= model.From) &&
(!model.To.HasValue || x.Date <= model.To)
)
)
.Select(x => x.GetViewModel)

View File

@ -13,8 +13,6 @@ namespace UniversityDatabaseImplement.Implements
{
using var context = new UniversityDatabase();
return context.Students
.Include(x => x.StudentExaminationResults)
.ThenInclude(x => x.ExaminationResult)
.Include(x => x.StatementStudents)
.ThenInclude(x => x.Statement)
.Select(x => x.GetViewModel)
@ -24,8 +22,6 @@ namespace UniversityDatabaseImplement.Implements
{
using var context = new UniversityDatabase();
return context.Students
.Include(x => x.StudentExaminationResults)
.ThenInclude(x => x.ExaminationResult)
.Include(x => x.StatementStudents)
.ThenInclude(x => x.Statement)
.Where(x => (
@ -44,8 +40,6 @@ namespace UniversityDatabaseImplement.Implements
}
using var context = new UniversityDatabase();
return context.Students
.Include(x => x.StudentExaminationResults)
.ThenInclude(x => x.ExaminationResult)
.Include(x => x.StatementStudents)
.ThenInclude(x => x.Statement)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && x.Name== model.Name) ||

View File

@ -19,6 +19,22 @@ namespace UniversityDatabaseImplement.Models
public virtual List<Activity> Activities { get; set; } = new();
[ForeignKey("ExaminationResultId")]
public virtual List<StudentExaminationResult> StudentExaminationResults { get; set; } = new();
private Dictionary<int, IStudentModel>? _students;
[NotMapped]
public Dictionary<int, IStudentModel> Students
{
get
{
if (_students == null)
{
_students = StudentExaminationResults.ToDictionary(
x => x.Student.Id, x => x.Student as IStudentModel);
}
return _students;
}
}
public static ExaminationResult Create(ExaminationResultBindingModel model)
{
return new ExaminationResult

View File

@ -18,20 +18,6 @@ namespace UniversityDatabaseImplement.Models
[ForeignKey("StudentId")]
public virtual List<StudentExaminationResult> StudentExaminationResults { get; set; } = new();
private Dictionary<int, IExaminationResultModel>? _results;
[NotMapped]
public Dictionary<int, IExaminationResultModel> Results {
get {
if(_results == null)
{
_results = StudentExaminationResults.ToDictionary(
x => x.ExaminationResult.Id, x => x.ExaminationResult as IExaminationResultModel);
}
return _results;
}
}
private Dictionary<int, IStatementModel>? _statements;
[NotMapped]
public Dictionary<int, IStatementModel> Statements
@ -70,7 +56,6 @@ namespace UniversityDatabaseImplement.Models
Id = Id,
Name = Name,
RecordCardNumber = RecordCardNumber,
Results = Results,
Statements = Statements
};