diff --git a/University/UniversityContracts/BindingModels/DisciplineBindingModel.cs b/University/UniversityContracts/BindingModels/DisciplineBindingModel.cs index 1d607b1..390e727 100644 --- a/University/UniversityContracts/BindingModels/DisciplineBindingModel.cs +++ b/University/UniversityContracts/BindingModels/DisciplineBindingModel.cs @@ -14,5 +14,6 @@ namespace UniversityContracts.BindingModels public int TeacherId { get; set; } public string Name { get; set; } = string.Empty; public string Description { get; set; } = string.Empty; + public Dictionary StudentDisciplines { get; set; } = new(); } } diff --git a/University/UniversityContracts/ViewModels/DisciplineViewModel.cs b/University/UniversityContracts/ViewModels/DisciplineViewModel.cs index 3d9be8e..32162f3 100644 --- a/University/UniversityContracts/ViewModels/DisciplineViewModel.cs +++ b/University/UniversityContracts/ViewModels/DisciplineViewModel.cs @@ -16,5 +16,10 @@ namespace UniversityContracts.ViewModels public string Name { get; set; } = string.Empty; [DisplayName("Описание дисциплины")] public string Description { get; set; } = string.Empty; + public Dictionary StudentDisciplines + { + get; + set; + } = new(); } } diff --git a/University/UniversityDatabaseImplement/Implements/DisciplineStorage.cs b/University/UniversityDatabaseImplement/Implements/DisciplineStorage.cs index 8428ffe..6a02cd7 100644 --- a/University/UniversityDatabaseImplement/Implements/DisciplineStorage.cs +++ b/University/UniversityDatabaseImplement/Implements/DisciplineStorage.cs @@ -18,7 +18,7 @@ namespace UniversityDatabaseImplement.Implements public DisciplineViewModel? Delete(DisciplineBindingModel model) { using var context = new UniversityDatabase(); - var element = context.Disciplines.FirstOrDefault(rec => rec.Id == model.Id); + var element = context.Disciplines.Include(x => x.Students).FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { context.Disciplines.Remove(element); @@ -35,7 +35,7 @@ namespace UniversityDatabaseImplement.Implements return null; } using var context = new UniversityDatabase(); - return context.Disciplines + return context.Disciplines.Include(x => x.Students).ThenInclude(x => x.Student) .Include(x => x.Teacher) .FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && x.Name == model.Name) || (model.Id.HasValue && x.Id == model.Id)) ?.GetViewModel; @@ -50,6 +50,8 @@ namespace UniversityDatabaseImplement.Implements } using var context = new UniversityDatabase(); return context.Disciplines + .Include(x => x.Students) + .ThenInclude(x => x.Student) .Where(x => x.Name.Contains(model.Name) || x.Description.Contains(model.Description) || x.Id == model.Id || x.TeacherId == model.TeacherId) .Include(x => x.Teacher) .Select(x => x.GetViewModel) @@ -60,6 +62,9 @@ namespace UniversityDatabaseImplement.Implements { using var context = new UniversityDatabase(); return context.Disciplines + .Include(x => x.Students) + .ThenInclude(x => x.Student) + .ToList() .Select(x => x.GetViewModel) .ToList(); } @@ -67,7 +72,7 @@ namespace UniversityDatabaseImplement.Implements public DisciplineViewModel? Insert(DisciplineBindingModel model) { using var context = new UniversityDatabase(); - var newDiscipline = Discipline.Create(model); + var newDiscipline = Discipline.Create(context, model); if (newDiscipline == null) { return null; @@ -80,14 +85,25 @@ namespace UniversityDatabaseImplement.Implements public DisciplineViewModel? Update(DisciplineBindingModel model) { using var context = new UniversityDatabase(); - var discipline = context.Disciplines.FirstOrDefault(x => x.Id == model.Id); - if (discipline == null) + using var transaction = context.Database.BeginTransaction(); + try { - return null; + var discipline = context.Disciplines.FirstOrDefault(x => x.Id == model.Id); + if (discipline == null) + { + return null; + } + discipline.Update(model); + context.SaveChanges(); + discipline.UpdateStudents(context, model); + transaction.Commit(); + return discipline.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; } - discipline.Update(model); - context.SaveChanges(); - return discipline.GetViewModel; } } } diff --git a/University/UniversityDatabaseImplement/Models/Discipline.cs b/University/UniversityDatabaseImplement/Models/Discipline.cs index 36bca79..6b12277 100644 --- a/University/UniversityDatabaseImplement/Models/Discipline.cs +++ b/University/UniversityDatabaseImplement/Models/Discipline.cs @@ -21,23 +21,23 @@ namespace UniversityDatabaseImplement.Models [Required] public string Description { get; private set; } = string.Empty; public virtual Teacher Teacher { get; set; } = new(); - [ForeignKey("DisciplineId")] - public virtual List StudentDisciplines { get; set; } = new(); - public static Discipline? Create(DisciplineBindingModel model) + private Dictionary? _studentDisciplines = null; + [NotMapped] + public Dictionary StudentDisciplines { - if (model == null) + get { - return null; + if (_studentDisciplines == null) + { + _studentDisciplines = Students + .ToDictionary(recPC => recPC.StudentId, recPC => recPC.Student as IStudentModel); + } + return _studentDisciplines; } - return new Discipline() - { - Id = model.Id, - TeacherId = model.TeacherId, - Name = model.Name, - Description = model.Description, - }; } - public static Discipline Create(DisciplineViewModel model) + [ForeignKey("DisciplineId")] + public virtual List Students { get; set; } = new(); + public static Discipline Create(UniversityDatabase context, DisciplineBindingModel model) { return new Discipline() { @@ -45,6 +45,11 @@ namespace UniversityDatabaseImplement.Models TeacherId = model.TeacherId, Name = model.Name, Description = model.Description, + Students = model.StudentDisciplines.Select(x => new + StudentDiscipline + { + Student = context.Students.First(y => y.Id == x.Key) + }).ToList() }; } public void Update(DisciplineBindingModel model) @@ -58,12 +63,41 @@ namespace UniversityDatabaseImplement.Models Name = model.Name; Description = model.Description; } + public void UpdateStudents(UniversityDatabase context, + DisciplineBindingModel model) + { + var studentDisciplines = context.StudentDisciplines.Where(rec => rec.DisciplineId == model.Id).ToList(); + if (studentDisciplines != null && studentDisciplines.Count > 0) + { // удалили те, которых нет в модели + context.StudentDisciplines.RemoveRange(studentDisciplines.Where(rec + => !model.StudentDisciplines.ContainsKey(rec.StudentId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateStudent in studentDisciplines) + { + model.StudentDisciplines.Remove(updateStudent.StudentId); + } + context.SaveChanges(); + } + var discipline = context.Disciplines.First(x => x.Id == Id); + foreach (var pc in model.StudentDisciplines) + { + context.StudentDisciplines.Add(new StudentDiscipline + { + Discipline = discipline, + Student = context.Students.First(x => x.Id == pc.Key) + }); + context.SaveChanges(); + } + _studentDisciplines = null; + } public DisciplineViewModel GetViewModel => new() { Id = Id, TeacherId = TeacherId, Name = Name, Description = Description, + StudentDisciplines = StudentDisciplines, }; } } diff --git a/University/UniversityDatabaseImplement/UniversityDatabase.cs b/University/UniversityDatabaseImplement/UniversityDatabase.cs index 30a3235..e7675ef 100644 --- a/University/UniversityDatabaseImplement/UniversityDatabase.cs +++ b/University/UniversityDatabaseImplement/UniversityDatabase.cs @@ -24,5 +24,6 @@ namespace UniversityDatabaseImplement public virtual DbSet Teachers { set; get; } public virtual DbSet Disciplines { set; get; } public virtual DbSet Statements { set; get; } + public virtual DbSet StudentDisciplines { set; get; } } }