change dictionaries in database implement

This commit is contained in:
MaxKarme 2023-04-08 23:26:45 +04:00
parent 4379b3a531
commit d36d38e353
4 changed files with 24 additions and 22 deletions

View File

@ -13,8 +13,6 @@ namespace UniversityDatabaseImplement.Implements
{
using var context = new UniversityDatabase();
return context.Statements
.Include(x => x.StatementStudents)
.ThenInclude(x => x.Student)
.Select(x => x.GetViewModel)
.ToList();
}
@ -22,8 +20,6 @@ namespace UniversityDatabaseImplement.Implements
{
using var context = new UniversityDatabase();
return context.Statements
.Include(x => x.StatementStudents)
.ThenInclude(x => x.Student)
.Where(x => (
(!model.Id.HasValue || x.Id == model.Id)
)
@ -39,8 +35,6 @@ namespace UniversityDatabaseImplement.Implements
}
using var context = new UniversityDatabase();
return context.Statements
.Include(x => x.StatementStudents)
.ThenInclude(x => x.Student)
.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public StatementViewModel? Insert(StatementBindingModel model)

View File

@ -15,6 +15,8 @@ namespace UniversityDatabaseImplement.Implements
return context.Students
.Include(x => x.StudentExaminationResults)
.ThenInclude(x => x.ExaminationResult)
.Include(x => x.StatementStudents)
.ThenInclude(x => x.Statement)
.Select(x => x.GetViewModel)
.ToList();
}
@ -24,6 +26,8 @@ namespace UniversityDatabaseImplement.Implements
return context.Students
.Include(x => x.StudentExaminationResults)
.ThenInclude(x => x.ExaminationResult)
.Include(x => x.StatementStudents)
.ThenInclude(x => x.Statement)
.Where(x => (
(!model.Id.HasValue || x.Id == model.Id) &&
(string.IsNullOrEmpty(model.Name) || x.Name.Contains(model.Name))
@ -42,6 +46,8 @@ namespace UniversityDatabaseImplement.Implements
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) ||
(model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}

View File

@ -17,20 +17,6 @@ namespace UniversityDatabaseImplement.Models
public virtual List<StatementStudent> StatementStudents { get; set; } = new();
[ForeignKey("StatementId")]
public virtual List<ExaminationResult> ExaminationResults { get; set; } = new();
private Dictionary<int, IStudentModel>? _students = null;
public Dictionary<int, IStudentModel> Students
{
get
{
if(_students == null)
{
_students = StatementStudents.ToDictionary(
x => x.Student.Id, x => x.Student as IStudentModel);
}
return _students;
}
}
public static Statement Create(StatementBindingModel model)
{
return new Statement
@ -54,7 +40,6 @@ namespace UniversityDatabaseImplement.Models
Id = Id,
Date = Date,
HoursCount = HoursCount,
Students = Students
};
}

View File

@ -31,6 +31,22 @@ namespace UniversityDatabaseImplement.Models
return _results;
}
}
private Dictionary<int, IStatementModel>? _statements;
[NotMapped]
public Dictionary<int, IStatementModel> Statements
{
get
{
if (_statements == null)
{
_statements = StatementStudents.ToDictionary(
x => x.Statement.Id, x => x.Statement as IStatementModel);
}
return _statements;
}
}
public static Student Create(StudentBindingModel model)
{
return new Student
@ -54,7 +70,8 @@ namespace UniversityDatabaseImplement.Models
Id = Id,
Name = Name,
RecordCardNumber = RecordCardNumber,
Results = Results
Results = Results,
Statements = Statements
};
}