Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show answers in take quiz questions #90

Merged
merged 1 commit into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions BlazorQuizApp.Client/Pages/Home.razor
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
@using System.Collections.ObjectModel
@using System.Security.Claims
@inject DialogService DialogService;
@inject NavigationManager NavigationManager
@inject IQuizApiClient apiClient
<PageTitle>Home</PageTitle>

Expand All @@ -13,7 +14,7 @@
<RadzenStack Orientation="Orientation.Vertical" JustifyContent="JustifyContent.Left" AlignItems="AlignItems.Start">
<h3>@quiz.Name</h3>
<RadzenText>@quiz.Description</RadzenText>
<RadzenButton Click="TakeQuiz">Take Quiz</RadzenButton>
<RadzenButton Click="@(() => TakeQuiz(quiz.Id.Value))">Take Quiz</RadzenButton>
</RadzenStack>
</RadzenCard>
</Template>
Expand All @@ -28,7 +29,7 @@

bool sidebar1Expanded = true;

private async Task TakeQuiz()
private async Task TakeQuiz(int quizId)
{
if (authenticationState is not null)
{
Expand Down Expand Up @@ -62,6 +63,10 @@
await DialogService.Alert("User creation failed");
}
}
else
{
NavigationManager.NavigateTo($"/take-quiz/{quizId}");
}
}
}
}
Expand Down
70 changes: 70 additions & 0 deletions BlazorQuizApp.Client/Pages/TakeQuiz.razor
Original file line number Diff line number Diff line change
@@ -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)
{
<p>Loading...</p>
}
else
{
<RadzenDataList Data="@Questions" TItem="QuestionDTO">
<Template Context="question">
<RadzenStack Orientation="Orientation.Vertical" JustifyContent="JustifyContent.Left" AlignItems="AlignItems.Start">
<h3>@question.Title</h3>
<RadzenRadioButtonList Data="@question.Answers" Value="@question.SelectedAnswerId" TValue="int" TextProperty="Text" ValueProperty="Id" Orientation="Orientation.Vertical" />
</RadzenStack>
</Template>
</RadzenDataList>

<RadzenButton Click="SubmitQuiz" Style="margin-left: 15px">Submit</RadzenButton>
}

@code {
[Parameter] public int QuizId { get; set; }

private List<QuestionDTO> Questions = new List<QuestionDTO>();
private List<AnswerDTO> UserAnswers = new List<AnswerDTO>();


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<QuestionDTO>(questionDTOs);
foreach(var a in Questions.SelectMany(s=>s.Answers))
{
a.IsCorrect = false;
}
}
else
{
Questions = new List<QuestionDTO>();
}

StateHasChanged();
}
}
}
1 change: 1 addition & 0 deletions QuizApp.Common/DTO/AnswerDTO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
1 change: 1 addition & 0 deletions QuizApp.Common/DTO/QuestionDTO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ public class QuestionDTO
public int Id { get; set; }
public string Title { get; set; } = string.Empty;
public IEnumerable<AnswerDTO> Answers { get; set; } = new List<AnswerDTO>();
public int SelectedAnswerId { get; set; }
}
7 changes: 4 additions & 3 deletions QuizApp.Database/Repositories/QuizRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace QuizApp.Database.Repositories;
public class QuizRepository : IQuizRepository
{
private readonly QuizContext db;

public QuizRepository(QuizContext db)
{
this.db = db;
Expand Down Expand Up @@ -291,10 +291,10 @@ public async Task<bool> DeleteQuiz(int quizId)
public async Task<IEnumerable<QuestionDTO>> 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<QuestionDTO>();
}
Expand All @@ -308,6 +308,7 @@ public async Task<IEnumerable<QuestionDTO>> GetQuestionsByQuizId(int quizId)
{
Id = a.Id,
Text = a.Text,
QuestionId = q.Id,
IsCorrect = a.IsCorrect
}).ToList()
});
Expand Down
Loading