From f93bc290e37d250057ddbe10a01cf9151cb8736a Mon Sep 17 00:00:00 2001 From: DyCTaTOR <125912249+DyCTaTOR@users.noreply.github.com> Date: Wed, 24 Apr 2024 14:08:27 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=B4=D0=B5=D0=BB=D0=B0=D0=BD=D0=BD?= =?UTF-8?q?=D1=8B=D0=B9=20planOfStudyTeachers?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BindingModels/DisciplineBindingModel.cs | 2 +- .../BindingModels/PlanOfStudyBindingModel.cs | 1 + .../ViewModels/PlanOfStudyViewModel.cs | 5 ++ .../Models/Attestation.cs | 43 ++++++++++ .../Models/PlanOfStudy.cs | 82 +++++++++++++++++++ .../Models/PlanOfStudyTeacher.cs | 20 +++++ .../Models/Teacher.cs | 1 + .../UniversityDatabase.cs | 5 +- 8 files changed, 156 insertions(+), 3 deletions(-) create mode 100644 University/UniversityDatabaseImplement/Models/PlanOfStudyTeacher.cs diff --git a/University/UniversityContracts/BindingModels/DisciplineBindingModel.cs b/University/UniversityContracts/BindingModels/DisciplineBindingModel.cs index 390e727..c49d61a 100644 --- a/University/UniversityContracts/BindingModels/DisciplineBindingModel.cs +++ b/University/UniversityContracts/BindingModels/DisciplineBindingModel.cs @@ -14,6 +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(); + public Dictionary StudentDisciplines { get; set; } = new(); } } diff --git a/University/UniversityContracts/BindingModels/PlanOfStudyBindingModel.cs b/University/UniversityContracts/BindingModels/PlanOfStudyBindingModel.cs index 5dae649..a2be402 100644 --- a/University/UniversityContracts/BindingModels/PlanOfStudyBindingModel.cs +++ b/University/UniversityContracts/BindingModels/PlanOfStudyBindingModel.cs @@ -13,5 +13,6 @@ namespace UniversityContracts.BindingModels public string Profile { get; set; } = string.Empty; public string FormOfStudy { get; set; } = string.Empty; public int WorkerId { get; set; } + public Dictionary PlanOfStudyTeachers { get; set; } = new(); } } diff --git a/University/UniversityContracts/ViewModels/PlanOfStudyViewModel.cs b/University/UniversityContracts/ViewModels/PlanOfStudyViewModel.cs index dbb7b07..411e916 100644 --- a/University/UniversityContracts/ViewModels/PlanOfStudyViewModel.cs +++ b/University/UniversityContracts/ViewModels/PlanOfStudyViewModel.cs @@ -16,5 +16,10 @@ namespace UniversityContracts.ViewModels public string Profile { get; set; } = string.Empty; [DisplayName("Форма обучения")] public string FormOfStudy { get; set; } = string.Empty; + public Dictionary PlanOfStudyTeachers + { + get; + set; + } = new(); } } diff --git a/University/UniversityDatabaseImplement/Models/Attestation.cs b/University/UniversityDatabaseImplement/Models/Attestation.cs index 14e2eec..dcd5f09 100644 --- a/University/UniversityDatabaseImplement/Models/Attestation.cs +++ b/University/UniversityDatabaseImplement/Models/Attestation.cs @@ -4,6 +4,8 @@ using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; +using UniversityContracts.BindingModels; +using UniversityContracts.ViewModels; using UniversityDataModels.Models; namespace UniversityDatabaseImplement.Models @@ -17,4 +19,45 @@ namespace UniversityDatabaseImplement.Models [Required] public string Score { get; private set; } = string.Empty; } + public static Student? Create(StudentBindingModel model) + { + if (model == null) + { + return null; + } + return new Student() + { + Id = model.Id, + PlanOfStudyId = model.PlanOfStudyId, + Name = model.Name, + PhoneNumber = model.PhoneNumber + }; + } + public static Student Create(StudentViewModel model) + { + return new Student + { + Id = model.Id, + PlanOfStudyId = model.PlanOfStudyId, + Name = model.Name, + PhoneNumber = model.PhoneNumber + }; + } + public void Update(StudentBindingModel model) + { + if (model == null) + { + return; + } + PhoneNumber = model.PhoneNumber; + Name = model.Name; + PlanOfStudyId = model.PlanOfStudyId; + } + public StudentViewModel GetViewModel => new() + { + Id = Id, + PlanOfStudyId = PlanOfStudyId, + Name = Name, + PhoneNumber = PhoneNumber + }; } diff --git a/University/UniversityDatabaseImplement/Models/PlanOfStudy.cs b/University/UniversityDatabaseImplement/Models/PlanOfStudy.cs index 9670bab..24ecae4 100644 --- a/University/UniversityDatabaseImplement/Models/PlanOfStudy.cs +++ b/University/UniversityDatabaseImplement/Models/PlanOfStudy.cs @@ -1,9 +1,12 @@ using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Text; using System.Threading.Tasks; +using UniversityContracts.BindingModels; +using UniversityContracts.ViewModels; using UniversityDataModels.Models; namespace UniversityDatabaseImplement.Models @@ -16,5 +19,84 @@ namespace UniversityDatabaseImplement.Models public string Profile { get; private set; } = string.Empty; [Required] public string FormOfStudy { get; private set; } = string.Empty; + private Dictionary? _planOfStudyTeachers = null; + [NotMapped] + public Dictionary PlanOfStudyTeachers + { + get + { + if (_planOfStudyTeachers == null) + { + _planOfStudyTeachers = Teachers + .ToDictionary(recPC => recPC.TeacherId, recPC => recPC.Teacher as ITeacherModel); + } + return _planOfStudyTeachers; + } + } + [ForeignKey("PlanOfStudyId")] + public virtual List Teachers { get; set; } = new(); + public static PlanOfStudy Create(UniversityDatabase context, PlanOfStudyBindingModel model) + { + return new PlanOfStudy() + { + Id = model.Id, + WorkerId = model.WorkerId, + Profile = model.Profile, + FormOfStudy = model.FormOfStudy, + Teachers = model.PlanOfStudyTeachers.Select(x => new + PlanOfStudyTeacher + { + Teacher = context.Teachers.First(y => y.Id == x.Key) + }).ToList() + }; + } + public void Update(PlanOfStudyBindingModel model) + { + if (model == null) + { + return; + } + Id = model.Id; + WorkerId = model.WorkerId; + Profile = model.Profile; + FormOfStudy = model.FormOfStudy; + } + public void UpdateTeachers(UniversityDatabase context, + PlanOfStudyBindingModel model) + { + var planOfStudyTeachers = context.PlanOfStudyTeachers + .Where(rec => rec.PlanOfStudyId == model.Id).ToList(); + if (planOfStudyTeachers != null && planOfStudyTeachers.Count > 0) + { // удалили те, которых нет в модели + context.PlanOfStudyTeachers.RemoveRange(planOfStudyTeachers.Where(rec + => !model.PlanOfStudyTeachers.ContainsKey(rec.TeacherId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateTeacher in planOfStudyTeachers) + { + model.PlanOfStudyTeachers.Remove(updateTeacher.TeacherId); + } + context.SaveChanges(); + } + var planOfStudy = context.PlanOfStudys.First(x => x.Id == Id); + foreach (var pc in model.PlanOfStudyTeachers) + { + context.PlanOfStudyTeachers.Add(new PlanOfStudyTeacher + { + PlanOfStudy = planOfStudy, + Teacher = context.Teachers.First(x => x.Id == pc.Key) + }); + context.SaveChanges(); + } + _planOfStudyTeachers = null; + } + public PlanOfStudyViewModel GetViewModel => new() + { + Id = Id, + WorkerId = WorkerId, + Profile = Profile, + FormOfStudy = FormOfStudy, + PlanOfStudyTeachers = PlanOfStudyTeachers, + }; } } diff --git a/University/UniversityDatabaseImplement/Models/PlanOfStudyTeacher.cs b/University/UniversityDatabaseImplement/Models/PlanOfStudyTeacher.cs new file mode 100644 index 0000000..fbd8a59 --- /dev/null +++ b/University/UniversityDatabaseImplement/Models/PlanOfStudyTeacher.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace UniversityDatabaseImplement.Models +{ + public class PlanOfStudyTeacher + { + public int Id { get; set; } + [Required] + public int PlanOfStudyId { get; set; } + [Required] + public int TeacherId { get; set; } + public virtual PlanOfStudy PlanOfStudy { get; set; } + public virtual Teacher Teacher { get; set; } + } +} diff --git a/University/UniversityDatabaseImplement/Models/Teacher.cs b/University/UniversityDatabaseImplement/Models/Teacher.cs index 946a6e0..2290a5f 100644 --- a/University/UniversityDatabaseImplement/Models/Teacher.cs +++ b/University/UniversityDatabaseImplement/Models/Teacher.cs @@ -28,6 +28,7 @@ namespace UniversityDatabaseImplement.Models public virtual List Statements { get; set; } = new(); [ForeignKey("TeacherId")] public virtual List Disciplines { get; set; } = new(); + public virtual List PlanOfStudyTeachers { get; set; } = new(); public static Teacher? Create(TeacherBindingModel model) { if (model == null) diff --git a/University/UniversityDatabaseImplement/UniversityDatabase.cs b/University/UniversityDatabaseImplement/UniversityDatabase.cs index e7675ef..97035d7 100644 --- a/University/UniversityDatabaseImplement/UniversityDatabase.cs +++ b/University/UniversityDatabaseImplement/UniversityDatabase.cs @@ -11,13 +11,13 @@ namespace UniversityDatabaseImplement { if (optionsBuilder.IsConfigured == false) { - //Возможно понадобится писать вместо (localdb) название пк, вот пк Егора: DESKTOP-N8BRIPR + //Возможно понадобится писать вместо (localdb) название пк, вот пк Егора: DESKTOP-N8BRIPR; other-name: LAPTOP-DYCTATOR optionsBuilder.UseSqlServer(@"Data Source=(localdb)\SQLEXPRESS;Initial Catalog=UniversityDatabaseFull;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); } base.OnConfiguring(optionsBuilder); } public virtual DbSet Students { set; get; } - public virtual DbSet PlansOfStudy { set; get; } + public virtual DbSet PlanOfStudys { set; get; } public virtual DbSet Attestations { set; get; } // public virtual DbSet Workers { set; get; } public virtual DbSet Storekeepers { set; get; } @@ -25,5 +25,6 @@ namespace UniversityDatabaseImplement public virtual DbSet Disciplines { set; get; } public virtual DbSet Statements { set; get; } public virtual DbSet StudentDisciplines { set; get; } + public virtual DbSet PlanOfStudyTeachers { set; get; } } }