From 391a645d9a43ace05ed83edc2bafba275fa691f9 Mon Sep 17 00:00:00 2001 From: Viet Hoang Date: Wed, 8 Jan 2025 12:27:53 +0800 Subject: [PATCH] init --- BlazorQuizApp.Client/Pages/Home.razor | 9 ++- BlazorQuizApp.Client/Pages/TakeQuiz.razor | 70 +++++++++++++++++++ QuizApp.Common/DTO/AnswerDTO.cs | 1 + QuizApp.Common/DTO/QuestionDTO.cs | 1 + .../Repositories/QuizRepository.cs | 7 +- 5 files changed, 83 insertions(+), 5 deletions(-) create mode 100644 BlazorQuizApp.Client/Pages/TakeQuiz.razor diff --git a/BlazorQuizApp.Client/Pages/Home.razor b/BlazorQuizApp.Client/Pages/Home.razor index 908172b..986330d 100644 --- a/BlazorQuizApp.Client/Pages/Home.razor +++ b/BlazorQuizApp.Client/Pages/Home.razor @@ -4,6 +4,7 @@ @using System.Collections.ObjectModel @using System.Security.Claims @inject DialogService DialogService; +@inject NavigationManager NavigationManager @inject IQuizApiClient apiClient Home @@ -13,7 +14,7 @@

@quiz.Name

@quiz.Description - Take Quiz + Take Quiz
@@ -28,7 +29,7 @@ bool sidebar1Expanded = true; - private async Task TakeQuiz() + private async Task TakeQuiz(int quizId) { if (authenticationState is not null) { @@ -62,6 +63,10 @@ await DialogService.Alert("User creation failed"); } } + else + { + NavigationManager.NavigateTo($"/take-quiz/{quizId}"); + } } } } diff --git a/BlazorQuizApp.Client/Pages/TakeQuiz.razor b/BlazorQuizApp.Client/Pages/TakeQuiz.razor new file mode 100644 index 0000000..7967e3b --- /dev/null +++ b/BlazorQuizApp.Client/Pages/TakeQuiz.razor @@ -0,0 +1,70 @@ +@page "/take-quiz/{quizId:int}" +@using QuizApp.Api +@using QuizApp.Common.DTO +@using QuizApp.Common.Request +@using System.Collections.ObjectModel +@inject IQuizApiClient apiClient +@inject DialogService DialogService +@inject NotificationService NotificationService +@inject NavigationManager NavigationManager +@attribute [StreamRendering] +@attribute [Authorize] + +@if (Questions == null) +{ +

Loading...

+} +else +{ + + + + + Submit +} + +@code { + [Parameter] public int QuizId { get; set; } + + private List Questions = new List(); + private List UserAnswers = new List(); + + + private async Task SubmitQuiz() + { + + } + + protected override async Task OnAfterRenderAsync(bool firstRender) + { + if (firstRender) + { + var questionDTOs = await apiClient.GetQuestionsByQuizIdEndpointAsync(QuizId); + + if (questionDTOs == null) + { + return; + } + + if (questionDTOs.Any()) + { + Questions = new List(questionDTOs); + foreach(var a in Questions.SelectMany(s=>s.Answers)) + { + a.IsCorrect = false; + } + } + else + { + Questions = new List(); + } + + StateHasChanged(); + } + } +} diff --git a/QuizApp.Common/DTO/AnswerDTO.cs b/QuizApp.Common/DTO/AnswerDTO.cs index a83e6fb..7840613 100644 --- a/QuizApp.Common/DTO/AnswerDTO.cs +++ b/QuizApp.Common/DTO/AnswerDTO.cs @@ -3,6 +3,7 @@ public class AnswerDTO { public int Id { get; set; } + public int QuestionId { get; set; } public string Text { get; set; } = string.Empty; public bool IsCorrect { get; set; } } diff --git a/QuizApp.Common/DTO/QuestionDTO.cs b/QuizApp.Common/DTO/QuestionDTO.cs index 068797b..4cf62fd 100644 --- a/QuizApp.Common/DTO/QuestionDTO.cs +++ b/QuizApp.Common/DTO/QuestionDTO.cs @@ -6,4 +6,5 @@ public class QuestionDTO public int Id { get; set; } public string Title { get; set; } = string.Empty; public IEnumerable Answers { get; set; } = new List(); + public int SelectedAnswerId { get; set; } } diff --git a/QuizApp.Database/Repositories/QuizRepository.cs b/QuizApp.Database/Repositories/QuizRepository.cs index 00b97d5..a0bca38 100644 --- a/QuizApp.Database/Repositories/QuizRepository.cs +++ b/QuizApp.Database/Repositories/QuizRepository.cs @@ -7,7 +7,7 @@ namespace QuizApp.Database.Repositories; public class QuizRepository : IQuizRepository { private readonly QuizContext db; - + public QuizRepository(QuizContext db) { this.db = db; @@ -291,10 +291,10 @@ public async Task DeleteQuiz(int quizId) public async Task> GetQuestionsByQuizId(int quizId) { var questions = await db.Questions.Where(q => q.QuizId == quizId) - .Include(q => q.Answers) + .Include(q => q.Answers) .ToListAsync(); - if(questions.Count == 0) + if (questions.Count == 0) { return Enumerable.Empty(); } @@ -308,6 +308,7 @@ public async Task> GetQuestionsByQuizId(int quizId) { Id = a.Id, Text = a.Text, + QuestionId = q.Id, IsCorrect = a.IsCorrect }).ToList() });