From fd5b88d26ee0ba853d955fe11f45b05a0e978f0a Mon Sep 17 00:00:00 2001 From: "ityurner02@mail.ru" Date: Wed, 5 Apr 2023 21:11:51 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9E=D0=B1=D0=BD=D0=BE=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=BC=D0=BE=D0=B4=D0=B5=D0=BB=D0=B5=D0=B9?= =?UTF-8?q?=20=D1=81=D0=BB=D0=BE=D1=8F=20=D1=85=D1=80=D0=B0=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F=20=D0=B4=D0=B0=D0=BD=D0=BD=D1=8B=D1=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Models/Diy.cs | 9 +++- .../Models/Interest.cs | 20 +++---- .../Models/Lesson.cs | 7 ++- .../Models/Material.cs | 16 +++--- .../Models/Product.cs | 9 +++- .../Models/Student.cs | 53 ++----------------- .../Models/StudentInterest.cs | 21 -------- .../Models/Task.cs | 8 ++- .../Models/Teacher.cs | 51 ++---------------- .../Models/TeacherMaterial.cs | 20 ------- .../SchoolDataBase.cs | 11 ++-- 11 files changed, 57 insertions(+), 168 deletions(-) delete mode 100644 SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/StudentInterest.cs delete mode 100644 SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/TeacherMaterial.cs diff --git a/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/Diy.cs b/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/Diy.cs index e50e271..58afc66 100644 --- a/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/Diy.cs +++ b/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/Diy.cs @@ -20,12 +20,15 @@ namespace SchoolAgainStudyDataBaseImplements.Models [Required] public string Description { get; set; } = string.Empty; [Required] - public DateTime DateCreate { get; set; } = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc); + public DateTime DateCreate { get; set; } [Required] public int TaskId { get; set; } public string TaskName { get; set; } = string.Empty; - public virtual Task task { get; set; } + public virtual Task Task { get; set; } + [Required] public int StudentId { get; set; } + public string StudentName { get; set; } = string.Empty; + public virtual Student Student { get; set; } private Dictionary? _DiyInterests = null; [NotMapped] public Dictionary DiyInterests { @@ -54,6 +57,7 @@ namespace SchoolAgainStudyDataBaseImplements.Models TaskId = model.TaskId, TaskName = model.TaskName, StudentId = model.StudentId, + StudentName = model.StudentName, Interests = model.DiyInterests.Select(x => new DiyInterest { Interest = context.Interests.First(y => y.Id == x.Key), @@ -77,6 +81,7 @@ namespace SchoolAgainStudyDataBaseImplements.Models TaskId = TaskId, TaskName = TaskName, StudentId = StudentId, + StudentName = StudentName, DiyInterests=DiyInterests }; diff --git a/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/Interest.cs b/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/Interest.cs index 3e82bd6..f530742 100644 --- a/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/Interest.cs +++ b/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/Interest.cs @@ -20,8 +20,9 @@ namespace SchoolAgainStudyDataBaseImplements.Models public string Description { get; set; } = string.Empty; public int Id { get; set; } - [ForeignKey("InterestId")] - public virtual List StudentInterests { get; set; } = new(); + [Required] + public int StudentId { get; set; } + public virtual Student Student { get; set; } [ForeignKey("InterestId")] public virtual List DiyInterests { get; set; } = new(); [ForeignKey("InterestId")] @@ -37,19 +38,11 @@ namespace SchoolAgainStudyDataBaseImplements.Models { Id = model.Id, Title = model.Title, - Description = model.Description + Description = model.Description, + StudentId = model.StudentId, }; } - public static Interest Create(InterestViewModel model) - { - return new Interest - { - Id = model.Id, - Title = model.Title, - Description = model.Description - }; - } public void Update(InterestBindingModel model) { @@ -65,7 +58,8 @@ namespace SchoolAgainStudyDataBaseImplements.Models { Id = Id, Title = Title, - Description = Description + Description = Description, + StudentId = StudentId, }; } } diff --git a/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/Lesson.cs b/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/Lesson.cs index 245a50e..9aed706 100644 --- a/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/Lesson.cs +++ b/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/Lesson.cs @@ -22,8 +22,11 @@ namespace SchoolAgainStudyDataBaseImplements.Models public string ProductName { get; set; } = string.Empty; - public virtual Product product { get; set; } + public virtual Product Product { get; set; } + [Required] public int TeacherId { get; set; } + public string TeacherName { get; set; } = string.Empty; + public virtual Teacher Teacher { get; set; } private Dictionary? _LessonMaterials = null; [NotMapped] public Dictionary LessonMaterials @@ -52,6 +55,7 @@ namespace SchoolAgainStudyDataBaseImplements.Models ProductId = model.ProductId, ProductName = model.ProductName, TeacherId = model.TeacherId, + TeacherName=model.TeacherName, Materials = model.LessonMaterials.Select(x => new LessonMaterial { Material = context.Materials.First(y => y.Id == x.Key), @@ -73,6 +77,7 @@ namespace SchoolAgainStudyDataBaseImplements.Models ProductId = ProductId, ProductName = ProductName, TeacherId = TeacherId, + TeacherName=TeacherName, LessonMaterials = LessonMaterials }; diff --git a/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/Material.cs b/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/Material.cs index d6e2dcc..ac952cb 100644 --- a/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/Material.cs +++ b/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/Material.cs @@ -17,15 +17,16 @@ namespace SchoolAgainStudyDataBaseImplements.Models public string Title { get; set; } = string.Empty; [Required] public string SphereUse { get; set; } = string.Empty; - + [Required] + public int TeacherId { get; set; } + public virtual Teacher Teacher { get; set; } public int Id { get; set; } [ForeignKey("MaterialId")] public virtual List LessonMaterials { get; set; } = new(); [ForeignKey("MaterialId")] public virtual List TaskMaterial { get; set; } = new(); - [ForeignKey("MaterialId")] - public virtual List TeacherMaterial { get; set; } = new(); + public static Material? Create(MaterialBindingModel model) { @@ -37,7 +38,8 @@ namespace SchoolAgainStudyDataBaseImplements.Models { Id = model.Id, Title = model.Title, - SphereUse = model.SphereUse + SphereUse = model.SphereUse, + TeacherId = model.TeacherId, }; } @@ -47,7 +49,8 @@ namespace SchoolAgainStudyDataBaseImplements.Models { Id = model.Id, Title = model.Title, - SphereUse = model.SphereUse + SphereUse = model.SphereUse, + TeacherId=model.TeacherId }; } @@ -65,7 +68,8 @@ namespace SchoolAgainStudyDataBaseImplements.Models { Id = Id, Title = Title, - SphereUse = SphereUse + SphereUse = SphereUse, + TeacherId = TeacherId, }; } } diff --git a/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/Product.cs b/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/Product.cs index b9975cb..01322cd 100644 --- a/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/Product.cs +++ b/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/Product.cs @@ -18,8 +18,11 @@ namespace SchoolAgainStudyDataBaseImplements.Models [Required] public string Description { get; set; } = string.Empty; [Required] - public DateTime DateCreate { get; set; } + public DateTime DateCreate { get; set; } + [Required] public int StudentId { get; set; } + public string StudentName { get; set; } = string.Empty; + public virtual Student Student { get; set; } private Dictionary? _ProductInterests = null; [NotMapped] public Dictionary ProductInterests @@ -34,7 +37,7 @@ namespace SchoolAgainStudyDataBaseImplements.Models return _ProductInterests; } } - + public int Id { get; set; } [ForeignKey("ProductId")] public virtual List Interests { get; set; } = new(); @@ -49,6 +52,7 @@ namespace SchoolAgainStudyDataBaseImplements.Models Description = model.Description, DateCreate = model.DateCreate, StudentId = model.StudentId, + StudentName=model.StudentName, Interests = model.ProductInterests.Select(x => new ProductInterest { Interest = context.Interests.First(y => y.Id == x.Key), @@ -70,6 +74,7 @@ namespace SchoolAgainStudyDataBaseImplements.Models Description = Description, DateCreate = DateCreate, StudentId = StudentId, + StudentName = StudentName, ProductInterests = ProductInterests }; diff --git a/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/Student.cs b/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/Student.cs index 8882334..1947ad0 100644 --- a/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/Student.cs +++ b/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/Student.cs @@ -26,25 +26,10 @@ namespace SchoolAgainStudyDataBaseImplements.Models [Required] public string Password { get; set; } = string.Empty; - private Dictionary? _StudentInterests = null; - [NotMapped] - public Dictionary StudentInterests - { - get - { - if (_StudentInterests == null) - { - _StudentInterests = Interests - .ToDictionary(recPC => recPC.InterestId, recPC => (recPC.Interest as IInterest)); - } - return _StudentInterests; - } - } - public int Id { get; set; } [ForeignKey("StudentId")] - public virtual List Interests { get; set; } = new(); + public virtual List Interests { get; set; } = new(); [ForeignKey("StudentId")] public virtual List Diys { get; set; } = new(); [ForeignKey("StudentId")] @@ -59,10 +44,7 @@ namespace SchoolAgainStudyDataBaseImplements.Models Email = model.Email, Login = model.Login, Password = model.Password, - Interests = model.StudentInterests.Select(x => new StudentInterest - { - Interest = context.Interests.First(y => y.Id == x.Key), - }).ToList() + }; } @@ -82,35 +64,10 @@ namespace SchoolAgainStudyDataBaseImplements.Models Class = Class, Email = Email, Login = Login, - Password = Password, - StudentInterests = StudentInterests + Password = Password + }; - public void UpdateInterests(SchoolDataBase context, StudentBindingModel model) - { - var studentInterests = context.StudentInterests.Where(rec => rec.StudentId == model.Id).ToList(); - if (studentInterests != null && studentInterests.Count > 0) - { - context.StudentInterests.RemoveRange(studentInterests.Where(rec => !model.StudentInterests.ContainsKey(rec.InterestId))); - context.SaveChanges(); - - foreach (var updateInterest in studentInterests) - { - model.StudentInterests.Remove(updateInterest.InterestId); - } - context.SaveChanges(); - } - var student = context.Students.First(x => x.Id == Id); - foreach (var pc in model.StudentInterests) - { - context.StudentInterests.Add(new StudentInterest - { - Student = student, - Interest = context.Interests.First(x => x.Id == pc.Key), - }); - context.SaveChanges(); - } - _StudentInterests = null; - } + } } diff --git a/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/StudentInterest.cs b/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/StudentInterest.cs deleted file mode 100644 index 783ee74..0000000 --- a/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/StudentInterest.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.ComponentModel.DataAnnotations; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace SchoolAgainStudyDataBaseImplements.Models -{ - public class StudentInterest - { - public int Id { get; set; } - [Required] - public int StudentId { get; set; } - [Required] - public int InterestId { get; set; } - public virtual Student Student { get; set; } = new(); - public virtual Interest Interest { get; set; } = new(); - } -} diff --git a/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/Task.cs b/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/Task.cs index 5b56111..f739f53 100644 --- a/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/Task.cs +++ b/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/Task.cs @@ -18,9 +18,11 @@ namespace SchoolAgainStudyDataBaseImplements.Models [Required] public DateTime DateIssue { get; set; } [Required] - public DateTime DateDelivery { get; set; } - + public DateTime DateDelivery { get; set; } + [Required] public int TeacherId { get; set; } + public string TeacherName { get; set; } = string.Empty; + public virtual Teacher Teacher { get; set; } private Dictionary? _TaskMaterials = null; [NotMapped] public Dictionary TaskMaterials { get @@ -49,6 +51,7 @@ namespace SchoolAgainStudyDataBaseImplements.Models DateIssue = model.DateIssue, DateDelivery = model.DateDelivery, TeacherId = model.TeacherId, + TeacherName = model.TeacherName, Materials = model.TaskMaterials.Select(x => new TaskMaterial { Material = context.Materials.First(y => y.Id == x.Key), @@ -70,6 +73,7 @@ namespace SchoolAgainStudyDataBaseImplements.Models DateIssue = DateIssue, DateDelivery = DateDelivery, TeacherId = TeacherId, + TeacherName= TeacherName, TaskMaterials = TaskMaterials }; diff --git a/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/Teacher.cs b/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/Teacher.cs index a545ba5..0a578eb 100644 --- a/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/Teacher.cs +++ b/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/Teacher.cs @@ -24,23 +24,10 @@ namespace SchoolAgainStudyDataBaseImplements.Models public string Login { get; set; } = string.Empty; [Required] public string Password { get; set; } = string.Empty; - private Dictionary? _TeacherMaterials = null; - [NotMapped] - public Dictionary TeacherMaterials { - get - { - if (_TeacherMaterials == null) - { - _TeacherMaterials = Materials - .ToDictionary(recPC => recPC.MaterialId, recPC => (recPC.Material as IMaterial)); - } - return _TeacherMaterials; - } - } - + public int Id { get; set; } [ForeignKey("TeacherId")] - public virtual List Materials { get; set; } = new(); + public virtual List Materials { get; set; } = new(); [ForeignKey("TeacherId")] public virtual List Tasks { get; set; } = new(); [ForeignKey("TeacherId")] @@ -55,10 +42,7 @@ namespace SchoolAgainStudyDataBaseImplements.Models Phone = model.Phone, Login = model.Login, Password = model.Password, - Materials = model.TeacherMaterials.Select(x => new TeacherMaterial - { - Material = context.Materials.First(y => y.Id == x.Key), - }).ToList() + }; } @@ -79,34 +63,9 @@ namespace SchoolAgainStudyDataBaseImplements.Models Phone = Phone, Login = Login, Password = Password, - TeacherMaterials = TeacherMaterials + }; - public void UpdateMaterials(SchoolDataBase context, TeacherBindingModel model) - { - var teacherMaterials = context.TeacherMaterials.Where(rec => rec.TeacherId == model.Id).ToList(); - if (teacherMaterials != null && teacherMaterials.Count > 0) - { - context.TeacherMaterials.RemoveRange(teacherMaterials.Where(rec => !model.TeacherMaterials.ContainsKey(rec.MaterialId))); - context.SaveChanges(); - - foreach (var updateMaterial in teacherMaterials) - { - model.TeacherMaterials.Remove(updateMaterial.MaterialId); - } - context.SaveChanges(); - } - var teacher = context.Teachers.First(x => x.Id == Id); - foreach (var pc in model.TeacherMaterials) - { - context.TeacherMaterials.Add(new TeacherMaterial - { - Teacher = teacher, - Material = context.Materials.First(x => x.Id == pc.Key), - }); - context.SaveChanges(); - } - _TeacherMaterials = null; - } + } } diff --git a/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/TeacherMaterial.cs b/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/TeacherMaterial.cs deleted file mode 100644 index 3435c05..0000000 --- a/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/TeacherMaterial.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel.DataAnnotations; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace SchoolAgainStudyDataBaseImplements.Models -{ - public class TeacherMaterial - { - public int Id { get; set; } - [Required] - public int TeacherId { get; set; } - [Required] - public int MaterialId { get; set; } - public virtual Teacher Teacher { get; set; } = new(); - public virtual Material Material { get; set; } = new(); - } -} diff --git a/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/SchoolDataBase.cs b/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/SchoolDataBase.cs index 8d7c89b..db8b74d 100644 --- a/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/SchoolDataBase.cs +++ b/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/SchoolDataBase.cs @@ -1,12 +1,13 @@ using Microsoft.EntityFrameworkCore; using SchoolAgainStudyDataBaseImplements.Models; -using SchoolAgainStudyDataModels.Models; using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; +using System.Reflection.Metadata; using System.Runtime.ConstrainedExecution; using System.Text; +using System.Threading.Tasks; using Task = SchoolAgainStudyDataBaseImplements.Models.Task; namespace SchoolAgainStudyDataBaseImplements @@ -21,11 +22,9 @@ namespace SchoolAgainStudyDataBaseImplements } base.OnConfiguring(optionsBuilder); } - + public virtual DbSet Students { set; get; } - public virtual DbSet StudentInterests { set; get; } - public virtual DbSet Interests { set; get; } public virtual DbSet Diys { set; get; } @@ -35,8 +34,6 @@ namespace SchoolAgainStudyDataBaseImplements public virtual DbSet Teachers { set; get; } - public virtual DbSet TeacherMaterials { set; get; } - public virtual DbSet Materials { set; get; } public virtual DbSet Lessons { set; get; } @@ -45,4 +42,4 @@ namespace SchoolAgainStudyDataBaseImplements public virtual DbSet TaskMaterials { set; get; } } -} \ No newline at end of file +}