diff --git a/QuizApp.Database/Migrations/20250103050729_initmigration.Designer.cs b/QuizApp.Database/Migrations/20250103050729_initmigration.Designer.cs deleted file mode 100644 index 747cefc..0000000 --- a/QuizApp.Database/Migrations/20250103050729_initmigration.Designer.cs +++ /dev/null @@ -1,269 +0,0 @@ -// -using System; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Migrations; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; -using QuizApp.Database; - -#nullable disable - -namespace QuizApp.Database.Migrations -{ - [DbContext(typeof(QuizContext))] - [Migration("20250103050729_initmigration")] - partial class initmigration - { - /// - protected override void BuildTargetModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder.HasAnnotation("ProductVersion", "9.0.0"); - - modelBuilder.Entity("QuizApp.Database.Models.BaseModel", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - - b.Property("Discriminator") - .IsRequired() - .HasMaxLength(13) - .HasColumnType("TEXT"); - - b.HasKey("Id"); - - b.ToTable("BaseModel"); - - b.HasDiscriminator().HasValue("BaseModel"); - - b.UseTphMappingStrategy(); - }); - - modelBuilder.Entity("QuizApp.Database.Models.Answer", b => - { - b.HasBaseType("QuizApp.Database.Models.BaseModel"); - - b.Property("ChoiceId") - .HasColumnType("INTEGER"); - - b.Property("QuestionId") - .HasColumnType("INTEGER"); - - b.Property("QuestionId1") - .HasColumnType("INTEGER"); - - b.Property("SubmissionId") - .HasColumnType("INTEGER"); - - b.HasIndex("ChoiceId"); - - b.HasIndex("QuestionId"); - - b.HasIndex("QuestionId1"); - - b.HasIndex("SubmissionId"); - - b.HasDiscriminator().HasValue("Answer"); - }); - - modelBuilder.Entity("QuizApp.Database.Models.Choice", b => - { - b.HasBaseType("QuizApp.Database.Models.BaseModel"); - - b.Property("ChoiceTitle") - .IsRequired() - .HasMaxLength(500) - .HasColumnType("TEXT"); - - b.Property("QuestionId") - .HasColumnType("INTEGER"); - - b.HasIndex("QuestionId"); - - b.ToTable("BaseModel", t => - { - t.Property("QuestionId") - .HasColumnName("Choice_QuestionId"); - }); - - b.HasDiscriminator().HasValue("Choice"); - }); - - modelBuilder.Entity("QuizApp.Database.Models.Question", b => - { - b.HasBaseType("QuizApp.Database.Models.BaseModel"); - - b.Property("QuizId") - .HasColumnType("INTEGER"); - - b.Property("Title") - .IsRequired() - .HasColumnType("TEXT"); - - b.HasIndex("QuizId"); - - b.HasDiscriminator().HasValue("Question"); - }); - - modelBuilder.Entity("QuizApp.Database.Models.Quiz", b => - { - b.HasBaseType("QuizApp.Database.Models.BaseModel"); - - b.Property("Description") - .IsRequired() - .HasColumnType("TEXT"); - - b.Property("QuizName") - .IsRequired() - .HasColumnType("TEXT"); - - b.HasDiscriminator().HasValue("Quiz"); - }); - - modelBuilder.Entity("QuizApp.Database.Models.Submission", b => - { - b.HasBaseType("QuizApp.Database.Models.BaseModel"); - - b.Property("QuizId") - .HasColumnType("INTEGER"); - - b.Property("Score") - .HasColumnType("INTEGER"); - - b.Property("SubmittedAt") - .HasColumnType("TEXT"); - - b.Property("UserId") - .HasColumnType("INTEGER"); - - b.HasIndex("QuizId"); - - b.HasIndex("UserId"); - - b.ToTable("BaseModel", t => - { - t.Property("QuizId") - .HasColumnName("Submission_QuizId"); - }); - - b.HasDiscriminator().HasValue("Submission"); - }); - - modelBuilder.Entity("QuizApp.Database.Models.User", b => - { - b.HasBaseType("QuizApp.Database.Models.BaseModel"); - - b.Property("Email") - .IsRequired() - .HasColumnType("TEXT"); - - b.Property("JoinDate") - .HasColumnType("TEXT"); - - b.Property("Role") - .IsRequired() - .HasColumnType("TEXT"); - - b.Property("Username") - .IsRequired() - .HasColumnType("TEXT"); - - b.HasDiscriminator().HasValue("User"); - }); - - modelBuilder.Entity("QuizApp.Database.Models.Answer", b => - { - b.HasOne("QuizApp.Database.Models.Choice", "Choice") - .WithMany() - .HasForeignKey("ChoiceId") - .OnDelete(DeleteBehavior.Restrict) - .IsRequired(); - - b.HasOne("QuizApp.Database.Models.Question", "Question") - .WithMany() - .HasForeignKey("QuestionId") - .OnDelete(DeleteBehavior.Restrict) - .IsRequired(); - - b.HasOne("QuizApp.Database.Models.Question", null) - .WithMany("Answers") - .HasForeignKey("QuestionId1"); - - b.HasOne("QuizApp.Database.Models.Submission", "Submission") - .WithMany("Answers") - .HasForeignKey("SubmissionId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("Choice"); - - b.Navigation("Question"); - - b.Navigation("Submission"); - }); - - modelBuilder.Entity("QuizApp.Database.Models.Choice", b => - { - b.HasOne("QuizApp.Database.Models.Question", "Question") - .WithMany() - .HasForeignKey("QuestionId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("Question"); - }); - - modelBuilder.Entity("QuizApp.Database.Models.Question", b => - { - b.HasOne("QuizApp.Database.Models.Quiz", "Quiz") - .WithMany("Questions") - .HasForeignKey("QuizId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("Quiz"); - }); - - modelBuilder.Entity("QuizApp.Database.Models.Submission", b => - { - b.HasOne("QuizApp.Database.Models.Quiz", "Quiz") - .WithMany() - .HasForeignKey("QuizId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("QuizApp.Database.Models.User", "User") - .WithMany("Submissions") - .HasForeignKey("UserId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("Quiz"); - - b.Navigation("User"); - }); - - modelBuilder.Entity("QuizApp.Database.Models.Question", b => - { - b.Navigation("Answers"); - }); - - modelBuilder.Entity("QuizApp.Database.Models.Quiz", b => - { - b.Navigation("Questions"); - }); - - modelBuilder.Entity("QuizApp.Database.Models.Submission", b => - { - b.Navigation("Answers"); - }); - - modelBuilder.Entity("QuizApp.Database.Models.User", b => - { - b.Navigation("Submissions"); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/QuizApp.Database/Migrations/20250103050729_initmigration.cs b/QuizApp.Database/Migrations/20250103050729_initmigration.cs deleted file mode 100644 index 8cfeb98..0000000 --- a/QuizApp.Database/Migrations/20250103050729_initmigration.cs +++ /dev/null @@ -1,595 +0,0 @@ -using System; -using Microsoft.EntityFrameworkCore.Migrations; - -#nullable disable - -#pragma warning disable CA1814 // Prefer jagged arrays over multidimensional - -namespace QuizApp.Database.Migrations -{ - /// - public partial class initmigration : Migration - { - /// - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropTable( - name: "Answers"); - - migrationBuilder.DropTable( - name: "Choices"); - - migrationBuilder.DropTable( - name: "Submissions"); - - migrationBuilder.DropTable( - name: "Questions"); - - migrationBuilder.DropTable( - name: "Quizzes"); - - migrationBuilder.DropPrimaryKey( - name: "PK_Users", - table: "Users"); - - migrationBuilder.RenameTable( - name: "Users", - newName: "BaseModel"); - - migrationBuilder.AlterColumn( - name: "Username", - table: "BaseModel", - type: "TEXT", - nullable: true, - oldClrType: typeof(string), - oldType: "TEXT"); - - migrationBuilder.AlterColumn( - name: "Role", - table: "BaseModel", - type: "TEXT", - nullable: true, - oldClrType: typeof(string), - oldType: "TEXT"); - - migrationBuilder.AddColumn( - name: "ChoiceId", - table: "BaseModel", - type: "INTEGER", - nullable: true); - - migrationBuilder.AddColumn( - name: "ChoiceTitle", - table: "BaseModel", - type: "TEXT", - maxLength: 500, - nullable: true); - - migrationBuilder.AddColumn( - name: "Choice_QuestionId", - table: "BaseModel", - type: "INTEGER", - nullable: true); - - migrationBuilder.AddColumn( - name: "Description", - table: "BaseModel", - type: "TEXT", - nullable: true); - - migrationBuilder.AddColumn( - name: "Discriminator", - table: "BaseModel", - type: "TEXT", - maxLength: 13, - nullable: false, - defaultValue: ""); - - migrationBuilder.AddColumn( - name: "Email", - table: "BaseModel", - type: "TEXT", - nullable: true); - - migrationBuilder.AddColumn( - name: "JoinDate", - table: "BaseModel", - type: "TEXT", - nullable: true); - - migrationBuilder.AddColumn( - name: "QuestionId", - table: "BaseModel", - type: "INTEGER", - nullable: true); - - migrationBuilder.AddColumn( - name: "QuestionId1", - table: "BaseModel", - type: "INTEGER", - nullable: true); - - migrationBuilder.AddColumn( - name: "QuizId", - table: "BaseModel", - type: "INTEGER", - nullable: true); - - migrationBuilder.AddColumn( - name: "QuizName", - table: "BaseModel", - type: "TEXT", - nullable: true); - - migrationBuilder.AddColumn( - name: "Score", - table: "BaseModel", - type: "INTEGER", - nullable: true); - - migrationBuilder.AddColumn( - name: "SubmissionId", - table: "BaseModel", - type: "INTEGER", - nullable: true); - - migrationBuilder.AddColumn( - name: "Submission_QuizId", - table: "BaseModel", - type: "INTEGER", - nullable: true); - - migrationBuilder.AddColumn( - name: "SubmittedAt", - table: "BaseModel", - type: "TEXT", - nullable: true); - - migrationBuilder.AddColumn( - name: "Title", - table: "BaseModel", - type: "TEXT", - nullable: true); - - migrationBuilder.AddColumn( - name: "UserId", - table: "BaseModel", - type: "INTEGER", - nullable: true); - - migrationBuilder.AddPrimaryKey( - name: "PK_BaseModel", - table: "BaseModel", - column: "Id"); - - migrationBuilder.CreateIndex( - name: "IX_BaseModel_Choice_QuestionId", - table: "BaseModel", - column: "Choice_QuestionId"); - - migrationBuilder.CreateIndex( - name: "IX_BaseModel_ChoiceId", - table: "BaseModel", - column: "ChoiceId"); - - migrationBuilder.CreateIndex( - name: "IX_BaseModel_QuestionId", - table: "BaseModel", - column: "QuestionId"); - - migrationBuilder.CreateIndex( - name: "IX_BaseModel_QuestionId1", - table: "BaseModel", - column: "QuestionId1"); - - migrationBuilder.CreateIndex( - name: "IX_BaseModel_QuizId", - table: "BaseModel", - column: "QuizId"); - - migrationBuilder.CreateIndex( - name: "IX_BaseModel_Submission_QuizId", - table: "BaseModel", - column: "Submission_QuizId"); - - migrationBuilder.CreateIndex( - name: "IX_BaseModel_SubmissionId", - table: "BaseModel", - column: "SubmissionId"); - - migrationBuilder.CreateIndex( - name: "IX_BaseModel_UserId", - table: "BaseModel", - column: "UserId"); - - migrationBuilder.AddForeignKey( - name: "FK_BaseModel_BaseModel_ChoiceId", - table: "BaseModel", - column: "ChoiceId", - principalTable: "BaseModel", - principalColumn: "Id", - onDelete: ReferentialAction.Restrict); - - migrationBuilder.AddForeignKey( - name: "FK_BaseModel_BaseModel_Choice_QuestionId", - table: "BaseModel", - column: "Choice_QuestionId", - principalTable: "BaseModel", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - - migrationBuilder.AddForeignKey( - name: "FK_BaseModel_BaseModel_QuestionId", - table: "BaseModel", - column: "QuestionId", - principalTable: "BaseModel", - principalColumn: "Id", - onDelete: ReferentialAction.Restrict); - - migrationBuilder.AddForeignKey( - name: "FK_BaseModel_BaseModel_QuestionId1", - table: "BaseModel", - column: "QuestionId1", - principalTable: "BaseModel", - principalColumn: "Id"); - - migrationBuilder.AddForeignKey( - name: "FK_BaseModel_BaseModel_QuizId", - table: "BaseModel", - column: "QuizId", - principalTable: "BaseModel", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - - migrationBuilder.AddForeignKey( - name: "FK_BaseModel_BaseModel_SubmissionId", - table: "BaseModel", - column: "SubmissionId", - principalTable: "BaseModel", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - - migrationBuilder.AddForeignKey( - name: "FK_BaseModel_BaseModel_Submission_QuizId", - table: "BaseModel", - column: "Submission_QuizId", - principalTable: "BaseModel", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - - migrationBuilder.AddForeignKey( - name: "FK_BaseModel_BaseModel_UserId", - table: "BaseModel", - column: "UserId", - principalTable: "BaseModel", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - } - - /// - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropForeignKey( - name: "FK_BaseModel_BaseModel_ChoiceId", - table: "BaseModel"); - - migrationBuilder.DropForeignKey( - name: "FK_BaseModel_BaseModel_Choice_QuestionId", - table: "BaseModel"); - - migrationBuilder.DropForeignKey( - name: "FK_BaseModel_BaseModel_QuestionId", - table: "BaseModel"); - - migrationBuilder.DropForeignKey( - name: "FK_BaseModel_BaseModel_QuestionId1", - table: "BaseModel"); - - migrationBuilder.DropForeignKey( - name: "FK_BaseModel_BaseModel_QuizId", - table: "BaseModel"); - - migrationBuilder.DropForeignKey( - name: "FK_BaseModel_BaseModel_SubmissionId", - table: "BaseModel"); - - migrationBuilder.DropForeignKey( - name: "FK_BaseModel_BaseModel_Submission_QuizId", - table: "BaseModel"); - - migrationBuilder.DropForeignKey( - name: "FK_BaseModel_BaseModel_UserId", - table: "BaseModel"); - - migrationBuilder.DropPrimaryKey( - name: "PK_BaseModel", - table: "BaseModel"); - - migrationBuilder.DropIndex( - name: "IX_BaseModel_Choice_QuestionId", - table: "BaseModel"); - - migrationBuilder.DropIndex( - name: "IX_BaseModel_ChoiceId", - table: "BaseModel"); - - migrationBuilder.DropIndex( - name: "IX_BaseModel_QuestionId", - table: "BaseModel"); - - migrationBuilder.DropIndex( - name: "IX_BaseModel_QuestionId1", - table: "BaseModel"); - - migrationBuilder.DropIndex( - name: "IX_BaseModel_QuizId", - table: "BaseModel"); - - migrationBuilder.DropIndex( - name: "IX_BaseModel_Submission_QuizId", - table: "BaseModel"); - - migrationBuilder.DropIndex( - name: "IX_BaseModel_SubmissionId", - table: "BaseModel"); - - migrationBuilder.DropIndex( - name: "IX_BaseModel_UserId", - table: "BaseModel"); - - migrationBuilder.DropColumn( - name: "ChoiceId", - table: "BaseModel"); - - migrationBuilder.DropColumn( - name: "ChoiceTitle", - table: "BaseModel"); - - migrationBuilder.DropColumn( - name: "Choice_QuestionId", - table: "BaseModel"); - - migrationBuilder.DropColumn( - name: "Description", - table: "BaseModel"); - - migrationBuilder.DropColumn( - name: "Discriminator", - table: "BaseModel"); - - migrationBuilder.DropColumn( - name: "Email", - table: "BaseModel"); - - migrationBuilder.DropColumn( - name: "JoinDate", - table: "BaseModel"); - - migrationBuilder.DropColumn( - name: "QuestionId", - table: "BaseModel"); - - migrationBuilder.DropColumn( - name: "QuestionId1", - table: "BaseModel"); - - migrationBuilder.DropColumn( - name: "QuizId", - table: "BaseModel"); - - migrationBuilder.DropColumn( - name: "QuizName", - table: "BaseModel"); - - migrationBuilder.DropColumn( - name: "Score", - table: "BaseModel"); - - migrationBuilder.DropColumn( - name: "SubmissionId", - table: "BaseModel"); - - migrationBuilder.DropColumn( - name: "Submission_QuizId", - table: "BaseModel"); - - migrationBuilder.DropColumn( - name: "SubmittedAt", - table: "BaseModel"); - - migrationBuilder.DropColumn( - name: "Title", - table: "BaseModel"); - - migrationBuilder.DropColumn( - name: "UserId", - table: "BaseModel"); - - migrationBuilder.RenameTable( - name: "BaseModel", - newName: "Users"); - - migrationBuilder.AlterColumn( - name: "Username", - table: "Users", - type: "TEXT", - nullable: false, - defaultValue: "", - oldClrType: typeof(string), - oldType: "TEXT", - oldNullable: true); - - migrationBuilder.AlterColumn( - name: "Role", - table: "Users", - type: "TEXT", - nullable: false, - defaultValue: "", - oldClrType: typeof(string), - oldType: "TEXT", - oldNullable: true); - - migrationBuilder.AddPrimaryKey( - name: "PK_Users", - table: "Users", - column: "Id"); - - migrationBuilder.CreateTable( - name: "Quizzes", - columns: table => new - { - Id = table.Column(type: "INTEGER", nullable: false) - .Annotation("Sqlite:Autoincrement", true), - QuizName = table.Column(type: "TEXT", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_Quizzes", x => x.Id); - }); - - migrationBuilder.CreateTable( - name: "Questions", - columns: table => new - { - Id = table.Column(type: "INTEGER", nullable: false) - .Annotation("Sqlite:Autoincrement", true), - QuizId = table.Column(type: "INTEGER", nullable: false), - Title = table.Column(type: "TEXT", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_Questions", x => x.Id); - table.ForeignKey( - name: "FK_Questions_Quizzes_QuizId", - column: x => x.QuizId, - principalTable: "Quizzes", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - }); - - migrationBuilder.CreateTable( - name: "Submissions", - columns: table => new - { - Id = table.Column(type: "INTEGER", nullable: false) - .Annotation("Sqlite:Autoincrement", true), - QuizId = table.Column(type: "INTEGER", nullable: false), - Score = table.Column(type: "INTEGER", nullable: false), - SubmittedAt = table.Column(type: "TEXT", nullable: false), - UserId = table.Column(type: "TEXT", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_Submissions", x => x.Id); - table.ForeignKey( - name: "FK_Submissions_Quizzes_QuizId", - column: x => x.QuizId, - principalTable: "Quizzes", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - }); - - migrationBuilder.CreateTable( - name: "Choices", - columns: table => new - { - Id = table.Column(type: "INTEGER", nullable: false) - .Annotation("Sqlite:Autoincrement", true), - QuestionId = table.Column(type: "INTEGER", nullable: false), - ChoiceTitle = table.Column(type: "TEXT", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_Choices", x => x.Id); - table.ForeignKey( - name: "FK_Choices_Questions_QuestionId", - column: x => x.QuestionId, - principalTable: "Questions", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - }); - - migrationBuilder.CreateTable( - name: "Answers", - columns: table => new - { - Id = table.Column(type: "INTEGER", nullable: false) - .Annotation("Sqlite:Autoincrement", true), - ChoiceId = table.Column(type: "INTEGER", nullable: false), - QuestionId = table.Column(type: "INTEGER", nullable: false), - SubmissionId = table.Column(type: "INTEGER", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_Answers", x => x.Id); - table.ForeignKey( - name: "FK_Answers_Choices_ChoiceId", - column: x => x.ChoiceId, - principalTable: "Choices", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - table.ForeignKey( - name: "FK_Answers_Questions_QuestionId", - column: x => x.QuestionId, - principalTable: "Questions", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - table.ForeignKey( - name: "FK_Answers_Submissions_SubmissionId", - column: x => x.SubmissionId, - principalTable: "Submissions", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - }); - - migrationBuilder.InsertData( - table: "Quizzes", - columns: new[] { "Id", "QuizName" }, - values: new object[,] - { - { 1, "General Knowledge" }, - { 2, "Science Trivia" } - }); - - migrationBuilder.InsertData( - table: "Questions", - columns: new[] { "Id", "QuizId", "Title" }, - values: new object[,] - { - { 1, 1, "What is the capital of France?" }, - { 2, 1, "Who wrote 'Romeo and Juliet'?" }, - { 3, 2, "What is the chemical symbol for water?" } - }); - - migrationBuilder.CreateIndex( - name: "IX_Answers_ChoiceId", - table: "Answers", - column: "ChoiceId"); - - migrationBuilder.CreateIndex( - name: "IX_Answers_QuestionId", - table: "Answers", - column: "QuestionId"); - - migrationBuilder.CreateIndex( - name: "IX_Answers_SubmissionId", - table: "Answers", - column: "SubmissionId"); - - migrationBuilder.CreateIndex( - name: "IX_Choices_QuestionId", - table: "Choices", - column: "QuestionId"); - - migrationBuilder.CreateIndex( - name: "IX_Questions_QuizId", - table: "Questions", - column: "QuizId"); - - migrationBuilder.CreateIndex( - name: "IX_Submissions_QuizId", - table: "Submissions", - column: "QuizId"); - } - } -} diff --git a/QuizApp.Database/Migrations/20241201101016_initdb.Designer.cs b/QuizApp.Database/Migrations/20250103100628_init.Designer.cs similarity index 63% rename from QuizApp.Database/Migrations/20241201101016_initdb.Designer.cs rename to QuizApp.Database/Migrations/20250103100628_init.Designer.cs index 438f4ec..3f0e0df 100644 --- a/QuizApp.Database/Migrations/20241201101016_initdb.Designer.cs +++ b/QuizApp.Database/Migrations/20250103100628_init.Designer.cs @@ -11,8 +11,8 @@ namespace QuizApp.Database.Migrations { [DbContext(typeof(QuizContext))] - [Migration("20241201101016_initdb")] - partial class initdb + [Migration("20250103100628_init")] + partial class init { /// protected override void BuildTargetModel(ModelBuilder modelBuilder) @@ -26,44 +26,21 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) .ValueGeneratedOnAdd() .HasColumnType("INTEGER"); - b.Property("ChoiceId") + b.Property("IsCorrect") .HasColumnType("INTEGER"); b.Property("QuestionId") .HasColumnType("INTEGER"); - b.Property("SubmissionId") - .HasColumnType("INTEGER"); - - b.HasKey("Id"); - - b.HasIndex("ChoiceId"); - - b.HasIndex("QuestionId"); - - b.HasIndex("SubmissionId"); - - b.ToTable("Answers"); - }); - - modelBuilder.Entity("QuizApp.Database.Models.Choice", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - - b.Property("ChoiceTitle") + b.Property("Text") .IsRequired() .HasColumnType("TEXT"); - b.Property("QuestionId") - .HasColumnType("INTEGER"); - b.HasKey("Id"); b.HasIndex("QuestionId"); - b.ToTable("Choices"); + b.ToTable("Answers"); }); modelBuilder.Entity("QuizApp.Database.Models.Question", b => @@ -84,26 +61,6 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) b.HasIndex("QuizId"); b.ToTable("Questions"); - - b.HasData( - new - { - Id = 1, - QuizId = 1, - Title = "What is the capital of France?" - }, - new - { - Id = 2, - QuizId = 1, - Title = "Who wrote 'Romeo and Juliet'?" - }, - new - { - Id = 3, - QuizId = 2, - Title = "What is the chemical symbol for water?" - }); }); modelBuilder.Entity("QuizApp.Database.Models.Quiz", b => @@ -112,6 +69,10 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) .ValueGeneratedOnAdd() .HasColumnType("INTEGER"); + b.Property("Description") + .IsRequired() + .HasColumnType("TEXT"); + b.Property("QuizName") .IsRequired() .HasColumnType("TEXT"); @@ -119,18 +80,6 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) b.HasKey("Id"); b.ToTable("Quizzes"); - - b.HasData( - new - { - Id = 1, - QuizName = "General Knowledge" - }, - new - { - Id = 2, - QuizName = "Science Trivia" - }); }); modelBuilder.Entity("QuizApp.Database.Models.Submission", b => @@ -139,23 +88,24 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) .ValueGeneratedOnAdd() .HasColumnType("INTEGER"); + b.Property("AttemptDate") + .HasColumnType("TEXT"); + b.Property("QuizId") .HasColumnType("INTEGER"); b.Property("Score") .HasColumnType("INTEGER"); - b.Property("SubmittedAt") - .HasColumnType("TEXT"); - - b.Property("UserId") - .IsRequired() - .HasColumnType("TEXT"); + b.Property("UserId") + .HasColumnType("INTEGER"); b.HasKey("Id"); b.HasIndex("QuizId"); + b.HasIndex("UserId"); + b.ToTable("Submissions"); }); @@ -165,6 +115,13 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) .ValueGeneratedOnAdd() .HasColumnType("INTEGER"); + b.Property("Email") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("JoinDate") + .HasColumnType("TEXT"); + b.Property("Role") .IsRequired() .HasColumnType("TEXT"); @@ -180,35 +137,8 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) modelBuilder.Entity("QuizApp.Database.Models.Answer", b => { - b.HasOne("QuizApp.Database.Models.Choice", "Choice") - .WithMany() - .HasForeignKey("ChoiceId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - b.HasOne("QuizApp.Database.Models.Question", "Question") - .WithMany() - .HasForeignKey("QuestionId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("QuizApp.Database.Models.Submission", "Submission") .WithMany("Answers") - .HasForeignKey("SubmissionId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("Choice"); - - b.Navigation("Question"); - - b.Navigation("Submission"); - }); - - modelBuilder.Entity("QuizApp.Database.Models.Choice", b => - { - b.HasOne("QuizApp.Database.Models.Question", "Question") - .WithMany() .HasForeignKey("QuestionId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); @@ -219,7 +149,7 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) modelBuilder.Entity("QuizApp.Database.Models.Question", b => { b.HasOne("QuizApp.Database.Models.Quiz", "Quiz") - .WithMany() + .WithMany("Questions") .HasForeignKey("QuizId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); @@ -230,18 +160,38 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) modelBuilder.Entity("QuizApp.Database.Models.Submission", b => { b.HasOne("QuizApp.Database.Models.Quiz", "Quiz") - .WithMany() + .WithMany("Submissions") .HasForeignKey("QuizId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); + b.HasOne("QuizApp.Database.Models.User", "User") + .WithMany("Submissions") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + b.Navigation("Quiz"); + + b.Navigation("User"); }); - modelBuilder.Entity("QuizApp.Database.Models.Submission", b => + modelBuilder.Entity("QuizApp.Database.Models.Question", b => { b.Navigation("Answers"); }); + + modelBuilder.Entity("QuizApp.Database.Models.Quiz", b => + { + b.Navigation("Questions"); + + b.Navigation("Submissions"); + }); + + modelBuilder.Entity("QuizApp.Database.Models.User", b => + { + b.Navigation("Submissions"); + }); #pragma warning restore 612, 618 } } diff --git a/QuizApp.Database/Migrations/20241201101016_initdb.cs b/QuizApp.Database/Migrations/20250103100628_init.cs similarity index 62% rename from QuizApp.Database/Migrations/20241201101016_initdb.cs rename to QuizApp.Database/Migrations/20250103100628_init.cs index 524c30a..4e08c1b 100644 --- a/QuizApp.Database/Migrations/20241201101016_initdb.cs +++ b/QuizApp.Database/Migrations/20250103100628_init.cs @@ -3,12 +3,10 @@ #nullable disable -#pragma warning disable CA1814 // Prefer jagged arrays over multidimensional - namespace QuizApp.Database.Migrations { /// - public partial class initdb : Migration + public partial class init : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) @@ -19,7 +17,8 @@ protected override void Up(MigrationBuilder migrationBuilder) { Id = table.Column(type: "INTEGER", nullable: false) .Annotation("Sqlite:Autoincrement", true), - QuizName = table.Column(type: "TEXT", nullable: false) + QuizName = table.Column(type: "TEXT", nullable: false), + Description = table.Column(type: "TEXT", nullable: false) }, constraints: table => { @@ -33,7 +32,9 @@ protected override void Up(MigrationBuilder migrationBuilder) Id = table.Column(type: "INTEGER", nullable: false) .Annotation("Sqlite:Autoincrement", true), Username = table.Column(type: "TEXT", nullable: false), - Role = table.Column(type: "TEXT", nullable: false) + Role = table.Column(type: "TEXT", nullable: false), + Email = table.Column(type: "TEXT", nullable: false), + JoinDate = table.Column(type: "TEXT", nullable: false) }, constraints: table => { @@ -46,8 +47,8 @@ protected override void Up(MigrationBuilder migrationBuilder) { Id = table.Column(type: "INTEGER", nullable: false) .Annotation("Sqlite:Autoincrement", true), - QuizId = table.Column(type: "INTEGER", nullable: false), - Title = table.Column(type: "TEXT", nullable: false) + Title = table.Column(type: "TEXT", nullable: false), + QuizId = table.Column(type: "INTEGER", nullable: false) }, constraints: table => { @@ -66,10 +67,10 @@ protected override void Up(MigrationBuilder migrationBuilder) { Id = table.Column(type: "INTEGER", nullable: false) .Annotation("Sqlite:Autoincrement", true), + UserId = table.Column(type: "INTEGER", nullable: false), QuizId = table.Column(type: "INTEGER", nullable: false), - Score = table.Column(type: "INTEGER", nullable: false), - UserId = table.Column(type: "TEXT", nullable: false), - SubmittedAt = table.Column(type: "TEXT", nullable: false) + AttemptDate = table.Column(type: "TEXT", nullable: false), + Score = table.Column(type: "INTEGER", nullable: false) }, constraints: table => { @@ -80,24 +81,10 @@ protected override void Up(MigrationBuilder migrationBuilder) principalTable: "Quizzes", principalColumn: "Id", onDelete: ReferentialAction.Cascade); - }); - - migrationBuilder.CreateTable( - name: "Choices", - columns: table => new - { - Id = table.Column(type: "INTEGER", nullable: false) - .Annotation("Sqlite:Autoincrement", true), - ChoiceTitle = table.Column(type: "TEXT", nullable: false), - QuestionId = table.Column(type: "INTEGER", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_Choices", x => x.Id); table.ForeignKey( - name: "FK_Choices_Questions_QuestionId", - column: x => x.QuestionId, - principalTable: "Questions", + name: "FK_Submissions_Users_UserId", + column: x => x.UserId, + principalTable: "Users", principalColumn: "Id", onDelete: ReferentialAction.Cascade); }); @@ -108,72 +95,26 @@ protected override void Up(MigrationBuilder migrationBuilder) { Id = table.Column(type: "INTEGER", nullable: false) .Annotation("Sqlite:Autoincrement", true), - QuestionId = table.Column(type: "INTEGER", nullable: false), - ChoiceId = table.Column(type: "INTEGER", nullable: false), - SubmissionId = table.Column(type: "INTEGER", nullable: false) + Text = table.Column(type: "TEXT", nullable: false), + IsCorrect = table.Column(type: "INTEGER", nullable: false), + QuestionId = table.Column(type: "INTEGER", nullable: false) }, constraints: table => { table.PrimaryKey("PK_Answers", x => x.Id); - table.ForeignKey( - name: "FK_Answers_Choices_ChoiceId", - column: x => x.ChoiceId, - principalTable: "Choices", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); table.ForeignKey( name: "FK_Answers_Questions_QuestionId", column: x => x.QuestionId, principalTable: "Questions", principalColumn: "Id", onDelete: ReferentialAction.Cascade); - table.ForeignKey( - name: "FK_Answers_Submissions_SubmissionId", - column: x => x.SubmissionId, - principalTable: "Submissions", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - }); - - migrationBuilder.InsertData( - table: "Quizzes", - columns: new[] { "Id", "QuizName" }, - values: new object[,] - { - { 1, "General Knowledge" }, - { 2, "Science Trivia" } }); - migrationBuilder.InsertData( - table: "Questions", - columns: new[] { "Id", "QuizId", "Title" }, - values: new object[,] - { - { 1, 1, "What is the capital of France?" }, - { 2, 1, "Who wrote 'Romeo and Juliet'?" }, - { 3, 2, "What is the chemical symbol for water?" } - }); - - migrationBuilder.CreateIndex( - name: "IX_Answers_ChoiceId", - table: "Answers", - column: "ChoiceId"); - migrationBuilder.CreateIndex( name: "IX_Answers_QuestionId", table: "Answers", column: "QuestionId"); - migrationBuilder.CreateIndex( - name: "IX_Answers_SubmissionId", - table: "Answers", - column: "SubmissionId"); - - migrationBuilder.CreateIndex( - name: "IX_Choices_QuestionId", - table: "Choices", - column: "QuestionId"); - migrationBuilder.CreateIndex( name: "IX_Questions_QuizId", table: "Questions", @@ -183,6 +124,11 @@ protected override void Up(MigrationBuilder migrationBuilder) name: "IX_Submissions_QuizId", table: "Submissions", column: "QuizId"); + + migrationBuilder.CreateIndex( + name: "IX_Submissions_UserId", + table: "Submissions", + column: "UserId"); } /// @@ -191,18 +137,15 @@ protected override void Down(MigrationBuilder migrationBuilder) migrationBuilder.DropTable( name: "Answers"); - migrationBuilder.DropTable( - name: "Users"); - - migrationBuilder.DropTable( - name: "Choices"); - migrationBuilder.DropTable( name: "Submissions"); migrationBuilder.DropTable( name: "Questions"); + migrationBuilder.DropTable( + name: "Users"); + migrationBuilder.DropTable( name: "Quizzes"); } diff --git a/QuizApp.Database/Migrations/QuizContextModelSnapshot.cs b/QuizApp.Database/Migrations/QuizContextModelSnapshot.cs index c3d01d8..258b6c8 100644 --- a/QuizApp.Database/Migrations/QuizContextModelSnapshot.cs +++ b/QuizApp.Database/Migrations/QuizContextModelSnapshot.cs @@ -17,79 +17,34 @@ protected override void BuildModel(ModelBuilder modelBuilder) #pragma warning disable 612, 618 modelBuilder.HasAnnotation("ProductVersion", "9.0.0"); - modelBuilder.Entity("QuizApp.Database.Models.BaseModel", b => + modelBuilder.Entity("QuizApp.Database.Models.Answer", b => { b.Property("Id") .ValueGeneratedOnAdd() .HasColumnType("INTEGER"); - b.Property("Discriminator") - .IsRequired() - .HasMaxLength(13) - .HasColumnType("TEXT"); - - b.HasKey("Id"); - - b.ToTable("BaseModel"); - - b.HasDiscriminator().HasValue("BaseModel"); - - b.UseTphMappingStrategy(); - }); - - modelBuilder.Entity("QuizApp.Database.Models.Answer", b => - { - b.HasBaseType("QuizApp.Database.Models.BaseModel"); - - b.Property("ChoiceId") + b.Property("IsCorrect") .HasColumnType("INTEGER"); b.Property("QuestionId") .HasColumnType("INTEGER"); - b.Property("QuestionId1") - .HasColumnType("INTEGER"); - - b.Property("SubmissionId") - .HasColumnType("INTEGER"); - - b.HasIndex("ChoiceId"); - - b.HasIndex("QuestionId"); - - b.HasIndex("QuestionId1"); - - b.HasIndex("SubmissionId"); - - b.HasDiscriminator().HasValue("Answer"); - }); - - modelBuilder.Entity("QuizApp.Database.Models.Choice", b => - { - b.HasBaseType("QuizApp.Database.Models.BaseModel"); - - b.Property("ChoiceTitle") + b.Property("Text") .IsRequired() - .HasMaxLength(500) .HasColumnType("TEXT"); - b.Property("QuestionId") - .HasColumnType("INTEGER"); + b.HasKey("Id"); b.HasIndex("QuestionId"); - b.ToTable("BaseModel", t => - { - t.Property("QuestionId") - .HasColumnName("Choice_QuestionId"); - }); - - b.HasDiscriminator().HasValue("Choice"); + b.ToTable("Answers"); }); modelBuilder.Entity("QuizApp.Database.Models.Question", b => { - b.HasBaseType("QuizApp.Database.Models.BaseModel"); + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); b.Property("QuizId") .HasColumnType("INTEGER"); @@ -98,14 +53,18 @@ protected override void BuildModel(ModelBuilder modelBuilder) .IsRequired() .HasColumnType("TEXT"); + b.HasKey("Id"); + b.HasIndex("QuizId"); - b.HasDiscriminator().HasValue("Question"); + b.ToTable("Questions"); }); modelBuilder.Entity("QuizApp.Database.Models.Quiz", b => { - b.HasBaseType("QuizApp.Database.Models.BaseModel"); + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); b.Property("Description") .IsRequired() @@ -115,12 +74,19 @@ protected override void BuildModel(ModelBuilder modelBuilder) .IsRequired() .HasColumnType("TEXT"); - b.HasDiscriminator().HasValue("Quiz"); + b.HasKey("Id"); + + b.ToTable("Quizzes"); }); modelBuilder.Entity("QuizApp.Database.Models.Submission", b => { - b.HasBaseType("QuizApp.Database.Models.BaseModel"); + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("AttemptDate") + .HasColumnType("TEXT"); b.Property("QuizId") .HasColumnType("INTEGER"); @@ -128,28 +94,23 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Property("Score") .HasColumnType("INTEGER"); - b.Property("SubmittedAt") - .HasColumnType("TEXT"); - b.Property("UserId") .HasColumnType("INTEGER"); + b.HasKey("Id"); + b.HasIndex("QuizId"); b.HasIndex("UserId"); - b.ToTable("BaseModel", t => - { - t.Property("QuizId") - .HasColumnName("Submission_QuizId"); - }); - - b.HasDiscriminator().HasValue("Submission"); + b.ToTable("Submissions"); }); modelBuilder.Entity("QuizApp.Database.Models.User", b => { - b.HasBaseType("QuizApp.Database.Models.BaseModel"); + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); b.Property("Email") .IsRequired() @@ -166,44 +127,15 @@ protected override void BuildModel(ModelBuilder modelBuilder) .IsRequired() .HasColumnType("TEXT"); - b.HasDiscriminator().HasValue("User"); + b.HasKey("Id"); + + b.ToTable("Users"); }); modelBuilder.Entity("QuizApp.Database.Models.Answer", b => { - b.HasOne("QuizApp.Database.Models.Choice", "Choice") - .WithMany() - .HasForeignKey("ChoiceId") - .OnDelete(DeleteBehavior.Restrict) - .IsRequired(); - b.HasOne("QuizApp.Database.Models.Question", "Question") - .WithMany() - .HasForeignKey("QuestionId") - .OnDelete(DeleteBehavior.Restrict) - .IsRequired(); - - b.HasOne("QuizApp.Database.Models.Question", null) - .WithMany("Answers") - .HasForeignKey("QuestionId1"); - - b.HasOne("QuizApp.Database.Models.Submission", "Submission") .WithMany("Answers") - .HasForeignKey("SubmissionId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("Choice"); - - b.Navigation("Question"); - - b.Navigation("Submission"); - }); - - modelBuilder.Entity("QuizApp.Database.Models.Choice", b => - { - b.HasOne("QuizApp.Database.Models.Question", "Question") - .WithMany() .HasForeignKey("QuestionId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); @@ -225,7 +157,7 @@ protected override void BuildModel(ModelBuilder modelBuilder) modelBuilder.Entity("QuizApp.Database.Models.Submission", b => { b.HasOne("QuizApp.Database.Models.Quiz", "Quiz") - .WithMany() + .WithMany("Submissions") .HasForeignKey("QuizId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); @@ -249,11 +181,8 @@ protected override void BuildModel(ModelBuilder modelBuilder) modelBuilder.Entity("QuizApp.Database.Models.Quiz", b => { b.Navigation("Questions"); - }); - modelBuilder.Entity("QuizApp.Database.Models.Submission", b => - { - b.Navigation("Answers"); + b.Navigation("Submissions"); }); modelBuilder.Entity("QuizApp.Database.Models.User", b => diff --git a/QuizApp.Database/Models/Answer.cs b/QuizApp.Database/Models/Answer.cs index 25a6bf1..0b7d380 100644 --- a/QuizApp.Database/Models/Answer.cs +++ b/QuizApp.Database/Models/Answer.cs @@ -2,12 +2,8 @@ namespace QuizApp.Database.Models; public class Answer : BaseModel { + public string Text { get; set; } = string.Empty; + public bool IsCorrect { get; set; } public int QuestionId { get; set; } public Question Question { get; set; } = null!; - - public int ChoiceId { get; set; } - public Choice Choice { get; set; } = null!; - - public int SubmissionId { get; set; } - public Submission Submission { get; set; } = null!; } \ No newline at end of file diff --git a/QuizApp.Database/Models/Choice.cs b/QuizApp.Database/Models/Choice.cs deleted file mode 100644 index 5e1536a..0000000 --- a/QuizApp.Database/Models/Choice.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace QuizApp.Database.Models; - -public class Choice : BaseModel -{ - public string ChoiceTitle { get; set; } = string.Empty; - - public int QuestionId { get; set; } - public Question Question { get; set; } = null!; -} \ No newline at end of file diff --git a/QuizApp.Database/Models/Question.cs b/QuizApp.Database/Models/Question.cs index 8edcae8..5240c5e 100644 --- a/QuizApp.Database/Models/Question.cs +++ b/QuizApp.Database/Models/Question.cs @@ -2,9 +2,8 @@ namespace QuizApp.Database.Models; public class Question : BaseModel { + public string Title { get; set; } = string.Empty; public int QuizId { get; set; } public Quiz Quiz { get; set; } = null!; - - public string Title { get; set; } = string.Empty; - public ICollection Answers { get; set; } + public ICollection Answers { get; set; } = new List(); } \ No newline at end of file diff --git a/QuizApp.Database/Models/Quiz.cs b/QuizApp.Database/Models/Quiz.cs index 9b5ea0a..6b857fd 100644 --- a/QuizApp.Database/Models/Quiz.cs +++ b/QuizApp.Database/Models/Quiz.cs @@ -2,7 +2,8 @@ namespace QuizApp.Database.Models; public class Quiz : BaseModel { - public string QuizName { get; set; } - public string Description { get; set; } - public ICollection Questions { get; set; } + public string QuizName { get; set; } = string.Empty; + public string Description { get; set; } = string.Empty; + public ICollection Questions { get; set; } = new List(); + public ICollection Submissions { get; set; } = new List(); } \ No newline at end of file diff --git a/QuizApp.Database/Models/Submission.cs b/QuizApp.Database/Models/Submission.cs index 10328e5..6fc92bb 100644 --- a/QuizApp.Database/Models/Submission.cs +++ b/QuizApp.Database/Models/Submission.cs @@ -2,13 +2,10 @@ namespace QuizApp.Database.Models; public class Submission : BaseModel { - public int QuizId { get; set; } - public Quiz Quiz { get; set; } = null!; - public int UserId { get; set; } public User User { get; set; } = null!; - - public ICollection Answers { get; set; } = new List(); + public int QuizId { get; set; } + public Quiz Quiz { get; set; } = null!; + public DateTime AttemptDate { get; set; } public int Score { get; set; } - public DateTime SubmittedAt { get; set; } } \ No newline at end of file diff --git a/QuizApp.Database/QuizContext.cs b/QuizApp.Database/QuizContext.cs index afec46f..ab3dce3 100644 --- a/QuizApp.Database/QuizContext.cs +++ b/QuizApp.Database/QuizContext.cs @@ -13,98 +13,23 @@ public QuizContext(DbContextOptions options) public DbSet Quizzes { get; set; } = null!; public DbSet Questions { get; set; } = null!; - public DbSet Choices { get; set; } = null!; public DbSet Submissions { get; set; } = null!; public DbSet Answers { get; set; } = null!; public DbSet Users { get; set; } = null!; protected override void OnModelCreating(ModelBuilder modelBuilder) { - // BaseModel Configuration - modelBuilder.Entity() - .HasKey(b => b.Id); - - // User Configuration - modelBuilder.Entity() - .HasMany(u => u.Submissions) - .WithOne(s => s.User) - .HasForeignKey(s => s.UserId) - .OnDelete(DeleteBehavior.Cascade); - - // Quiz Configuration - modelBuilder.Entity() - .HasMany(q => q.Questions) - .WithOne(q => q.Quiz) - .HasForeignKey(q => q.QuizId) - .OnDelete(DeleteBehavior.Cascade); - - // Question Configuration - modelBuilder.Entity() - .HasMany(q => q.Answers) - .WithOne(c => c.Question) - .HasForeignKey(c => c.QuestionId) - .OnDelete(DeleteBehavior.Cascade); - - // Choice Configuration - modelBuilder.Entity() - .Property(c => c.ChoiceTitle) - .IsRequired() - .HasMaxLength(500); // Example length limit, adjust as needed - - // Submission Configuration modelBuilder.Entity() - .HasMany(s => s.Answers) - .WithOne(a => a.Submission) - .HasForeignKey(a => a.SubmissionId) - .OnDelete(DeleteBehavior.Cascade); - - // Answer Configuration - modelBuilder.Entity() - .HasOne(a => a.Question) - .WithMany() - .HasForeignKey(a => a.QuestionId) - .OnDelete(DeleteBehavior.Restrict); // Prevent deletion of Question if Answer exists + .HasKey(uq => uq.Id); - modelBuilder.Entity() - .HasOne(a => a.Choice) - .WithMany() - .HasForeignKey(a => a.ChoiceId) - .OnDelete(DeleteBehavior.Restrict); // Prevent deletion of Choice if Answer exists + modelBuilder.Entity() + .HasOne(uq => uq.User) + .WithMany(u => u.Submissions) + .HasForeignKey(uq => uq.UserId); - //modelBuilder.Entity().HasData( - // new Quiz - // { - // Id = 1, - // QuizName = "General Knowledge", - // Description = "GK" - // }, - // new Quiz - // { - // Id = 2, - // QuizName = "Science Trivia", - // Description = "ST", - // } - //); - - //modelBuilder.Entity().HasData( - // new Question - // { - // Id = 1, - // QuizId = 1, - // Title = "What is the capital of France?", - // }, - // new Question - // { - // Id = 2, - // QuizId = 1, - // Title = "Who wrote 'Romeo and Juliet'?", - // }, - // new Question - // { - // Id = 3, - // QuizId = 2, - // Title = "What is the chemical symbol for water?", - // } - //); + modelBuilder.Entity() + .HasOne(uq => uq.Quiz) + .WithMany(q => q.Submissions) + .HasForeignKey(uq => uq.QuizId); } } \ No newline at end of file diff --git a/QuizApp.Database/Repositories/IQuizRepository.cs b/QuizApp.Database/Repositories/IQuizRepository.cs index 8f39285..9dd9e2e 100644 --- a/QuizApp.Database/Repositories/IQuizRepository.cs +++ b/QuizApp.Database/Repositories/IQuizRepository.cs @@ -3,5 +3,5 @@ namespace QuizApp.Database.Repositories; public interface IQuizRepository { Task CreateQuiz(string name); - Task CreateQuestion(int quizId, string question); + Task CreateQuestion(int quizId, string question, List<(string text, bool isCorrect)> answers); } \ No newline at end of file diff --git a/QuizApp.Database/Repositories/QuizRepository.cs b/QuizApp.Database/Repositories/QuizRepository.cs index 0cdfd68..9f74ecc 100644 --- a/QuizApp.Database/Repositories/QuizRepository.cs +++ b/QuizApp.Database/Repositories/QuizRepository.cs @@ -1,3 +1,4 @@ +using Microsoft.EntityFrameworkCore; using QuizApp.Database.Models; namespace QuizApp.Database.Repositories; @@ -23,25 +24,35 @@ public async Task CreateQuiz(string name) return model.Entity.Id; } - public async Task CreateQuestion(int quizId, string question) + public async Task CreateQuestion(int quizId, string question, List<(string text, bool isCorrect)> answers) { var quizModel = db.Quizzes.SingleOrDefault(x => x.Id == quizId); if (quizModel is null) { - throw new InvalidOperationException($"Cannot found quiz id = {quizId}"); + throw new InvalidOperationException($"Cannot find quiz with id = {quizId}"); } var questionModel = new Question { QuizId = quizModel.Id, - Quiz = quizModel, + Quiz = quizModel, Title = question, }; - - var result = db.Questions.Add(questionModel); - + + foreach (var (text, isCorrect) in answers) + { + var answer = new Answer + { + Text = text, + IsCorrect = isCorrect, + Question = questionModel // Automatically sets the QuestionId due to EF Core navigation properties + }; + questionModel.Answers.Add(answer); + } + + db.Questions.Add(questionModel); await db.SaveChangesAsync(); - - return result.Entity.Id; + + return questionModel.Id; } } \ No newline at end of file diff --git a/QuizApp.Test/QuizRepositoryTest.cs b/QuizApp.Test/QuizRepositoryTest.cs index 951ae99..1f9c519 100644 --- a/QuizApp.Test/QuizRepositoryTest.cs +++ b/QuizApp.Test/QuizRepositoryTest.cs @@ -3,6 +3,7 @@ using Xunit; using FluentAssertions; using QuizApp.Database.Models; +using Microsoft.EntityFrameworkCore; namespace QuizApp.Test; @@ -32,11 +33,11 @@ public async Task CreateQuizOK() } [Fact] - public async Task CreateQuestionOK() + public async Task CreateQuestionWithAnswer() { // Arrange var quizId = 1; - var question = "Who create macos?"; + var questionTitle = "What is the capital of France?"; using (var setupContext = new QuizContext(Options)) { // Ensure the database is clean @@ -47,19 +48,28 @@ public async Task CreateQuestionOK() setupContext.Quizzes.Add(new Quiz { Id = quizId, - QuizName = "Technology Quiz", - Description = "A quiz about technology.", + QuizName = "General Knowledge", + Description = "GK", }); await setupContext.SaveChangesAsync(); } + var answers = new List<(string Text, bool IsCorrect)> + { + ("Paris", true), + ("London", false), + ("Berlin", false), + ("Madrid", false) + }; + // Act - var result = await quizRepository.CreateQuestion(quizId, question); - + var result = await quizRepository.CreateQuestion(quizId, questionTitle, answers); + // Assert result.Should().NotBe(0); using var context = new QuizContext(Options); var model = context.Questions.SingleOrDefault(s => s.Id == 4); - model?.Title.Should().Be(question); + model?.Title.Should().Be(questionTitle); + context.Answers.Count().Should().Be(4); } } \ No newline at end of file