From 78516eef253130c0d65068e3edfece1949a0d6ba Mon Sep 17 00:00:00 2001 From: Kirill <117719052+KirillFirsof@users.noreply.github.com> Date: Tue, 30 Apr 2024 23:50:39 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9E=D1=82=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D1=8E=20=D0=BF=D0=BE=D0=BA=D0=B0=20=D1=82=D0=BE=20=D0=A7=D1=82?= =?UTF-8?q?=D0=BE=20=D1=83=D1=81=D0=BF=D0=B5=D0=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implements/LessonStorage.cs | 82 ++++++++++++++++++ .../Implements/UserStorage.cs | 84 +++++++++++++++++++ ... 20240430191557_InitialCreate.Designer.cs} | 27 ++---- ...ate.cs => 20240430191557_InitialCreate.cs} | 25 ++---- .../Migrations/SchoolDatabaseModelSnapshot.cs | 25 ++---- .../SchoolDatabaseImplement/Models/Lesson.cs | 2 +- School/SchoolDatabaseImplement/Models/User.cs | 2 +- .../SchoolDatabaseImplement.csproj | 4 - 8 files changed, 188 insertions(+), 63 deletions(-) create mode 100644 School/SchoolDatabaseImplement/Implements/LessonStorage.cs create mode 100644 School/SchoolDatabaseImplement/Implements/UserStorage.cs rename School/SchoolDatabaseImplement/Migrations/{20240430184208_InitialCreate.Designer.cs => 20240430191557_InitialCreate.Designer.cs} (90%) rename School/SchoolDatabaseImplement/Migrations/{20240430184208_InitialCreate.cs => 20240430191557_InitialCreate.cs} (87%) diff --git a/School/SchoolDatabaseImplement/Implements/LessonStorage.cs b/School/SchoolDatabaseImplement/Implements/LessonStorage.cs new file mode 100644 index 0000000..6babfb1 --- /dev/null +++ b/School/SchoolDatabaseImplement/Implements/LessonStorage.cs @@ -0,0 +1,82 @@ +using Microsoft.EntityFrameworkCore; +using SchoolContracts.BindingModels; +using SchoolContracts.SearchModels; +using SchoolContracts.StoragesContracts; +using SchoolContracts.ViewModels; +using SchoolDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SchoolDatabaseImplement.Implements +{ + public class LessonStorage : ILessonStorage + { + public List GetFullList() + { + using var context = new SchoolDatabase(); + return context.Lessons.Include(x => x.Achievements) + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(LessonSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + using var context = new SchoolDatabase(); + return context.Lessons.Include(x => x.Achievements) + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + public LessonViewModel? GetElement(LessonSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new SchoolDatabase(); + return context.Lessons.Include(x => x.Achievements).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + } + public LessonViewModel? Insert(LessonBindingModel model) + { + var newLesson = Lesson.Create(model); + if (newLesson == null) + { + return null; + } + using var context = new SchoolDatabase(); + context.Lessons.Add(newLesson); + context.SaveChanges(); + return newLesson.GetViewModel; + } + public LessonViewModel? Update(LessonBindingModel model) + { + using var context = new SchoolDatabase(); + var Lesson = context.Lessons.Include(x => x.Achievements).FirstOrDefault(x => x.Id == model.Id); + if (Lesson == null) + { + return null; + } + Lesson.Update(model); + context.SaveChanges(); + return Lesson.GetViewModel; + } + public LessonViewModel? Delete(LessonBindingModel model) + { + using var context = new SchoolDatabase(); + var element = context.Lessons.Include(x => x.Achievements).FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Lessons.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/School/SchoolDatabaseImplement/Implements/UserStorage.cs b/School/SchoolDatabaseImplement/Implements/UserStorage.cs new file mode 100644 index 0000000..b14ca75 --- /dev/null +++ b/School/SchoolDatabaseImplement/Implements/UserStorage.cs @@ -0,0 +1,84 @@ +using Microsoft.EntityFrameworkCore; +using SchoolContracts.BindingModels; +using SchoolContracts.SearchModels; +using SchoolContracts.StoragesContracts; +using SchoolContracts.ViewModels; +using SchoolDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SchoolDatabaseImplement.Implements +{ + public class UserStorage + { + public List GetFullList() + { + using var context = new SchoolDatabase(); + return context.Users.Include(x => x.Lessons) + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(UserSearchModel model) + { + if (string.IsNullOrEmpty(model.Name)) + { + return new(); + } + using var context = new SchoolDatabase(); + return context.Users.Include(x => x.Lessons) + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + public UserViewModel? GetElement(UserSearchModel model) + { + if (string.IsNullOrEmpty(model.Name) && + !model.Id.HasValue) + { + return null; + } + using var context = new SchoolDatabase(); + return context.Users.Include(x => x.Lessons).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + } + public UserViewModel? Insert(UserBindingModel model) + { + var newUser = User.Create(model); + if (newUser == null) + { + return null; + } + using var context = new SchoolDatabase(); + context.Users.Add(newUser); + context.SaveChanges(); + return newUser.GetViewModel; + } + public UserViewModel? Update(UserBindingModel model) + { + using var context = new SchoolDatabase(); + var user = context.Users.Include(x => x.Lessons).FirstOrDefault(x => x.Id == model.Id); + if (user == null) + { + return null; + } + user.Update(model); + context.SaveChanges(); + return user.GetViewModel; + } + public UserViewModel? Delete(UserBindingModel model) + { + using var context = new SchoolDatabase(); + var element = context.Users.Include(x => x.Lessons).FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Users.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } + +} diff --git a/School/SchoolDatabaseImplement/Migrations/20240430184208_InitialCreate.Designer.cs b/School/SchoolDatabaseImplement/Migrations/20240430191557_InitialCreate.Designer.cs similarity index 90% rename from School/SchoolDatabaseImplement/Migrations/20240430184208_InitialCreate.Designer.cs rename to School/SchoolDatabaseImplement/Migrations/20240430191557_InitialCreate.Designer.cs index f552fa0..edbcfe4 100644 --- a/School/SchoolDatabaseImplement/Migrations/20240430184208_InitialCreate.Designer.cs +++ b/School/SchoolDatabaseImplement/Migrations/20240430191557_InitialCreate.Designer.cs @@ -12,7 +12,7 @@ using SchoolDatabaseImplement; namespace SchoolDatabaseImplement.Migrations { [DbContext(typeof(SchoolDatabase))] - [Migration("20240430184208_InitialCreate")] + [Migration("20240430191557_InitialCreate")] partial class InitialCreate { /// @@ -33,9 +33,6 @@ namespace SchoolDatabaseImplement.Migrations SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); - b.Property("AchievementId") - .HasColumnType("int"); - b.Property("Description") .IsRequired() .HasColumnType("nvarchar(max)"); @@ -52,7 +49,7 @@ namespace SchoolDatabaseImplement.Migrations b.HasKey("Id"); - b.HasIndex("AchievementId"); + b.HasIndex("LessonId"); b.ToTable("Achievements"); }); @@ -156,13 +153,8 @@ namespace SchoolDatabaseImplement.Migrations .IsRequired() .HasColumnType("nvarchar(max)"); - b.Property("UserId") - .HasColumnType("int"); - b.HasKey("Id"); - b.HasIndex("UserId"); - b.ToTable("Users"); }); @@ -170,7 +162,9 @@ namespace SchoolDatabaseImplement.Migrations { b.HasOne("SchoolDatabaseImplement.Models.Lesson", "Lesson") .WithMany("Achievements") - .HasForeignKey("AchievementId"); + .HasForeignKey("LessonId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); b.Navigation("Lesson"); }); @@ -197,7 +191,7 @@ namespace SchoolDatabaseImplement.Migrations modelBuilder.Entity("SchoolDatabaseImplement.Models.Lesson", b => { b.HasOne("SchoolDatabaseImplement.Models.User", "User") - .WithMany() + .WithMany("Lessons") .HasForeignKey("UserId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); @@ -205,13 +199,6 @@ namespace SchoolDatabaseImplement.Migrations 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"); @@ -226,7 +213,7 @@ namespace SchoolDatabaseImplement.Migrations modelBuilder.Entity("SchoolDatabaseImplement.Models.User", b => { - b.Navigation("Users"); + b.Navigation("Lessons"); }); #pragma warning restore 612, 618 } diff --git a/School/SchoolDatabaseImplement/Migrations/20240430184208_InitialCreate.cs b/School/SchoolDatabaseImplement/Migrations/20240430191557_InitialCreate.cs similarity index 87% rename from School/SchoolDatabaseImplement/Migrations/20240430184208_InitialCreate.cs rename to School/SchoolDatabaseImplement/Migrations/20240430191557_InitialCreate.cs index a350d37..8c634fa 100644 --- a/School/SchoolDatabaseImplement/Migrations/20240430184208_InitialCreate.cs +++ b/School/SchoolDatabaseImplement/Migrations/20240430191557_InitialCreate.cs @@ -36,17 +36,11 @@ namespace SchoolDatabaseImplement.Migrations 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) + Password = table.Column(type: "nvarchar(max)", nullable: false) }, 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( @@ -77,7 +71,6 @@ namespace SchoolDatabaseImplement.Migrations 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) @@ -86,10 +79,11 @@ namespace SchoolDatabaseImplement.Migrations { table.PrimaryKey("PK_Achievements", x => x.Id); table.ForeignKey( - name: "FK_Achievements_Lessons_AchievementId", - column: x => x.AchievementId, + name: "FK_Achievements_Lessons_LessonId", + column: x => x.LessonId, principalTable: "Lessons", - principalColumn: "Id"); + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); }); migrationBuilder.CreateTable( @@ -119,9 +113,9 @@ namespace SchoolDatabaseImplement.Migrations }); migrationBuilder.CreateIndex( - name: "IX_Achievements_AchievementId", + name: "IX_Achievements_LessonId", table: "Achievements", - column: "AchievementId"); + column: "LessonId"); migrationBuilder.CreateIndex( name: "IX_InterestLessons_InterestId", @@ -137,11 +131,6 @@ namespace SchoolDatabaseImplement.Migrations name: "IX_Lessons_UserId", table: "Lessons", column: "UserId"); - - migrationBuilder.CreateIndex( - name: "IX_Users_UserId", - table: "Users", - column: "UserId"); } /// diff --git a/School/SchoolDatabaseImplement/Migrations/SchoolDatabaseModelSnapshot.cs b/School/SchoolDatabaseImplement/Migrations/SchoolDatabaseModelSnapshot.cs index 6bd211e..d8cc9cc 100644 --- a/School/SchoolDatabaseImplement/Migrations/SchoolDatabaseModelSnapshot.cs +++ b/School/SchoolDatabaseImplement/Migrations/SchoolDatabaseModelSnapshot.cs @@ -30,9 +30,6 @@ namespace SchoolDatabaseImplement.Migrations SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); - b.Property("AchievementId") - .HasColumnType("int"); - b.Property("Description") .IsRequired() .HasColumnType("nvarchar(max)"); @@ -49,7 +46,7 @@ namespace SchoolDatabaseImplement.Migrations b.HasKey("Id"); - b.HasIndex("AchievementId"); + b.HasIndex("LessonId"); b.ToTable("Achievements"); }); @@ -153,13 +150,8 @@ namespace SchoolDatabaseImplement.Migrations .IsRequired() .HasColumnType("nvarchar(max)"); - b.Property("UserId") - .HasColumnType("int"); - b.HasKey("Id"); - b.HasIndex("UserId"); - b.ToTable("Users"); }); @@ -167,7 +159,9 @@ namespace SchoolDatabaseImplement.Migrations { b.HasOne("SchoolDatabaseImplement.Models.Lesson", "Lesson") .WithMany("Achievements") - .HasForeignKey("AchievementId"); + .HasForeignKey("LessonId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); b.Navigation("Lesson"); }); @@ -194,7 +188,7 @@ namespace SchoolDatabaseImplement.Migrations modelBuilder.Entity("SchoolDatabaseImplement.Models.Lesson", b => { b.HasOne("SchoolDatabaseImplement.Models.User", "User") - .WithMany() + .WithMany("Lessons") .HasForeignKey("UserId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); @@ -202,13 +196,6 @@ namespace SchoolDatabaseImplement.Migrations 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"); @@ -223,7 +210,7 @@ namespace SchoolDatabaseImplement.Migrations modelBuilder.Entity("SchoolDatabaseImplement.Models.User", b => { - b.Navigation("Users"); + b.Navigation("Lessons"); }); #pragma warning restore 612, 618 } diff --git a/School/SchoolDatabaseImplement/Models/Lesson.cs b/School/SchoolDatabaseImplement/Models/Lesson.cs index d5c3c28..c59ce15 100644 --- a/School/SchoolDatabaseImplement/Models/Lesson.cs +++ b/School/SchoolDatabaseImplement/Models/Lesson.cs @@ -24,7 +24,7 @@ namespace SchoolDatabaseImplement.Models public int UserId { get; private set; } public virtual User? User { get; private set; } - [ForeignKey("AchievementId")] + [ForeignKey("LessonId")] public virtual List Achievements { get; set; } = new(); public static Lesson? Create(LessonBindingModel model) { diff --git a/School/SchoolDatabaseImplement/Models/User.cs b/School/SchoolDatabaseImplement/Models/User.cs index 080cc4e..930bc38 100644 --- a/School/SchoolDatabaseImplement/Models/User.cs +++ b/School/SchoolDatabaseImplement/Models/User.cs @@ -26,7 +26,7 @@ namespace SchoolDatabaseImplement.Models public string Password { get; private set; } = string.Empty; [ForeignKey("UserId")] - public virtual List Users { get; set; } = new(); + public virtual List Lessons { get; set; } = new(); public static User? Create(UserBindingModel model) { if (model == null) diff --git a/School/SchoolDatabaseImplement/SchoolDatabaseImplement.csproj b/School/SchoolDatabaseImplement/SchoolDatabaseImplement.csproj index 18d83ea..1bcedbf 100644 --- a/School/SchoolDatabaseImplement/SchoolDatabaseImplement.csproj +++ b/School/SchoolDatabaseImplement/SchoolDatabaseImplement.csproj @@ -6,10 +6,6 @@ enable - - - -