From bc6d955ad47a177e4fe3a7fef17db1ffec25a7fb Mon Sep 17 00:00:00 2001 From: Danil Markov Date: Thu, 18 May 2023 14:36:57 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B2=D1=81=D0=B5=20crud=20=D1=81=D0=B4=D0=B5?= =?UTF-8?q?=D0=BB=D0=B0=D0=BD=D1=8B.=20=D0=9F=D1=80=D0=B8=D0=B2=D1=8F?= =?UTF-8?q?=D0=B7=D0=BA=D0=B0=20=D0=B8=D0=B7=20=D0=BD=D0=B5=D1=81=D0=BA?= =?UTF-8?q?=D0=BE=D0=BB=D1=8C=D0=BA=D0=B8=D1=85=20=D0=B7=D0=B0=D0=BF=D0=B8?= =?UTF-8?q?=D1=81=D0=B5=D0=B9=20=D0=B3=D1=80=D1=83=D0=BF=D0=BF=20=D0=B4?= =?UTF-8?q?=D0=BB=D1=8F=20=D0=BF=D1=80=D0=B8=D0=BA=D0=B0=D0=B7=D0=B0=20?= =?UTF-8?q?=D1=81=D0=B4=D0=B5=D0=BB=D0=B0=D0=BD=D0=B0.=20=D0=9F=D1=80?= =?UTF-8?q?=D0=B8=D0=B2=D1=8F=D0=B7=D0=BA=D0=B0=20=D0=B8=D0=B7=20=D0=BE?= =?UTF-8?q?=D0=B4=D0=BD=D0=BE=D0=B9=20=D0=B7=D0=B0=D0=BF=D0=B8=D1=81=D0=B8?= =?UTF-8?q?=20=D1=81=D1=82=D1=83=D0=B4=D0=B5=D0=BD=D1=82=D0=B0=20=D0=B4?= =?UTF-8?q?=D0=BB=D1=8F=20=D1=81=D1=82=D0=B0=D1=82=D1=83=D1=81=D0=B0=20?= =?UTF-8?q?=D0=BE=D0=B1=D1=83=D1=87=D0=B5=D0=BD=D0=B8=D1=8F=20=D1=81=D0=B4?= =?UTF-8?q?=D0=B5=D0=BB=D0=B0=D0=BD=D0=B0.=20=D0=9E=D1=81=D1=82=D0=B0?= =?UTF-8?q?=D0=BB=D0=B8=D1=81=D1=8C=20=D0=BE=D1=82=D1=87=D0=B5=D1=82=D1=8B?= =?UTF-8?q?,=20=D0=B2=D0=BE=D0=B7=D0=BC=D0=BE=D0=B6=D0=BD=D0=BE=20=D0=B3?= =?UTF-8?q?=D1=80=D0=B0=D1=84=D0=B8=D0=BA=D0=B8=20=D0=B8=20=D0=B4=D0=B5?= =?UTF-8?q?=D0=B7=D0=B8=D0=B3=D0=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogics/EducationGroupLogic.cs | 3 +- .../BindingModels/DocumentBindingModel.cs | 1 + .../ViewModels/DocumentViewModel.cs | 1 + .../ViewModels/EducationGroupViewModel.cs | 15 +- .../Implements/DisciplineStorage.cs | 2 +- .../Implements/DocumentStorage.cs | 4 + .../Implements/EducationGroupStorage.cs | 8 +- .../Implements/EducationStatusStorage.cs | 4 +- .../Implements/StreamStorage.cs | 2 +- .../Migrations/20230518085846_fix.Designer.cs | 485 ++++++++++++++++++ .../Migrations/20230518085846_fix.cs | 62 +++ .../Migrations/DatabaseModelSnapshot.cs | 12 - UniversityDataBaseImplemet/Models/Document.cs | 39 +- .../Models/EducationGroup.cs | 2 +- .../Models/EducationGroupDocument.cs | 1 - .../Models/EducationGroupStream.cs | 1 - .../Models/StudentDocument.cs | 1 - .../Models/StudentStream.cs | 1 - .../Controllers/DocumentController.cs | 34 +- UniversityProvider/Views/Document/Bind.cshtml | 35 ++ .../Views/Document/Update.cshtml | 42 +- .../Views/Home/Documents.cshtml | 5 +- .../wwwroot/js/document/document-bind.js | 84 +++ .../wwwroot/js/document/document-update.js | 83 ++- .../wwwroot/js/document/documents.js | 4 +- .../Controllers/DocumentController.cs | 18 +- UniversityRestAPI/Program.cs | 2 + 27 files changed, 913 insertions(+), 38 deletions(-) create mode 100644 UniversityDataBaseImplemet/Migrations/20230518085846_fix.Designer.cs create mode 100644 UniversityDataBaseImplemet/Migrations/20230518085846_fix.cs create mode 100644 UniversityProvider/Views/Document/Bind.cshtml create mode 100644 UniversityProvider/wwwroot/js/document/document-bind.js diff --git a/UniversityBusinessLogic/BusinessLogics/EducationGroupLogic.cs b/UniversityBusinessLogic/BusinessLogics/EducationGroupLogic.cs index ad3c95f..4caf119 100644 --- a/UniversityBusinessLogic/BusinessLogics/EducationGroupLogic.cs +++ b/UniversityBusinessLogic/BusinessLogics/EducationGroupLogic.cs @@ -4,13 +4,14 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using UniversityContracts.BindingModels; +using UniversityContracts.BusinessLogicContracts; using UniversityContracts.SearchModels; using UniversityContracts.StoragesContracts; using UniversityContracts.ViewModels; namespace UniversityBusinessLogic.BusinessLogics { - public class EducationGroupLogic + public class EducationGroupLogic : IEducationGroupLogic { private readonly IEducationGroupStorage _egStorage; diff --git a/UniversityContracts/BindingModels/DocumentBindingModel.cs b/UniversityContracts/BindingModels/DocumentBindingModel.cs index 3856cba..435c275 100644 --- a/UniversityContracts/BindingModels/DocumentBindingModel.cs +++ b/UniversityContracts/BindingModels/DocumentBindingModel.cs @@ -15,5 +15,6 @@ namespace UniversityContracts.BindingModels public DateTime Date { get; set; } = DateTime.Now; public int UserId { get; set; } public List DocumentStudents { get; set; } = new(); + public List DocumentGroups { get; set; } = new(); } } diff --git a/UniversityContracts/ViewModels/DocumentViewModel.cs b/UniversityContracts/ViewModels/DocumentViewModel.cs index 1c0c1fc..8bb0086 100644 --- a/UniversityContracts/ViewModels/DocumentViewModel.cs +++ b/UniversityContracts/ViewModels/DocumentViewModel.cs @@ -18,5 +18,6 @@ namespace UniversityContracts.ViewModels [DisplayName("Дата создания документа")] public DateTime Date { get; set; } = DateTime.Now; public List DocumentStudents { get; set; } = new(); + public List DocumentEdGroups { get; set; } = new(); } } diff --git a/UniversityContracts/ViewModels/EducationGroupViewModel.cs b/UniversityContracts/ViewModels/EducationGroupViewModel.cs index 97dfd3a..e2d7bd2 100644 --- a/UniversityContracts/ViewModels/EducationGroupViewModel.cs +++ b/UniversityContracts/ViewModels/EducationGroupViewModel.cs @@ -4,16 +4,27 @@ using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; +using UniversityModels.Models; namespace UniversityContracts.ViewModels { - public class EducationGroupViewModel + public class EducationGroupViewModel : IEducationGroupModel { public int Id { get; set; } [DisplayName("Название группы")] public string Name { get; set; } = string.Empty; [DisplayName("Количество студентов в группе")] - public int NumberOfStudents { get; set; } + public int NumberOfStudent { get; set; } public int UserId { get; set; } + + public EducationGroupViewModel() { } + + public EducationGroupViewModel(IEducationGroupModel model) + { + Id = model.Id; + UserId = model.UserId; + Name = model.Name; + NumberOfStudent = model.NumberOfStudent; + } } } diff --git a/UniversityDataBaseImplemet/Implements/DisciplineStorage.cs b/UniversityDataBaseImplemet/Implements/DisciplineStorage.cs index 0040889..87bec92 100644 --- a/UniversityDataBaseImplemet/Implements/DisciplineStorage.cs +++ b/UniversityDataBaseImplemet/Implements/DisciplineStorage.cs @@ -11,7 +11,7 @@ using UniversityDataBaseImplemet.Models; namespace UniversityDataBaseImplemet.Implements { - public class DisciplineStorage:IDisciplineStorage + public class DisciplineStorage : IDisciplineStorage { public DisciplineViewModel? GetElement(DisciplineSearchModel model) { diff --git a/UniversityDataBaseImplemet/Implements/DocumentStorage.cs b/UniversityDataBaseImplemet/Implements/DocumentStorage.cs index 02cc16a..50e04b0 100644 --- a/UniversityDataBaseImplemet/Implements/DocumentStorage.cs +++ b/UniversityDataBaseImplemet/Implements/DocumentStorage.cs @@ -28,6 +28,8 @@ namespace UniversityDataBaseImplemet.Implements .Include(record => record.User) .Include(record => record.Students) .ThenInclude(record => record.Student) + .Include(record => record.EducationGroupDocument) + .ThenInclude(record => record.EducationGroup) .FirstOrDefault(record => record.Id == model.Id || record.Name.Equals(model.Name)) ?.GetViewModel; @@ -113,6 +115,7 @@ namespace UniversityDataBaseImplemet.Implements document.Update(model); context.SaveChanges(); document.UpdateStudents(context, model); + document.UpdateGroups(context, model); transaction.Commit(); return document.GetViewModel; } @@ -127,6 +130,7 @@ namespace UniversityDataBaseImplemet.Implements using var context = new Database(); var document = context.Documents .Include(record => record.User) + .Include(record => record.Students) .FirstOrDefault(record => record.Id.Equals(model.Id)); if (document == null) { diff --git a/UniversityDataBaseImplemet/Implements/EducationGroupStorage.cs b/UniversityDataBaseImplemet/Implements/EducationGroupStorage.cs index 644cb19..b7727e3 100644 --- a/UniversityDataBaseImplemet/Implements/EducationGroupStorage.cs +++ b/UniversityDataBaseImplemet/Implements/EducationGroupStorage.cs @@ -1,16 +1,18 @@ -using System; +using Microsoft.EntityFrameworkCore; +using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using UniversityContracts.BindingModels; using UniversityContracts.SearchModels; +using UniversityContracts.StoragesContracts; using UniversityContracts.ViewModels; using UniversityDataBaseImplemet.Models; namespace UniversityDataBaseImplemet.Implements { - public class EducationGroupStorage + public class EducationGroupStorage : IEducationGroupStorage { public EducationGroupViewModel? GetElement(EducationGroupSearchModel model) { @@ -49,7 +51,7 @@ namespace UniversityDataBaseImplemet.Implements public List GetFullList() { using var context = new Database(); - return context.EducationGroups + return context.EducationGroups .Select(record => record.GetViewModel) .ToList(); } diff --git a/UniversityDataBaseImplemet/Implements/EducationStatusStorage.cs b/UniversityDataBaseImplemet/Implements/EducationStatusStorage.cs index 1652349..97c1055 100644 --- a/UniversityDataBaseImplemet/Implements/EducationStatusStorage.cs +++ b/UniversityDataBaseImplemet/Implements/EducationStatusStorage.cs @@ -56,8 +56,7 @@ namespace UniversityDataBaseImplemet.Implements public List GetFullList() { using var context = new Database(); - return context.EducationStatuses - .Include(record => record.User) + return context.EducationStatuses .Select(record => record.GetViewModel) .ToList(); } @@ -108,6 +107,7 @@ namespace UniversityDataBaseImplemet.Implements using var context = new Database(); var educationStatus = context.EducationStatuses .Include(record => record.User) + .Include(record => record.Students) .FirstOrDefault(record => record.Id.Equals(model.Id)); if (educationStatus == null) { diff --git a/UniversityDataBaseImplemet/Implements/StreamStorage.cs b/UniversityDataBaseImplemet/Implements/StreamStorage.cs index 9152a39..5edc699 100644 --- a/UniversityDataBaseImplemet/Implements/StreamStorage.cs +++ b/UniversityDataBaseImplemet/Implements/StreamStorage.cs @@ -12,7 +12,7 @@ using Stream = UniversityDataBaseImplemet.Models.Stream; namespace UniversityDataBaseImplemet.Implements { - public class StreamStorage:IStreamStorage + public class StreamStorage : IStreamStorage { public StreamViewModel? GetElement(StreamSearchModel model) { diff --git a/UniversityDataBaseImplemet/Migrations/20230518085846_fix.Designer.cs b/UniversityDataBaseImplemet/Migrations/20230518085846_fix.Designer.cs new file mode 100644 index 0000000..7ac9d24 --- /dev/null +++ b/UniversityDataBaseImplemet/Migrations/20230518085846_fix.Designer.cs @@ -0,0 +1,485 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; +using UniversityDataBaseImplemet; + +#nullable disable + +namespace UniversityDataBaseImplemet.Migrations +{ + [DbContext(typeof(Database))] + [Migration("20230518085846_fix")] + partial class fix + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.5") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("UniversityDataBaseImplemet.Models.Discipline", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Hours") + .HasColumnType("integer"); + + b.Property("MarkType") + .HasColumnType("boolean"); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.Property("StreamId") + .HasColumnType("integer"); + + b.Property("UserId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("StreamId"); + + b.HasIndex("UserId"); + + b.ToTable("Discipline"); + }); + + modelBuilder.Entity("UniversityDataBaseImplemet.Models.Document", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Date") + .HasColumnType("timestamp with time zone"); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.Property("UserId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("Documents"); + }); + + modelBuilder.Entity("UniversityDataBaseImplemet.Models.EducationGroup", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.Property("NumberOfStudent") + .HasColumnType("integer"); + + b.Property("UserId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("EducationGroups"); + }); + + modelBuilder.Entity("UniversityDataBaseImplemet.Models.EducationGroupDocument", b => + { + b.Property("EducationGroupId") + .HasColumnType("integer"); + + b.Property("DocumentId") + .HasColumnType("integer"); + + b.HasKey("EducationGroupId", "DocumentId"); + + b.HasIndex("DocumentId"); + + b.ToTable("EducationGroupsDocuments"); + }); + + modelBuilder.Entity("UniversityDataBaseImplemet.Models.EducationGroupStream", b => + { + b.Property("EducationGroupId") + .HasColumnType("integer"); + + b.Property("StreamId") + .HasColumnType("integer"); + + b.HasKey("EducationGroupId", "StreamId"); + + b.HasIndex("StreamId"); + + b.ToTable("EducationGroupsStreams"); + }); + + modelBuilder.Entity("UniversityDataBaseImplemet.Models.EducationStatus", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.Property("UserId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("EducationStatuses"); + }); + + modelBuilder.Entity("UniversityDataBaseImplemet.Models.Stream", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Course") + .HasColumnType("integer"); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.Property("UserId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("Streams"); + }); + + modelBuilder.Entity("UniversityDataBaseImplemet.Models.Student", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("DateOfBirth") + .HasColumnType("timestamp with time zone"); + + b.Property("EducationStatusId") + .HasColumnType("integer"); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.Property("StudentCard") + .HasColumnType("integer"); + + b.Property("Surname") + .IsRequired() + .HasColumnType("text"); + + b.Property("UserId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("EducationStatusId"); + + b.HasIndex("UserId"); + + b.ToTable("Students"); + }); + + modelBuilder.Entity("UniversityDataBaseImplemet.Models.StudentDocument", b => + { + b.Property("StudentId") + .HasColumnType("integer"); + + b.Property("DocumentId") + .HasColumnType("integer"); + + b.HasKey("StudentId", "DocumentId"); + + b.HasIndex("DocumentId"); + + b.ToTable("StudentDocuments"); + }); + + modelBuilder.Entity("UniversityDataBaseImplemet.Models.StudentStream", b => + { + b.Property("StudentId") + .HasColumnType("integer"); + + b.Property("StreamId") + .HasColumnType("integer"); + + b.HasKey("StudentId", "StreamId"); + + b.HasIndex("StreamId"); + + b.ToTable("StudentStreams"); + }); + + modelBuilder.Entity("UniversityDataBaseImplemet.Models.User", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Login") + .IsRequired() + .HasColumnType("text"); + + b.Property("Password") + .IsRequired() + .HasColumnType("text"); + + b.Property("Role") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.ToTable("User"); + }); + + modelBuilder.Entity("UniversityDataBaseImplemet.Models.Discipline", b => + { + b.HasOne("UniversityDataBaseImplemet.Models.Stream", "Stream") + .WithMany() + .HasForeignKey("StreamId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("UniversityDataBaseImplemet.Models.User", "User") + .WithMany("Disciplines") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Stream"); + + b.Navigation("User"); + }); + + modelBuilder.Entity("UniversityDataBaseImplemet.Models.Document", b => + { + b.HasOne("UniversityDataBaseImplemet.Models.User", "User") + .WithMany("Documents") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.NoAction) + .IsRequired(); + + b.Navigation("User"); + }); + + modelBuilder.Entity("UniversityDataBaseImplemet.Models.EducationGroup", b => + { + b.HasOne("UniversityDataBaseImplemet.Models.User", "User") + .WithMany("EducationGroups") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("User"); + }); + + modelBuilder.Entity("UniversityDataBaseImplemet.Models.EducationGroupDocument", b => + { + b.HasOne("UniversityDataBaseImplemet.Models.Document", "Document") + .WithMany("EducationGroupDocument") + .HasForeignKey("DocumentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("UniversityDataBaseImplemet.Models.EducationGroup", "EducationGroup") + .WithMany("EducationGroupDocument") + .HasForeignKey("EducationGroupId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Document"); + + b.Navigation("EducationGroup"); + }); + + modelBuilder.Entity("UniversityDataBaseImplemet.Models.EducationGroupStream", b => + { + b.HasOne("UniversityDataBaseImplemet.Models.EducationGroup", "EducationGroup") + .WithMany("EducationGroupStream") + .HasForeignKey("EducationGroupId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("UniversityDataBaseImplemet.Models.Stream", "Stream") + .WithMany("EducationGroupStream") + .HasForeignKey("StreamId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("EducationGroup"); + + b.Navigation("Stream"); + }); + + modelBuilder.Entity("UniversityDataBaseImplemet.Models.EducationStatus", b => + { + b.HasOne("UniversityDataBaseImplemet.Models.User", "User") + .WithMany("EducationStatuses") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.NoAction) + .IsRequired(); + + b.Navigation("User"); + }); + + modelBuilder.Entity("UniversityDataBaseImplemet.Models.Stream", b => + { + b.HasOne("UniversityDataBaseImplemet.Models.User", "User") + .WithMany("Streams") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("User"); + }); + + modelBuilder.Entity("UniversityDataBaseImplemet.Models.Student", b => + { + b.HasOne("UniversityDataBaseImplemet.Models.EducationStatus", "EducationStatus") + .WithMany("Students") + .HasForeignKey("EducationStatusId"); + + b.HasOne("UniversityDataBaseImplemet.Models.User", "User") + .WithMany("Students") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.NoAction) + .IsRequired(); + + b.Navigation("EducationStatus"); + + b.Navigation("User"); + }); + + modelBuilder.Entity("UniversityDataBaseImplemet.Models.StudentDocument", b => + { + b.HasOne("UniversityDataBaseImplemet.Models.Document", "Document") + .WithMany("Students") + .HasForeignKey("DocumentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("UniversityDataBaseImplemet.Models.Student", "Student") + .WithMany("DocumentStudents") + .HasForeignKey("StudentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Document"); + + b.Navigation("Student"); + }); + + modelBuilder.Entity("UniversityDataBaseImplemet.Models.StudentStream", b => + { + b.HasOne("UniversityDataBaseImplemet.Models.Stream", "Stream") + .WithMany("StreamStudents") + .HasForeignKey("StreamId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("UniversityDataBaseImplemet.Models.Student", "Student") + .WithMany("StudentStream") + .HasForeignKey("StudentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Stream"); + + b.Navigation("Student"); + }); + + modelBuilder.Entity("UniversityDataBaseImplemet.Models.Document", b => + { + b.Navigation("EducationGroupDocument"); + + b.Navigation("Students"); + }); + + modelBuilder.Entity("UniversityDataBaseImplemet.Models.EducationGroup", b => + { + b.Navigation("EducationGroupDocument"); + + b.Navigation("EducationGroupStream"); + }); + + modelBuilder.Entity("UniversityDataBaseImplemet.Models.EducationStatus", b => + { + b.Navigation("Students"); + }); + + modelBuilder.Entity("UniversityDataBaseImplemet.Models.Stream", b => + { + b.Navigation("EducationGroupStream"); + + b.Navigation("StreamStudents"); + }); + + modelBuilder.Entity("UniversityDataBaseImplemet.Models.Student", b => + { + b.Navigation("DocumentStudents"); + + b.Navigation("StudentStream"); + }); + + modelBuilder.Entity("UniversityDataBaseImplemet.Models.User", b => + { + b.Navigation("Disciplines"); + + b.Navigation("Documents"); + + b.Navigation("EducationGroups"); + + b.Navigation("EducationStatuses"); + + b.Navigation("Streams"); + + b.Navigation("Students"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/UniversityDataBaseImplemet/Migrations/20230518085846_fix.cs b/UniversityDataBaseImplemet/Migrations/20230518085846_fix.cs new file mode 100644 index 0000000..d53c8f0 --- /dev/null +++ b/UniversityDataBaseImplemet/Migrations/20230518085846_fix.cs @@ -0,0 +1,62 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace UniversityDataBaseImplemet.Migrations +{ + /// + public partial class fix : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropColumn( + name: "Id", + table: "StudentStreams"); + + migrationBuilder.DropColumn( + name: "Id", + table: "StudentDocuments"); + + migrationBuilder.DropColumn( + name: "Id", + table: "EducationGroupsStreams"); + + migrationBuilder.DropColumn( + name: "Id", + table: "EducationGroupsDocuments"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.AddColumn( + name: "Id", + table: "StudentStreams", + type: "integer", + nullable: false, + defaultValue: 0); + + migrationBuilder.AddColumn( + name: "Id", + table: "StudentDocuments", + type: "integer", + nullable: false, + defaultValue: 0); + + migrationBuilder.AddColumn( + name: "Id", + table: "EducationGroupsStreams", + type: "integer", + nullable: false, + defaultValue: 0); + + migrationBuilder.AddColumn( + name: "Id", + table: "EducationGroupsDocuments", + type: "integer", + nullable: false, + defaultValue: 0); + } + } +} diff --git a/UniversityDataBaseImplemet/Migrations/DatabaseModelSnapshot.cs b/UniversityDataBaseImplemet/Migrations/DatabaseModelSnapshot.cs index 0d0cd3e..5c8e40d 100644 --- a/UniversityDataBaseImplemet/Migrations/DatabaseModelSnapshot.cs +++ b/UniversityDataBaseImplemet/Migrations/DatabaseModelSnapshot.cs @@ -113,9 +113,6 @@ namespace UniversityDataBaseImplemet.Migrations b.Property("DocumentId") .HasColumnType("integer"); - b.Property("Id") - .HasColumnType("integer"); - b.HasKey("EducationGroupId", "DocumentId"); b.HasIndex("DocumentId"); @@ -131,9 +128,6 @@ namespace UniversityDataBaseImplemet.Migrations b.Property("StreamId") .HasColumnType("integer"); - b.Property("Id") - .HasColumnType("integer"); - b.HasKey("EducationGroupId", "StreamId"); b.HasIndex("StreamId"); @@ -233,9 +227,6 @@ namespace UniversityDataBaseImplemet.Migrations b.Property("DocumentId") .HasColumnType("integer"); - b.Property("Id") - .HasColumnType("integer"); - b.HasKey("StudentId", "DocumentId"); b.HasIndex("DocumentId"); @@ -251,9 +242,6 @@ namespace UniversityDataBaseImplemet.Migrations b.Property("StreamId") .HasColumnType("integer"); - b.Property("Id") - .HasColumnType("integer"); - b.HasKey("StudentId", "StreamId"); b.HasIndex("StreamId"); diff --git a/UniversityDataBaseImplemet/Models/Document.cs b/UniversityDataBaseImplemet/Models/Document.cs index 98b24c3..11910c4 100644 --- a/UniversityDataBaseImplemet/Models/Document.cs +++ b/UniversityDataBaseImplemet/Models/Document.cs @@ -38,6 +38,19 @@ namespace UniversityDataBaseImplemet.Models return _documentStudents; } } + private List? _documentEdGroups = null; + [NotMapped] + public List DocumentEdGroups + { + get + { + if (_documentEdGroups == null) + { + _documentEdGroups = EducationGroupDocument.Select(x => new EducationGroupViewModel(x.EducationGroup)).ToList(); + } + return _documentEdGroups; + } + } public static Document? Create(Database context, DocumentBindingModel? model) { if (model == null) @@ -89,13 +102,37 @@ namespace UniversityDataBaseImplemet.Models } _documentStudents = null; } + public void UpdateGroups(Database context, DocumentBindingModel model) + { + var documentGroups = context.EducationGroupsDocuments.Where(x => x.DocumentId == model.Id).ToList(); + List currentGroups = documentGroups.Select(x => x.EducationGroupId).ToList(); + List modelGroups = model.DocumentGroups.Select(x => x.Id).ToList(); + if (documentGroups != null && documentGroups.Count > 0) + { + context.EducationGroupsDocuments.RemoveRange(documentGroups.Where(x => !modelGroups.Contains(x.EducationGroupId))); + model.DocumentGroups.RemoveAll(x => currentGroups.Contains(x.Id)); + context.SaveChanges(); + } + var document = context.Documents.First(x => x.Id == Id); + foreach (var record in model.DocumentGroups) + { + context.EducationGroupsDocuments.Add(new EducationGroupDocument + { + Document = document, + EducationGroup = context.EducationGroups.First(x => x.Id == record.Id), + }); + context.SaveChanges(); + } + _documentEdGroups = null; + } public DocumentViewModel GetViewModel => new() { Id = Id, Name = Name, Date = Date, UserId = UserId, - DocumentStudents = DocumentStudents + DocumentStudents = DocumentStudents, + DocumentEdGroups = DocumentEdGroups, }; } } diff --git a/UniversityDataBaseImplemet/Models/EducationGroup.cs b/UniversityDataBaseImplemet/Models/EducationGroup.cs index 2bc7079..0d88dff 100644 --- a/UniversityDataBaseImplemet/Models/EducationGroup.cs +++ b/UniversityDataBaseImplemet/Models/EducationGroup.cs @@ -50,7 +50,7 @@ namespace UniversityDataBaseImplemet.Models Id = Id, Name = Name, UserId = UserId, - NumberOfStudents= NumberOfStudent + NumberOfStudent = NumberOfStudent }; } diff --git a/UniversityDataBaseImplemet/Models/EducationGroupDocument.cs b/UniversityDataBaseImplemet/Models/EducationGroupDocument.cs index fc64da7..aead609 100644 --- a/UniversityDataBaseImplemet/Models/EducationGroupDocument.cs +++ b/UniversityDataBaseImplemet/Models/EducationGroupDocument.cs @@ -9,7 +9,6 @@ namespace UniversityDataBaseImplemet.Models { public class EducationGroupDocument { - public int Id { get; set; } [Required] public int EducationGroupId { get; set; } [Required] diff --git a/UniversityDataBaseImplemet/Models/EducationGroupStream.cs b/UniversityDataBaseImplemet/Models/EducationGroupStream.cs index cc7afca..677eb26 100644 --- a/UniversityDataBaseImplemet/Models/EducationGroupStream.cs +++ b/UniversityDataBaseImplemet/Models/EducationGroupStream.cs @@ -9,7 +9,6 @@ namespace UniversityDataBaseImplemet.Models { public class EducationGroupStream { - public int Id { get; set; } [Required] public int EducationGroupId { get; set; } [Required] diff --git a/UniversityDataBaseImplemet/Models/StudentDocument.cs b/UniversityDataBaseImplemet/Models/StudentDocument.cs index 263c6ef..a942dd8 100644 --- a/UniversityDataBaseImplemet/Models/StudentDocument.cs +++ b/UniversityDataBaseImplemet/Models/StudentDocument.cs @@ -9,7 +9,6 @@ namespace UniversityDataBaseImplemet.Models { public class StudentDocument { - public int Id { get; set; } [Required] public int StudentId { get; set; } [Required] diff --git a/UniversityDataBaseImplemet/Models/StudentStream.cs b/UniversityDataBaseImplemet/Models/StudentStream.cs index 6cbcec3..bfc5efc 100644 --- a/UniversityDataBaseImplemet/Models/StudentStream.cs +++ b/UniversityDataBaseImplemet/Models/StudentStream.cs @@ -9,7 +9,6 @@ namespace UniversityDataBaseImplemet.Models { public class StudentStream { - public int Id { get; set; } [Required] public int StudentId { get; set; } [Required] diff --git a/UniversityProvider/Controllers/DocumentController.cs b/UniversityProvider/Controllers/DocumentController.cs index 8ed6157..c305387 100644 --- a/UniversityProvider/Controllers/DocumentController.cs +++ b/UniversityProvider/Controllers/DocumentController.cs @@ -69,7 +69,27 @@ namespace UniversityProvider.Controllers APIClient.PostRequest("api/document/update", documentModel); } - [HttpPost] + public IActionResult Bind(int id) + { + if (APIClient.User == null) + { + return Redirect("~/Home/Enter"); + } + ViewBag.Document = APIClient.GetRequest($"api/document/get?id={id}"); + return View(); + } + + [HttpPost] + public void Bind([FromBody] DocumentBindingModel documentModel) + { + if (APIClient.User == null) + { + throw new Exception("403"); + } + APIClient.PostRequest("api/document/update", documentModel); + } + + [HttpPost] public void Delete(int id) { if (APIClient.User == null) @@ -90,7 +110,17 @@ namespace UniversityProvider.Controllers return document ?? new(); } - public DocumentViewModel? Get(int id) + public List GetAllGroups() + { + if (APIClient.User == null) + { + return new(); + } + List? group = APIClient.GetRequest>("api/document/GetAllGroups"); + return group ?? new(); + } + + public DocumentViewModel? Get(int id) { if (APIClient.User == null) { diff --git a/UniversityProvider/Views/Document/Bind.cshtml b/UniversityProvider/Views/Document/Bind.cshtml new file mode 100644 index 0000000..13a7f35 --- /dev/null +++ b/UniversityProvider/Views/Document/Bind.cshtml @@ -0,0 +1,35 @@ +@{ + ViewData["Title"] = "Приказ"; +} + +@{ +

Привязка группы к приказу

+ +
+
+

+
+
+ +

Название:

+ + + + +
+
+ + + + + + + + +
Название группы:
+
+
+} + \ No newline at end of file diff --git a/UniversityProvider/Views/Document/Update.cshtml b/UniversityProvider/Views/Document/Update.cshtml index e1dd794..9e6e5f2 100644 --- a/UniversityProvider/Views/Document/Update.cshtml +++ b/UniversityProvider/Views/Document/Update.cshtml @@ -1,5 +1,39 @@ -@* - For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 -*@ -@{ +@{ + ViewData["Title"] = "Приказ"; } + +@{ +

Обновление приказа

+ +
+
+

+
+
+ +

Название:

+ + + + +
+
+ + + + + + + + + + + + +
ИмяФамилияДата рожденияНомер студ. билетаСтатус обучения
+
+
+} + \ No newline at end of file diff --git a/UniversityProvider/Views/Home/Documents.cshtml b/UniversityProvider/Views/Home/Documents.cshtml index 10ad681..1912345 100644 --- a/UniversityProvider/Views/Home/Documents.cshtml +++ b/UniversityProvider/Views/Home/Documents.cshtml @@ -55,6 +55,9 @@ @item.Date.ToString("yyyy-MM-dd") + + Привязка + Изменить @@ -68,4 +71,4 @@ } - + diff --git a/UniversityProvider/wwwroot/js/document/document-bind.js b/UniversityProvider/wwwroot/js/document/document-bind.js new file mode 100644 index 0000000..b1c23c3 --- /dev/null +++ b/UniversityProvider/wwwroot/js/document/document-bind.js @@ -0,0 +1,84 @@ +const createBtn = document.getElementById("create-button"); +const tbody = document.getElementById("scrollable-table__tbody"); +const nameInput = document.getElementById("name-input"); +const currentDocumentId = document.getElementById("document-data").dataset.id; +var groups = []; +var dataArray = []; +var currentDocument = null; + +window.addEventListener('load', async () => { + await $.ajax({ + url: "/document/getallgroups", + type: "GET", + contentType: "json" + }).done((result) => { + groups = result; + console.log(groups) + }); + await $.ajax({ + url: `/document/get?id=${currentDocumentId}`, + type: "GET", + contentType: "json" + }).done((result) => { + currentDocument = result; + console.log(currentDocument) + }); + groups.forEach((group) => createRowForGroupsTable(group)); +}) + +createBtn.addEventListener('click', () => { + var documentGroupsUpdate = { + "Id": currentDocument.id, + "Name": currentDocument.name, + "Date": currentDocument.date, + "DocumentStudents": currentDocument.documentStudents, + "DocumentGroups": dataArray, + } + $.ajax({ + url: "/document/update", + type: "POST", + contentType: "application/json", + data: JSON.stringify(documentGroupsUpdate) + }).done(() => { + window.location.href = "/Home/Documents"; + }); +}) + +const createRowForGroupsTable = (group) => { + const { id, name } = group; + const row = tbody.insertRow(); + row.setAttribute("data-id", id); + + const cells = [name]; + cells.forEach((value) => { + const cell = row.insertCell(); + cell.textContent = value; + }); + console.log(currentDocument) + if (currentDocument.documentEdGroups?.find(x => parseInt(x.id) === parseInt(group.id))) { + row.classList.add("bg-success"); + dataArray.push(group); + } + + row.addEventListener('click', () => addAndRemoveFromList(row)); +}; + +const formatDate = (dateString) => { + const date = new Date(dateString); + const year = date.getFullYear(); + const month = ('0' + (date.getMonth() + 1)).slice(-2); + const day = ('0' + date.getDate()).slice(-2); + return `${year}-${month}-${day}`; +}; + +const addAndRemoveFromList = (row) => { + var id = parseInt(row.dataset.id); + var index = dataArray.indexOf(groups.find(x => x.id === id)); + if (index === -1) { + dataArray.push(groups.find(x => x.id === id)); + row.classList.add("bg-success"); + } else { + dataArray.splice(index, 1); + row.classList.remove("bg-success"); + } +} \ No newline at end of file diff --git a/UniversityProvider/wwwroot/js/document/document-update.js b/UniversityProvider/wwwroot/js/document/document-update.js index 5f28270..be3a20c 100644 --- a/UniversityProvider/wwwroot/js/document/document-update.js +++ b/UniversityProvider/wwwroot/js/document/document-update.js @@ -1 +1,82 @@ - \ No newline at end of file +const createBtn = document.getElementById("create-button"); +const tbody = document.getElementById("scrollable-table__tbody"); +const nameInput = document.getElementById("name-input"); +const currentDocumentId = document.getElementById("document-data").dataset.id; +var students = []; +var dataArray = []; +var currentDocument = null; + +window.addEventListener('load', async () => { + await $.ajax({ + url: "/student/getallbyuser", + type: "GET", + contentType: "json" + }).done((result) => { + students = result; + }); + await $.ajax({ + url: `/document/get?id=${currentDocumentId}`, + type: "GET", + contentType: "json" + }).done((result) => { + currentDocument = result; + }); + students.forEach((student) => createRowForStudentsTable(student)); +}) + +createBtn.addEventListener('click', () => { + var documentGroupsUpdate = { + "Id": currentDocument.id, + "Name": nameInput.value, + "Date": currentDocument.date, + "DocumentStudents": dataArray, + "DocumentGroups": currentDocument.documentEdGroups, + } + $.ajax({ + url: "/document/update", + type: "POST", + contentType: "application/json", + data: JSON.stringify(documentGroupsUpdate) + }).done(() => { + window.location.href = "/Home/Documents"; + }); +}) + +const createRowForStudentsTable = (student) => { + const { id, name, surname, dateOfBirth, studentCard, educationStatusName } = student; + const row = tbody.insertRow(); + row.setAttribute("data-id", id); + + const cells = [name, surname, formatDate(dateOfBirth), studentCard, educationStatusName]; + cells.forEach((value) => { + const cell = row.insertCell(); + cell.textContent = value; + }); + + if (currentDocument.documentStudents.find(x => parseInt(x.id) === parseInt(student.id))) { + row.classList.add("bg-success"); + dataArray.push(student); + } + + row.addEventListener('click', () => addAndRemoveFromList(row)); +}; + +const formatDate = (dateString) => { + const date = new Date(dateString); + const year = date.getFullYear(); + const month = ('0' + (date.getMonth() + 1)).slice(-2); + const day = ('0' + date.getDate()).slice(-2); + return `${year}-${month}-${day}`; +}; + +const addAndRemoveFromList = (row) => { + var id = parseInt(row.dataset.id); + var index = dataArray.indexOf(students.find(x => x.id === id)); + if (index === -1) { + dataArray.push(students.find(x => x.id === id)); + row.classList.add("bg-success"); + } else { + dataArray.splice(index, 1); + row.classList.remove("bg-success"); + } +} \ No newline at end of file diff --git a/UniversityProvider/wwwroot/js/document/documents.js b/UniversityProvider/wwwroot/js/document/documents.js index d4554b2..489391f 100644 --- a/UniversityProvider/wwwroot/js/document/documents.js +++ b/UniversityProvider/wwwroot/js/document/documents.js @@ -2,10 +2,12 @@ const pageInput = document.getElementById("page-input"); const removeButtons = document.querySelectorAll(".remove-btn"); +console.log(removeButtons) + removeButtons.forEach(function (button) { button.addEventListener("click", function (event) { var id = this.dataset.id; - + console.log(id) var result = confirm("Вы уверены, что хотите удалить эту запись?"); if (result) { $.ajax({ diff --git a/UniversityRestAPI/Controllers/DocumentController.cs b/UniversityRestAPI/Controllers/DocumentController.cs index 273712f..128711d 100644 --- a/UniversityRestAPI/Controllers/DocumentController.cs +++ b/UniversityRestAPI/Controllers/DocumentController.cs @@ -11,10 +11,13 @@ namespace UniversityRestAPI.Controllers public class DocumentController : Controller { private readonly IDocumentLogic _documentLogic; + private readonly IEducationGroupLogic _edGroupLogic; - public DocumentController(IDocumentLogic documentLogic) + public DocumentController(IDocumentLogic documentLogic, + IEducationGroupLogic edGroupLogic) { _documentLogic = documentLogic; + _edGroupLogic = edGroupLogic; } [HttpGet] @@ -56,6 +59,19 @@ namespace UniversityRestAPI.Controllers } } + [HttpGet] + public List? GetAllGroups() + { + try + { + return _edGroupLogic.ReadList(null); + } + catch (Exception ex) + { + throw; + } + } + [HttpGet] public int GetNumberOfPages(int userId) { diff --git a/UniversityRestAPI/Program.cs b/UniversityRestAPI/Program.cs index da817c9..993efe7 100644 --- a/UniversityRestAPI/Program.cs +++ b/UniversityRestAPI/Program.cs @@ -11,11 +11,13 @@ builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); +builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); +builder.Services.AddTransient(); builder.Services.AddControllers();