diff --git a/University/DatabaseImplement/Implements/DisciplineStorage.cs b/University/DatabaseImplement/Implements/DisciplineStorage.cs index de5a49f..4ff387a 100644 --- a/University/DatabaseImplement/Implements/DisciplineStorage.cs +++ b/University/DatabaseImplement/Implements/DisciplineStorage.cs @@ -3,6 +3,7 @@ using UniversityContracts.SearchModels; using UniversityContracts.StoragesContracts; using UniversityContracts.ViewModels; using UniversityDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; namespace UniversityDatabaseImplement.Implements { @@ -11,12 +12,15 @@ namespace UniversityDatabaseImplement.Implements public List GetFullList() { using var context = new UniversityDatabase(); - return context.Disciplines.Select(x => x.GetViewModel).ToList(); + return context.Disciplines + .Include(x => x.Statements) + .Select(x => x.GetViewModel).ToList(); } public List GetFilteredList(DisciplineSearchModel model) { using var context = new UniversityDatabase(); return context.Disciplines + .Include(x => x.Statements) .Where(x => ( (!model.Id.HasValue || x.Id == model.Id) && (string.IsNullOrEmpty(model.Name) || x.Name.Contains(model.Name)) @@ -33,7 +37,8 @@ namespace UniversityDatabaseImplement.Implements } using var context = new UniversityDatabase(); return context.Disciplines - .FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && x.Name == model.Name) || + .Include(x => x.Statements) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && x.Name == model.Name) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; } public DisciplineViewModel? Insert(DisciplineBindingModel model) diff --git a/University/DatabaseImplement/Models/Discipline.cs b/University/DatabaseImplement/Models/Discipline.cs index 6aa5679..3c35ca6 100644 --- a/University/DatabaseImplement/Models/Discipline.cs +++ b/University/DatabaseImplement/Models/Discipline.cs @@ -17,6 +17,21 @@ namespace UniversityDatabaseImplement.Models public virtual List Statements { get; set; } = new(); [ForeignKey("DisciplineId")] public virtual List ReportTypeDisciplines { get; set; } = new(); + private Dictionary? _disciplineStatements; + [NotMapped] + public Dictionary DisciplineStatements + { + get + { + if (_disciplineStatements == null) + { + _disciplineStatements = Statements.ToDictionary( + x => x.Id, x => x as IStatementModel); + } + + return _disciplineStatements; + } + } public static Discipline Create(DisciplineBindingModel model) { diff --git a/University/DatabaseImplement/Models/Student.cs b/University/DatabaseImplement/Models/Student.cs index 615ae6d..849ba54 100644 --- a/University/DatabaseImplement/Models/Student.cs +++ b/University/DatabaseImplement/Models/Student.cs @@ -17,6 +17,8 @@ namespace UniversityDatabaseImplement.Models public virtual List StatementStudents { get; set; } = new(); [ForeignKey("StudentId")] public virtual List StudentExaminationResults { get; set; } = new(); + [ForeignKey("StudentId")] + public virtual List StudentUsers { get; set; } = new(); private Dictionary? _statements; [NotMapped] diff --git a/University/DatabaseImplement/Models/User.cs b/University/DatabaseImplement/Models/User.cs index f6090e0..d9f0978 100644 --- a/University/DatabaseImplement/Models/User.cs +++ b/University/DatabaseImplement/Models/User.cs @@ -21,6 +21,10 @@ namespace UniversityDatabaseImplement.Models public string Login { get; set; } = string.Empty; [Required] public string Password { get; set; } = string.Empty; + [ForeignKey("UserId")] + public virtual List StudentUsers { get; set; } = new(); + [ForeignKey("UserId")] + public virtual List Statements { get; set; } = new(); public static User Create(UserBindingModel model) { return new User diff --git a/University/DatabaseImplement/Models/UserStudent.cs b/University/DatabaseImplement/Models/UserStudent.cs new file mode 100644 index 0000000..e7f1e29 --- /dev/null +++ b/University/DatabaseImplement/Models/UserStudent.cs @@ -0,0 +1,15 @@ +using System.ComponentModel.DataAnnotations; + +namespace UniversityDatabaseImplement.Models +{ + public class UserStudent + { + public int Id { get; set; } + [Required] + public int StudentTypeId { get; set; } + [Required] + public int UserId { get; set; } + public virtual Student Student { get; set; } = new(); + public virtual User User { get; set; } = new(); + } +} diff --git a/University/DatabaseImplement/UniversityDatabase.cs b/University/DatabaseImplement/UniversityDatabase.cs index 7561a59..6b8e224 100644 --- a/University/DatabaseImplement/UniversityDatabase.cs +++ b/University/DatabaseImplement/UniversityDatabase.cs @@ -24,5 +24,6 @@ namespace UniversityDatabaseImplement public virtual DbSet Students { set; get; } public virtual DbSet StudentExaminationResults { set; get; } public virtual DbSet Users { set; get; } + public virtual DbSet UserStudents { set; get; } } } \ No newline at end of file diff --git a/University/UniversityContracts/BindingModels/DisciplineBindingModel.cs b/University/UniversityContracts/BindingModels/DisciplineBindingModel.cs index 9b07450..627295e 100644 --- a/University/UniversityContracts/BindingModels/DisciplineBindingModel.cs +++ b/University/UniversityContracts/BindingModels/DisciplineBindingModel.cs @@ -8,5 +8,6 @@ namespace UniversityContracts.BindingModels public string Name { get; set; } = String.Empty; public string Department { get; set; } = String.Empty; public int StatementId { get; set; } + public Dictionary DisciplineStatements { get; set; } = new(); } } diff --git a/University/UniversityContracts/ViewModels/DisciplineViewModel.cs b/University/UniversityContracts/ViewModels/DisciplineViewModel.cs index 4b16129..a5f888d 100644 --- a/University/UniversityContracts/ViewModels/DisciplineViewModel.cs +++ b/University/UniversityContracts/ViewModels/DisciplineViewModel.cs @@ -8,5 +8,6 @@ namespace UniversityContracts.ViewModels public string Name { get; set; } = String.Empty; public string Department { get; set; } = String.Empty; public int StatementId { get; set; } + public Dictionary DisciplineStatements { get; set; } = new(); } } diff --git a/University/UniversityDataModels/Models/IDisciplineModel.cs b/University/UniversityDataModels/Models/IDisciplineModel.cs index c2c26ae..efd1c9d 100644 --- a/University/UniversityDataModels/Models/IDisciplineModel.cs +++ b/University/UniversityDataModels/Models/IDisciplineModel.cs @@ -4,6 +4,6 @@ { String Name { get; } String Department { get; } - int StatementId { get; } + Dictionary DisciplineStatements { get; } } }