From d81a2ea768509d693e1f71f7e3e979f5173755b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=BE=D0=BB=D0=BE=D0=B4=D1=8F?= Date: Mon, 3 Apr 2023 20:27:31 +0300 Subject: [PATCH] =?UTF-8?q?=D0=98=D1=81=D0=BF=D0=BE=D0=BB=D0=BD=D0=B8?= =?UTF-8?q?=D1=82=D0=B5=D0=BB=D1=8C=20:=20=D1=81=D0=BB=D0=BE=D0=B9=20?= =?UTF-8?q?=D1=85=D1=80=D0=B0=D0=BD=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=B4=D0=B0?= =?UTF-8?q?=D0=BD=D0=BD=D1=8B=D1=85=20(=D0=BC=D0=BE=D0=B4=D0=B5=D0=BB?= =?UTF-8?q?=D0=B8)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Models/Diy.cs | 110 +++++++++++++++++ .../Models/DiyInterest.cs | 21 ++++ .../Models/Interest.cs | 71 +++++++++++ .../Models/Product.cs | 103 ++++++++++++++++ .../Models/ProductInterest.cs | 20 +++ .../Models/Student.cs | 116 ++++++++++++++++++ .../Models/StudentInterest.cs | 21 ++++ .../SchoolAgainStudyDataBaseImplements.csproj | 13 ++ 8 files changed, 475 insertions(+) create mode 100644 SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/Diy.cs create mode 100644 SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/DiyInterest.cs create mode 100644 SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/Interest.cs create mode 100644 SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/Product.cs create mode 100644 SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/ProductInterest.cs create mode 100644 SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/Student.cs create mode 100644 SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/StudentInterest.cs diff --git a/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/Diy.cs b/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/Diy.cs new file mode 100644 index 0000000..e50e271 --- /dev/null +++ b/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/Diy.cs @@ -0,0 +1,110 @@ +using SchoolAgainStudyContracts.BindingModel; +using SchoolAgainStudyContracts.ViewModel; +using SchoolAgainStudyDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Security.Claims; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace SchoolAgainStudyDataBaseImplements.Models +{ + public class Diy : IDiy + { + [Required] + public string Title { get; set; } = string.Empty; + [Required] + public string Description { get; set; } = string.Empty; + [Required] + public DateTime DateCreate { get; set; } = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc); + [Required] + public int TaskId { get; set; } + public string TaskName { get; set; } = string.Empty; + public virtual Task task { get; set; } + public int StudentId { get; set; } + private Dictionary? _DiyInterests = null; + [NotMapped] + public Dictionary DiyInterests { + get + { + if (_DiyInterests == null) + { + _DiyInterests = Interests + .ToDictionary(recPC => recPC.InterestId, recPC => (recPC.Interest as IInterest)); + } + return _DiyInterests; + } + } + + public int Id { get; set; } + [ForeignKey("DiyId")] + public virtual List Interests { get; set; } = new(); + public static Diy Create(SchoolDataBase context, DiyBindingModel model) + { + return new Diy() + { + Id = model.Id, + Title = model.Title, + Description = model.Description, + DateCreate = model.DateCreate, + TaskId = model.TaskId, + TaskName = model.TaskName, + StudentId = model.StudentId, + Interests = model.DiyInterests.Select(x => new DiyInterest + { + Interest = context.Interests.First(y => y.Id == x.Key), + }).ToList() + }; + } + + public void Update(DiyBindingModel model) + { + Title = model.Title; + Description = model.Description; + DateCreate = model.DateCreate; + } + + public DiyViewModel GetViewModel => new() + { + Id = Id, + Title=Title, + Description=Description, + DateCreate = DateCreate, + TaskId = TaskId, + TaskName = TaskName, + StudentId = StudentId, + DiyInterests=DiyInterests + }; + + public void UpdateInterests(SchoolDataBase context, DiyBindingModel model) + { + var diyInterests = context.DiyInterests.Where(rec => rec.DiyId == model.Id).ToList(); + if (diyInterests != null && diyInterests.Count > 0) + { + context.DiyInterests.RemoveRange(diyInterests.Where(rec => !model.DiyInterests.ContainsKey(rec.InterestId))); + context.SaveChanges(); + + foreach (var updateInterest in diyInterests) + { + model.DiyInterests.Remove(updateInterest.InterestId); + } + context.SaveChanges(); + } + var diy = context.Diys.First(x => x.Id == Id); + foreach (var pc in model.DiyInterests) + { + context.DiyInterests.Add(new DiyInterest + { + Diy = diy, + Interest = context.Interests.First(x => x.Id == pc.Key), + }); + context.SaveChanges(); + } + _DiyInterests = null; + } + } +} diff --git a/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/DiyInterest.cs b/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/DiyInterest.cs new file mode 100644 index 0000000..d9724b1 --- /dev/null +++ b/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/DiyInterest.cs @@ -0,0 +1,21 @@ +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 DiyInterest + { + public int Id { get; set; } + [Required] + public int DiyId { get; set; } + [Required] + public int InterestId { get; set; } + public virtual Diy Diy { get; set; } = new(); + public virtual Interest Interest { get; set; } = new(); + + } +} diff --git a/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/Interest.cs b/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/Interest.cs new file mode 100644 index 0000000..3e82bd6 --- /dev/null +++ b/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/Interest.cs @@ -0,0 +1,71 @@ +using SchoolAgainStudyContracts.BindingModel; +using SchoolAgainStudyContracts.ViewModel; +using SchoolAgainStudyDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SchoolAgainStudyDataBaseImplements.Models +{ + public class Interest : IInterest + { + [Required] + public string Title { get; set; } = string.Empty; + [Required] + public string Description { get; set; } = string.Empty; + + public int Id { get; set; } + [ForeignKey("InterestId")] + public virtual List StudentInterests { get; set; } = new(); + [ForeignKey("InterestId")] + public virtual List DiyInterests { get; set; } = new(); + [ForeignKey("InterestId")] + public virtual List ProductInterests { get; set; } = new(); + + public static Interest? Create(InterestBindingModel model) + { + if (model == null) + { + return null; + } + return new Interest() + { + Id = model.Id, + Title = model.Title, + Description = model.Description + }; + } + + public static Interest Create(InterestViewModel model) + { + return new Interest + { + Id = model.Id, + Title = model.Title, + Description = model.Description + }; + } + + public void Update(InterestBindingModel model) + { + if (model == null) + { + return; + } + Title = model.Title; + Description = model.Description; + } + + public InterestViewModel GetViewModel => new() + { + Id = Id, + Title = Title, + Description = Description + }; + } +} diff --git a/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/Product.cs b/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/Product.cs new file mode 100644 index 0000000..b9975cb --- /dev/null +++ b/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/Product.cs @@ -0,0 +1,103 @@ +using SchoolAgainStudyContracts.BindingModel; +using SchoolAgainStudyContracts.ViewModel; +using SchoolAgainStudyDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SchoolAgainStudyDataBaseImplements.Models +{ + public class Product : IProduct + { + [Required] + public string Title { get; set; } = string.Empty; + [Required] + public string Description { get; set; } = string.Empty; + [Required] + public DateTime DateCreate { get; set; } + public int StudentId { get; set; } + private Dictionary? _ProductInterests = null; + [NotMapped] + public Dictionary ProductInterests + { + get + { + if (_ProductInterests == null) + { + _ProductInterests = Interests + .ToDictionary(recPC => recPC.InterestId, recPC => (recPC.Interest as IInterest)); + } + return _ProductInterests; + } + } + + public int Id { get; set; } + [ForeignKey("ProductId")] + public virtual List Interests { get; set; } = new(); + [ForeignKey("ProductId")] + public virtual List Lessons { get; set; } = new(); + public static Product Create(SchoolDataBase context, ProductBindingModel model) + { + return new Product() + { + Id = model.Id, + Title = model.Title, + Description = model.Description, + DateCreate = model.DateCreate, + StudentId = model.StudentId, + Interests = model.ProductInterests.Select(x => new ProductInterest + { + Interest = context.Interests.First(y => y.Id == x.Key), + }).ToList() + }; + } + + public void Update(ProductBindingModel model) + { + Title = model.Title; + Description = model.Description; + DateCreate = model.DateCreate; + } + + public ProductViewModel GetViewModel => new() + { + Id = Id, + Title = Title, + Description = Description, + DateCreate = DateCreate, + StudentId = StudentId, + ProductInterests = ProductInterests + }; + + public void UpdateInterests(SchoolDataBase context, ProductBindingModel model) + { + var productInterests = context.ProductInterests.Where(rec => rec.ProductId == model.Id).ToList(); + if (productInterests != null && productInterests.Count > 0) + { + context.ProductInterests.RemoveRange(productInterests.Where(rec => !model.ProductInterests.ContainsKey(rec.InterestId))); + context.SaveChanges(); + + foreach (var updateInterest in productInterests) + { + model.ProductInterests.Remove(updateInterest.InterestId); + } + context.SaveChanges(); + } + var product = context.Products.First(x => x.Id == Id); + foreach (var pc in model.ProductInterests) + { + context.ProductInterests.Add(new ProductInterest + { + Product = product, + Interest = context.Interests.First(x => x.Id == pc.Key), + }); + context.SaveChanges(); + } + _ProductInterests = null; + } + } +} diff --git a/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/ProductInterest.cs b/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/ProductInterest.cs new file mode 100644 index 0000000..534359d --- /dev/null +++ b/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/ProductInterest.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 SchoolAgainStudyDataBaseImplements.Models +{ + public class ProductInterest + { + public int Id { get; set; } + [Required] + public int ProductId { get; set; } + [Required] + public int InterestId { get; set; } + public virtual Product Product { get; set; } = new(); + public virtual Interest Interest { get; set; } = new(); + } +} diff --git a/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/Student.cs b/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/Student.cs new file mode 100644 index 0000000..8882334 --- /dev/null +++ b/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/Student.cs @@ -0,0 +1,116 @@ +using SchoolAgainStudyContracts.BindingModel; +using SchoolAgainStudyContracts.ViewModel; +using SchoolAgainStudyDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Diagnostics; +using System.Linq; +using System.Runtime.ConstrainedExecution; +using System.Text; +using System.Threading.Tasks; + +namespace SchoolAgainStudyDataBaseImplements.Models +{ + public class Student : IStudent + { + [Required] + public string Name { get; set; } = string.Empty; + [Required] + public int Class { get; set; } + [Required] + public string Email { get; set; } = string.Empty; + [Required] + public string Login { get; set; } = string.Empty; + [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(); + [ForeignKey("StudentId")] + public virtual List Diys { get; set; } = new(); + [ForeignKey("StudentId")] + public virtual List Products { get; set; } = new(); + public static Student Create(SchoolDataBase context, StudentBindingModel model) + { + return new Student() + { + Id = model.Id, + Name = model.Name, + Class = model.Class, + 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() + }; + } + + public void Update(StudentBindingModel model) + { + Name = model.Name; + Class = model.Class; + Email = model.Email; + Login = model.Login; + Password = model.Password; + } + + public StudentViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + Class = Class, + Email = Email, + Login = Login, + Password = Password, + StudentInterests = StudentInterests + }; + + 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 new file mode 100644 index 0000000..783ee74 --- /dev/null +++ b/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/StudentInterest.cs @@ -0,0 +1,21 @@ +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/SchoolAgainStudyDataBaseImplements.csproj b/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/SchoolAgainStudyDataBaseImplements.csproj index 132c02c..6b028b0 100644 --- a/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/SchoolAgainStudyDataBaseImplements.csproj +++ b/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/SchoolAgainStudyDataBaseImplements.csproj @@ -6,4 +6,17 @@ enable + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + +