diff --git a/SchoolAgainStudy/SchoolAgainStudy.sln b/SchoolAgainStudy/SchoolAgainStudy.sln index f9de9e6..3ee18f1 100644 --- a/SchoolAgainStudy/SchoolAgainStudy.sln +++ b/SchoolAgainStudy/SchoolAgainStudy.sln @@ -9,6 +9,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SchoolAgainStudyDataModels" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SchoolAgainStudyContracts", "SchoolAgainStudyContracts\SchoolAgainStudyContracts.csproj", "{5D678B52-4EDB-439A-BF15-E18280D39585}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SchoolAgainStudyDataBaseImplements", "SchoolAgainStudyDataBaseImplements\SchoolAgainStudyDataBaseImplements.csproj", "{7B3598B3-8AE0-4353-B967-0D9141F2798F}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -27,6 +29,10 @@ Global {5D678B52-4EDB-439A-BF15-E18280D39585}.Debug|Any CPU.Build.0 = Debug|Any CPU {5D678B52-4EDB-439A-BF15-E18280D39585}.Release|Any CPU.ActiveCfg = Release|Any CPU {5D678B52-4EDB-439A-BF15-E18280D39585}.Release|Any CPU.Build.0 = Release|Any CPU + {7B3598B3-8AE0-4353-B967-0D9141F2798F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7B3598B3-8AE0-4353-B967-0D9141F2798F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7B3598B3-8AE0-4353-B967-0D9141F2798F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7B3598B3-8AE0-4353-B967-0D9141F2798F}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/SchoolAgainStudy/SchoolAgainStudyContracts/SchoolAgainStudyContracts.csproj b/SchoolAgainStudy/SchoolAgainStudyContracts/SchoolAgainStudyContracts.csproj index 132c02c..004cf8d 100644 --- a/SchoolAgainStudy/SchoolAgainStudyContracts/SchoolAgainStudyContracts.csproj +++ b/SchoolAgainStudy/SchoolAgainStudyContracts/SchoolAgainStudyContracts.csproj @@ -6,4 +6,16 @@ enable + + + + + + + + + + + + diff --git a/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/Lesson.cs b/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/Lesson.cs new file mode 100644 index 0000000..245a50e --- /dev/null +++ b/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/Lesson.cs @@ -0,0 +1,106 @@ +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.Text; +using System.Threading.Tasks; + +namespace SchoolAgainStudyDataBaseImplements.Models +{ + public class Lesson : ILesson + { + [Required] + public string Title { get; set; } = string.Empty; + [Required] + public DateTime DateEvent { get; set; } + [Required] + public int ProductId { get; set; } + + public string ProductName { get; set; } = string.Empty; + + public virtual Product product { get; set; } + public int TeacherId { get; set; } + private Dictionary? _LessonMaterials = null; + [NotMapped] + public Dictionary LessonMaterials + { get + { + if (_LessonMaterials == null) + { + _LessonMaterials = Materials + .ToDictionary(recPC => recPC.MaterialId, recPC => (recPC.Material as IMaterial)); + } + return _LessonMaterials; + } + } + + public int Id { get; set; } + + [ForeignKey("LessonId")] + public virtual List Materials { get; set; } = new(); + public static Lesson Create(SchoolDataBase context, LessonBindingModel model) + { + return new Lesson() + { + Id = model.Id, + Title = model.Title, + DateEvent = model.DateEvent, + ProductId = model.ProductId, + ProductName = model.ProductName, + TeacherId = model.TeacherId, + Materials = model.LessonMaterials.Select(x => new LessonMaterial + { + Material = context.Materials.First(y => y.Id == x.Key), + }).ToList() + }; + } + + public void Update(LessonBindingModel model) + { + Title = model.Title; + DateEvent = model.DateEvent; + } + + public LessonViewModel GetViewModel => new() + { + Id = Id, + Title = Title, + DateEvent = DateEvent, + ProductId = ProductId, + ProductName = ProductName, + TeacherId = TeacherId, + LessonMaterials = LessonMaterials + }; + + public void UpdateMaterials(SchoolDataBase context, LessonBindingModel model) + { + var lessonMaterials = context.LessonMaterials.Where(rec => rec.LessonId == model.Id).ToList(); + if (lessonMaterials != null && lessonMaterials.Count > 0) + { + context.LessonMaterials.RemoveRange(lessonMaterials.Where(rec => !model.LessonMaterials.ContainsKey(rec.MaterialId))); + context.SaveChanges(); + + foreach (var updateMaterial in lessonMaterials) + { + model.LessonMaterials.Remove(updateMaterial.MaterialId); + } + context.SaveChanges(); + } + var lesson = context.Lessons.First(x => x.Id == Id); + foreach (var pc in model.LessonMaterials) + { + context.LessonMaterials.Add(new LessonMaterial + { + Lesson = lesson, + Material = context.Materials.First(x => x.Id == pc.Key), + }); + context.SaveChanges(); + } + _LessonMaterials = null; + } + } +} diff --git a/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/LessonMaterial.cs b/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/LessonMaterial.cs new file mode 100644 index 0000000..2dac9ac --- /dev/null +++ b/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/LessonMaterial.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 LessonMaterial + { + public int Id { get; set; } + [Required] + public int LessonId { get; set; } + [Required] + public int MaterialId { get; set; } + public virtual Lesson Lesson { get; set; } = new(); + public virtual Material Material { get; set; } = new(); + } +} diff --git a/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/Material.cs b/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/Material.cs new file mode 100644 index 0000000..d6e2dcc --- /dev/null +++ b/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/Material.cs @@ -0,0 +1,71 @@ +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.Text; +using System.Threading.Tasks; + +namespace SchoolAgainStudyDataBaseImplements.Models +{ + public class Material : IMaterial + { + [Required] + public string Title { get; set; } = string.Empty; + [Required] + public string SphereUse { get; set; } = string.Empty; + + 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) + { + if (model == null) + { + return null; + } + return new Material() + { + Id = model.Id, + Title = model.Title, + SphereUse = model.SphereUse + }; + } + + public static Material Create(MaterialViewModel model) + { + return new Material + { + Id = model.Id, + Title = model.Title, + SphereUse = model.SphereUse + }; + } + + public void Update(MaterialBindingModel model) + { + if (model == null) + { + return; + } + Title = model.Title; + SphereUse = model.SphereUse; + } + + public MaterialViewModel GetViewModel => new() + { + Id = Id, + Title = Title, + SphereUse = SphereUse + }; + } +} diff --git a/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/Task.cs b/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/Task.cs new file mode 100644 index 0000000..5b56111 --- /dev/null +++ b/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/Task.cs @@ -0,0 +1,103 @@ +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.Text; +using System.Threading.Tasks; + +namespace SchoolAgainStudyDataBaseImplements.Models +{ + public class Task : ITask + { + [Required] + public string Title { get; set; } = string.Empty; + [Required] + public DateTime DateIssue { get; set; } + [Required] + public DateTime DateDelivery { get; set; } + + public int TeacherId { get; set; } + private Dictionary? _TaskMaterials = null; + [NotMapped] + public Dictionary TaskMaterials { get + { + if (_TaskMaterials == null) + { + _TaskMaterials = Materials + .ToDictionary(recPC => recPC.MaterialId, recPC => (recPC.Material as IMaterial)); + } + return _TaskMaterials; + } + } + + public int Id { get; set; } + [ForeignKey("TaskId")] + public virtual List Materials { get; set; } = new(); + [ForeignKey("TaskId")] + public virtual List Diys { get; set; } = new(); + + public static Task Create(SchoolDataBase context, TaskBindingModel model) + { + return new Task() + { + Id = model.Id, + Title = model.Title, + DateIssue = model.DateIssue, + DateDelivery = model.DateDelivery, + TeacherId = model.TeacherId, + Materials = model.TaskMaterials.Select(x => new TaskMaterial + { + Material = context.Materials.First(y => y.Id == x.Key), + }).ToList() + }; + } + + public void Update(TaskBindingModel model) + { + Title = model.Title; + DateIssue = model.DateIssue; + DateDelivery = model.DateDelivery; + } + + public TaskViewModel GetViewModel => new() + { + Id = Id, + Title = Title, + DateIssue = DateIssue, + DateDelivery = DateDelivery, + TeacherId = TeacherId, + TaskMaterials = TaskMaterials + }; + + public void UpdateMaterials(SchoolDataBase context, TaskBindingModel model) + { + var taskMaterials = context.TaskMaterials.Where(rec => rec.TaskId == model.Id).ToList(); + if (taskMaterials != null && taskMaterials.Count > 0) + { + context.TaskMaterials.RemoveRange(taskMaterials.Where(rec => !model.TaskMaterials.ContainsKey(rec.MaterialId))); + context.SaveChanges(); + + foreach (var updateMaterial in taskMaterials) + { + model.TaskMaterials.Remove(updateMaterial.MaterialId); + } + context.SaveChanges(); + } + var task = context.Tasks.First(x => x.Id == Id); + foreach (var pc in model.TaskMaterials) + { + context.TaskMaterials.Add(new TaskMaterial + { + Task = task, + Material = context.Materials.First(x => x.Id == pc.Key), + }); + context.SaveChanges(); + } + _TaskMaterials = null; + } + } +} diff --git a/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/TaskMaterial.cs b/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/TaskMaterial.cs new file mode 100644 index 0000000..37c513d --- /dev/null +++ b/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/TaskMaterial.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 TaskMaterial + { + public int Id { get; set; } + [Required] + public int TaskId { get; set; } + [Required] + public int MaterialId { get; set; } + public virtual Task Task { get; set; } = new(); + public virtual Material Material { get; set; } = new(); + } +} diff --git a/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/Teacher.cs b/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/Teacher.cs new file mode 100644 index 0000000..a545ba5 --- /dev/null +++ b/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/Teacher.cs @@ -0,0 +1,112 @@ +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; + +namespace SchoolAgainStudyDataBaseImplements.Models +{ + public class Teacher : ITeacher + { + [Required] + public string Name { get; set; } = string.Empty; + [Required] + public string Post { get; set; } = string.Empty; + [Required] + public string Phone { get; set; } = string.Empty; + [Required] + 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(); + [ForeignKey("TeacherId")] + public virtual List Tasks { get; set; } = new(); + [ForeignKey("TeacherId")] + public virtual List Lessons { get; set; } = new(); + public static Teacher Create(SchoolDataBase context, TeacherBindingModel model) + { + return new Teacher() + { + Id = model.Id, + Name = model.Name, + Post = model.Post, + 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() + }; + } + + public void Update(TeacherBindingModel model) + { + Name = model.Name; + Post = model.Post; + Phone = model.Phone; + Login = model.Login; + Password = model.Password; + } + + public TeacherViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + Post = Post, + 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 new file mode 100644 index 0000000..3435c05 --- /dev/null +++ b/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/TeacherMaterial.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 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/SchoolAgainStudyDataBaseImplements.csproj b/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/SchoolAgainStudyDataBaseImplements.csproj new file mode 100644 index 0000000..132c02c --- /dev/null +++ b/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/SchoolAgainStudyDataBaseImplements.csproj @@ -0,0 +1,9 @@ + + + + net6.0 + enable + enable + + + diff --git a/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/SchoolDataBase.cs b/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/SchoolDataBase.cs new file mode 100644 index 0000000..8d7c89b --- /dev/null +++ b/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/SchoolDataBase.cs @@ -0,0 +1,48 @@ +using Microsoft.EntityFrameworkCore; +using SchoolAgainStudyDataBaseImplements.Models; +using SchoolAgainStudyDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Runtime.ConstrainedExecution; +using System.Text; +using Task = SchoolAgainStudyDataBaseImplements.Models.Task; + +namespace SchoolAgainStudyDataBaseImplements +{ + public class SchoolDataBase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseNpgsql("Server=PostgreSQL;Host=localhost;Port=5432;Database=SchoolAgainStudyDataBase;Username=postgres;Password=postgres"); + } + 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; } + public virtual DbSet DiyInterests { set; get; } + public virtual DbSet Products { set; get; } + public virtual DbSet ProductInterests { set; get; } + + public virtual DbSet Teachers { set; get; } + + public virtual DbSet TeacherMaterials { set; get; } + + public virtual DbSet Materials { set; get; } + + public virtual DbSet Lessons { set; get; } + public virtual DbSet LessonMaterials { set; get; } + public virtual DbSet Tasks { set; get; } + public virtual DbSet TaskMaterials { set; get; } + + } +} \ No newline at end of file