From 5cec4e59aca9d595bbabe17cc3550306be8b6c2b Mon Sep 17 00:00:00 2001 From: Kirill <117719052+KirillFirsof@users.noreply.github.com> Date: Tue, 30 Apr 2024 22:59:25 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A3=D1=81=D0=BF=D0=B5=D1=88=D0=BD=D0=BE=20?= =?UTF-8?q?=D1=81=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B0=20=D0=BC=D0=B8=D0=B3?= =?UTF-8?q?=D1=80=D0=B0=D1=86=D0=B8=D1=8F=20=D0=B8=20=D0=BF=D0=BE=D0=B4?= =?UTF-8?q?=D0=BA=D0=BB=D1=8E=D1=87=D0=B5=D0=BD=D0=B0=20=D0=B1=D0=B0=D0=B7?= =?UTF-8?q?=D0=B0=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 --- School/School.sln | 6 +- School/SchoolApp/SchoolApp.csproj | 9 +- .../BindingModels/InterestBindingModel.cs | 2 +- .../ViewModels/InterestViewModel.cs | 2 +- .../20240430184208_InitialCreate.Designer.cs | 234 ++++++++++++++++++ .../20240430184208_InitialCreate.cs | 166 +++++++++++++ .../Migrations/SchoolDatabaseModelSnapshot.cs | 231 +++++++++++++++++ .../Models/Interest.cs | 67 +++-- .../SchoolDatabaseImplement/Models/Lesson.cs | 3 + School/SchoolDatabaseImplement/Models/User.cs | 3 + .../SchoolDatabaseImplement/SchoolDatabase.cs | 30 +++ .../Models/IInterestModel.cs | 2 +- 12 files changed, 731 insertions(+), 24 deletions(-) create mode 100644 School/SchoolDatabaseImplement/Migrations/20240430184208_InitialCreate.Designer.cs create mode 100644 School/SchoolDatabaseImplement/Migrations/20240430184208_InitialCreate.cs create mode 100644 School/SchoolDatabaseImplement/Migrations/SchoolDatabaseModelSnapshot.cs create mode 100644 School/SchoolDatabaseImplement/SchoolDatabase.cs diff --git a/School/School.sln b/School/School.sln index dc165ee..3f22cfb 100644 --- a/School/School.sln +++ b/School/School.sln @@ -7,11 +7,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SchoolApp", "SchoolApp\Scho EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SchoolDataModels", "SchoolsDataModels\SchoolDataModels.csproj", "{ADBB4992-BE2F-4B10-83CC-335E5B92EA35}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SchoolContracts", "SchoolContracts\SchoolContracts.csproj", "{D58B0AFE-E28D-4583-B9F1-C0EDF443243F}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SchoolContracts", "SchoolContracts\SchoolContracts.csproj", "{D58B0AFE-E28D-4583-B9F1-C0EDF443243F}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SchoolBusinessLogic", "SchoolBusinessLogic\SchoolBusinessLogic.csproj", "{E78D42A3-7FA0-4E3A-B0C8-AEC70B3EE331}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SchoolBusinessLogic", "SchoolBusinessLogic\SchoolBusinessLogic.csproj", "{E78D42A3-7FA0-4E3A-B0C8-AEC70B3EE331}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SchoolDatabaseImplement", "SchoolDatabaseImplement\SchoolDatabaseImplement.csproj", "{8C8F56D4-E267-498F-AFCD-06701962D09F}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SchoolDatabaseImplement", "SchoolDatabaseImplement\SchoolDatabaseImplement.csproj", "{8C8F56D4-E267-498F-AFCD-06701962D09F}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/School/SchoolApp/SchoolApp.csproj b/School/SchoolApp/SchoolApp.csproj index 1d1d459..57ea347 100644 --- a/School/SchoolApp/SchoolApp.csproj +++ b/School/SchoolApp/SchoolApp.csproj @@ -1,4 +1,4 @@ - + net6.0 @@ -13,4 +13,11 @@ + + + + + + + diff --git a/School/SchoolContracts/BindingModels/InterestBindingModel.cs b/School/SchoolContracts/BindingModels/InterestBindingModel.cs index 1b7adc3..8826676 100644 --- a/School/SchoolContracts/BindingModels/InterestBindingModel.cs +++ b/School/SchoolContracts/BindingModels/InterestBindingModel.cs @@ -13,7 +13,7 @@ namespace SchoolContracts.BindingModels public string Name { get; set; } = string.Empty; public string Direction { get; set; } = string.Empty; public string Description { get; set; } = string.Empty; - public Dictionary InterestLessons + public Dictionary InterestLessons { get; set; diff --git a/School/SchoolContracts/ViewModels/InterestViewModel.cs b/School/SchoolContracts/ViewModels/InterestViewModel.cs index 5ee4e08..cc08468 100644 --- a/School/SchoolContracts/ViewModels/InterestViewModel.cs +++ b/School/SchoolContracts/ViewModels/InterestViewModel.cs @@ -17,7 +17,7 @@ namespace SchoolContracts.ViewModels public string Direction { get; set; } = string.Empty; [DisplayName("Описание интереса")] public string Description { get; set; } = string.Empty; - public Dictionary InterestLessons + public Dictionary InterestLessons { get; set; diff --git a/School/SchoolDatabaseImplement/Migrations/20240430184208_InitialCreate.Designer.cs b/School/SchoolDatabaseImplement/Migrations/20240430184208_InitialCreate.Designer.cs new file mode 100644 index 0000000..f552fa0 --- /dev/null +++ b/School/SchoolDatabaseImplement/Migrations/20240430184208_InitialCreate.Designer.cs @@ -0,0 +1,234 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using SchoolDatabaseImplement; + +#nullable disable + +namespace SchoolDatabaseImplement.Migrations +{ + [DbContext(typeof(SchoolDatabase))] + [Migration("20240430184208_InitialCreate")] + partial class InitialCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.0") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("SchoolDatabaseImplement.Models.Achievement", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("AchievementId") + .HasColumnType("int"); + + b.Property("Description") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("LessonId") + .HasColumnType("int"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("ReceiptDate") + .HasColumnType("datetime2"); + + b.HasKey("Id"); + + b.HasIndex("AchievementId"); + + b.ToTable("Achievements"); + }); + + modelBuilder.Entity("SchoolDatabaseImplement.Models.Interest", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Description") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Direction") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Interests"); + }); + + modelBuilder.Entity("SchoolDatabaseImplement.Models.InterestLesson", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("InterestId") + .HasColumnType("int"); + + b.Property("LessonId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("InterestId"); + + b.HasIndex("LessonId"); + + b.ToTable("InterestLessons"); + }); + + modelBuilder.Entity("SchoolDatabaseImplement.Models.Lesson", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("TimeEnd") + .HasColumnType("datetime2"); + + b.Property("TimeStart") + .HasColumnType("datetime2"); + + b.Property("UserId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("Lessons"); + }); + + modelBuilder.Entity("SchoolDatabaseImplement.Models.User", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("BirthDate") + .HasColumnType("datetime2"); + + b.Property("Mail") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Password") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("PhoneNumber") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("UserId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("Users"); + }); + + modelBuilder.Entity("SchoolDatabaseImplement.Models.Achievement", b => + { + b.HasOne("SchoolDatabaseImplement.Models.Lesson", "Lesson") + .WithMany("Achievements") + .HasForeignKey("AchievementId"); + + b.Navigation("Lesson"); + }); + + modelBuilder.Entity("SchoolDatabaseImplement.Models.InterestLesson", b => + { + b.HasOne("SchoolDatabaseImplement.Models.Interest", "Interest") + .WithMany("Lessons") + .HasForeignKey("InterestId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("SchoolDatabaseImplement.Models.Lesson", "Lesson") + .WithMany("InterestLessons") + .HasForeignKey("LessonId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Interest"); + + b.Navigation("Lesson"); + }); + + modelBuilder.Entity("SchoolDatabaseImplement.Models.Lesson", b => + { + b.HasOne("SchoolDatabaseImplement.Models.User", "User") + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("User"); + }); + + modelBuilder.Entity("SchoolDatabaseImplement.Models.User", b => + { + b.HasOne("SchoolDatabaseImplement.Models.User", null) + .WithMany("Users") + .HasForeignKey("UserId"); + }); + + modelBuilder.Entity("SchoolDatabaseImplement.Models.Interest", b => + { + b.Navigation("Lessons"); + }); + + modelBuilder.Entity("SchoolDatabaseImplement.Models.Lesson", b => + { + b.Navigation("Achievements"); + + b.Navigation("InterestLessons"); + }); + + modelBuilder.Entity("SchoolDatabaseImplement.Models.User", b => + { + b.Navigation("Users"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/School/SchoolDatabaseImplement/Migrations/20240430184208_InitialCreate.cs b/School/SchoolDatabaseImplement/Migrations/20240430184208_InitialCreate.cs new file mode 100644 index 0000000..a350d37 --- /dev/null +++ b/School/SchoolDatabaseImplement/Migrations/20240430184208_InitialCreate.cs @@ -0,0 +1,166 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace SchoolDatabaseImplement.Migrations +{ + /// + public partial class InitialCreate : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Interests", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + Name = table.Column(type: "nvarchar(max)", nullable: false), + Direction = table.Column(type: "nvarchar(max)", nullable: false), + Description = table.Column(type: "nvarchar(max)", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Interests", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Users", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + Name = table.Column(type: "nvarchar(max)", nullable: false), + BirthDate = table.Column(type: "datetime2", nullable: false), + Mail = table.Column(type: "nvarchar(max)", nullable: false), + PhoneNumber = table.Column(type: "nvarchar(max)", nullable: false), + Password = table.Column(type: "nvarchar(max)", nullable: false), + UserId = table.Column(type: "int", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Users", x => x.Id); + table.ForeignKey( + name: "FK_Users_Users_UserId", + column: x => x.UserId, + principalTable: "Users", + principalColumn: "Id"); + }); + + migrationBuilder.CreateTable( + name: "Lessons", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + TimeStart = table.Column(type: "datetime2", nullable: false), + TimeEnd = table.Column(type: "datetime2", nullable: false), + UserId = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Lessons", x => x.Id); + table.ForeignKey( + name: "FK_Lessons_Users_UserId", + column: x => x.UserId, + principalTable: "Users", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "Achievements", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + LessonId = table.Column(type: "int", nullable: false), + AchievementId = table.Column(type: "int", nullable: true), + Name = table.Column(type: "nvarchar(max)", nullable: false), + Description = table.Column(type: "nvarchar(max)", nullable: false), + ReceiptDate = table.Column(type: "datetime2", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Achievements", x => x.Id); + table.ForeignKey( + name: "FK_Achievements_Lessons_AchievementId", + column: x => x.AchievementId, + principalTable: "Lessons", + principalColumn: "Id"); + }); + + migrationBuilder.CreateTable( + name: "InterestLessons", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + InterestId = table.Column(type: "int", nullable: false), + LessonId = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_InterestLessons", x => x.Id); + table.ForeignKey( + name: "FK_InterestLessons_Interests_InterestId", + column: x => x.InterestId, + principalTable: "Interests", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_InterestLessons_Lessons_LessonId", + column: x => x.LessonId, + principalTable: "Lessons", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_Achievements_AchievementId", + table: "Achievements", + column: "AchievementId"); + + migrationBuilder.CreateIndex( + name: "IX_InterestLessons_InterestId", + table: "InterestLessons", + column: "InterestId"); + + migrationBuilder.CreateIndex( + name: "IX_InterestLessons_LessonId", + table: "InterestLessons", + column: "LessonId"); + + migrationBuilder.CreateIndex( + name: "IX_Lessons_UserId", + table: "Lessons", + column: "UserId"); + + migrationBuilder.CreateIndex( + name: "IX_Users_UserId", + table: "Users", + column: "UserId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Achievements"); + + migrationBuilder.DropTable( + name: "InterestLessons"); + + migrationBuilder.DropTable( + name: "Interests"); + + migrationBuilder.DropTable( + name: "Lessons"); + + migrationBuilder.DropTable( + name: "Users"); + } + } +} diff --git a/School/SchoolDatabaseImplement/Migrations/SchoolDatabaseModelSnapshot.cs b/School/SchoolDatabaseImplement/Migrations/SchoolDatabaseModelSnapshot.cs new file mode 100644 index 0000000..6bd211e --- /dev/null +++ b/School/SchoolDatabaseImplement/Migrations/SchoolDatabaseModelSnapshot.cs @@ -0,0 +1,231 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using SchoolDatabaseImplement; + +#nullable disable + +namespace SchoolDatabaseImplement.Migrations +{ + [DbContext(typeof(SchoolDatabase))] + partial class SchoolDatabaseModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.0") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("SchoolDatabaseImplement.Models.Achievement", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("AchievementId") + .HasColumnType("int"); + + b.Property("Description") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("LessonId") + .HasColumnType("int"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("ReceiptDate") + .HasColumnType("datetime2"); + + b.HasKey("Id"); + + b.HasIndex("AchievementId"); + + b.ToTable("Achievements"); + }); + + modelBuilder.Entity("SchoolDatabaseImplement.Models.Interest", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Description") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Direction") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Interests"); + }); + + modelBuilder.Entity("SchoolDatabaseImplement.Models.InterestLesson", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("InterestId") + .HasColumnType("int"); + + b.Property("LessonId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("InterestId"); + + b.HasIndex("LessonId"); + + b.ToTable("InterestLessons"); + }); + + modelBuilder.Entity("SchoolDatabaseImplement.Models.Lesson", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("TimeEnd") + .HasColumnType("datetime2"); + + b.Property("TimeStart") + .HasColumnType("datetime2"); + + b.Property("UserId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("Lessons"); + }); + + modelBuilder.Entity("SchoolDatabaseImplement.Models.User", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("BirthDate") + .HasColumnType("datetime2"); + + b.Property("Mail") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Password") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("PhoneNumber") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("UserId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("Users"); + }); + + modelBuilder.Entity("SchoolDatabaseImplement.Models.Achievement", b => + { + b.HasOne("SchoolDatabaseImplement.Models.Lesson", "Lesson") + .WithMany("Achievements") + .HasForeignKey("AchievementId"); + + b.Navigation("Lesson"); + }); + + modelBuilder.Entity("SchoolDatabaseImplement.Models.InterestLesson", b => + { + b.HasOne("SchoolDatabaseImplement.Models.Interest", "Interest") + .WithMany("Lessons") + .HasForeignKey("InterestId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("SchoolDatabaseImplement.Models.Lesson", "Lesson") + .WithMany("InterestLessons") + .HasForeignKey("LessonId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Interest"); + + b.Navigation("Lesson"); + }); + + modelBuilder.Entity("SchoolDatabaseImplement.Models.Lesson", b => + { + b.HasOne("SchoolDatabaseImplement.Models.User", "User") + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("User"); + }); + + modelBuilder.Entity("SchoolDatabaseImplement.Models.User", b => + { + b.HasOne("SchoolDatabaseImplement.Models.User", null) + .WithMany("Users") + .HasForeignKey("UserId"); + }); + + modelBuilder.Entity("SchoolDatabaseImplement.Models.Interest", b => + { + b.Navigation("Lessons"); + }); + + modelBuilder.Entity("SchoolDatabaseImplement.Models.Lesson", b => + { + b.Navigation("Achievements"); + + b.Navigation("InterestLessons"); + }); + + modelBuilder.Entity("SchoolDatabaseImplement.Models.User", b => + { + b.Navigation("Users"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/School/SchoolDatabaseImplement/Models/Interest.cs b/School/SchoolDatabaseImplement/Models/Interest.cs index fe0679c..adf6a3d 100644 --- a/School/SchoolDatabaseImplement/Models/Interest.cs +++ b/School/SchoolDatabaseImplement/Models/Interest.cs @@ -13,39 +13,45 @@ namespace SchoolDatabaseImplement.Models { public class Interest : IInterestModel { - public int Id { get; private set; } + public int Id { get; set; } [Required] public string Name { get; private set; } = string.Empty; [Required] public string Direction { get; private set; } = string.Empty; public string Description { get; private set; } = string.Empty; - [ForeignKey("InterestId")] - public virtual List InterestLessons { get; set; } = new(); [Required] - - - Dictionary IInterestModel.InterestLessons => throw new NotImplementedException(); - public static Interest? Create(InterestBindingModel model) + private Dictionary? _InterestLessons = null; + [NotMapped] + public Dictionary InterestLessons { - if (model == null) + get { - return null; + if (_InterestLessons == null) + { + _InterestLessons = Lessons.ToDictionary(recPC => recPC.LessonId, recPC => recPC.Lesson as ILessonModel); + } + return _InterestLessons; } + } + [ForeignKey("InterestId")] + public virtual List Lessons { get; set; } = new(); + public static Interest Create(SchoolDatabase context, InterestBindingModel model) + { return new Interest() { Id = model.Id, Name = model.Name, Direction = model.Direction, - Description = model.Description + Description = model.Description, + Lessons = model.InterestLessons.Select(x => new InterestLesson + { + Lesson = context.Lessons.First(y => y.Id == x.Key) + }).ToList() }; } public void Update(InterestBindingModel model) { - if (model == null) - { - return; - } Name = model.Name; Direction = model.Direction; Description = model.Description; @@ -55,9 +61,36 @@ namespace SchoolDatabaseImplement.Models Id = Id, Name = Name, Direction = Direction, - Description = Description + Description = Description, + InterestLessons = InterestLessons }; - - + public void UpdateLessons(SchoolDatabase context, + InterestBindingModel model) + { + var InterestLessons = context.InterestLessons.Where(rec => rec.InterestId == model.Id).ToList(); + if (InterestLessons != null) + { // удалили те, которых нет в модели + context.InterestLessons.RemoveRange(InterestLessons.Where(rec + => !model.InterestLessons.ContainsKey(rec.LessonId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateLesson in InterestLessons) + { + model.InterestLessons.Remove(updateLesson.LessonId); + } + context.SaveChanges(); + } + var Interest = context.Interests.First(x => x.Id == Id); + foreach (var pc in model.InterestLessons) + { + context.InterestLessons.Add(new InterestLesson + { + Interest = Interest, + Lesson = context.Lessons.First(x => x.Id == pc.Key), + }); + context.SaveChanges(); + } + _InterestLessons = null; + } } } diff --git a/School/SchoolDatabaseImplement/Models/Lesson.cs b/School/SchoolDatabaseImplement/Models/Lesson.cs index e697536..d5c3c28 100644 --- a/School/SchoolDatabaseImplement/Models/Lesson.cs +++ b/School/SchoolDatabaseImplement/Models/Lesson.cs @@ -23,6 +23,9 @@ namespace SchoolDatabaseImplement.Models [Required] public int UserId { get; private set; } public virtual User? User { get; private set; } + + [ForeignKey("AchievementId")] + public virtual List Achievements { get; set; } = new(); public static Lesson? Create(LessonBindingModel model) { if (model == null) diff --git a/School/SchoolDatabaseImplement/Models/User.cs b/School/SchoolDatabaseImplement/Models/User.cs index c8522b9..080cc4e 100644 --- a/School/SchoolDatabaseImplement/Models/User.cs +++ b/School/SchoolDatabaseImplement/Models/User.cs @@ -24,6 +24,9 @@ namespace SchoolDatabaseImplement.Models public string PhoneNumber { get; private set; } = string.Empty; public string Password { get; private set; } = string.Empty; + + [ForeignKey("UserId")] + public virtual List Users { get; set; } = new(); public static User? Create(UserBindingModel model) { if (model == null) diff --git a/School/SchoolDatabaseImplement/SchoolDatabase.cs b/School/SchoolDatabaseImplement/SchoolDatabase.cs new file mode 100644 index 0000000..feaa016 --- /dev/null +++ b/School/SchoolDatabaseImplement/SchoolDatabase.cs @@ -0,0 +1,30 @@ +using Microsoft.EntityFrameworkCore; +using SchoolDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SchoolDatabaseImplement +{ + public class SchoolDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder + optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"Data Source=firsovpk;Initial Catalog=SchoolDataBase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True" + ); + } + base.OnConfiguring(optionsBuilder); + } + public virtual DbSet Users { set; get; } + public virtual DbSet Lessons { set; get; } + public virtual DbSet InterestLessons { set; get; } + public virtual DbSet Interests { set; get; } + public virtual DbSet Achievements { set; get; } + } + +} diff --git a/School/SchoolsDataModels/Models/IInterestModel.cs b/School/SchoolsDataModels/Models/IInterestModel.cs index 7c2acd5..09080f5 100644 --- a/School/SchoolsDataModels/Models/IInterestModel.cs +++ b/School/SchoolsDataModels/Models/IInterestModel.cs @@ -14,6 +14,6 @@ namespace SchoolDataModels.Models string Description { get; } - Dictionary InterestLessons { get; } + Dictionary InterestLessons { get; } } }