diff --git a/Blazor/Blazor/Components/DisplayQuestions.razor b/Blazor/Blazor/Components/DisplayQuestions.razor new file mode 100644 index 0000000..fdf398d --- /dev/null +++ b/Blazor/Blazor/Components/DisplayQuestions.razor @@ -0,0 +1,32 @@ +@page "/displayquestions/{QuestionId:int}" + +@if (question != null) +{ +
+

Question n°@question.Id

+
+
+
@question.Content
+
+ +
+ @foreach (var answer in answers) + { +
+ @if (answer.Id == question.IdAnswerGood) + { +

@answer.Content

+ } + else + { +

@answer.Content

+ } +
+ + } +
+} +else +{ +

Question not found

+} diff --git a/Blazor/Blazor/Components/DisplayQuestions.razor.cs b/Blazor/Blazor/Components/DisplayQuestions.razor.cs new file mode 100644 index 0000000..91383e1 --- /dev/null +++ b/Blazor/Blazor/Components/DisplayQuestions.razor.cs @@ -0,0 +1,42 @@ +using Blazor.Models; +using Blazor.Pages; +using Blazor.Pages.Questions; +using Blazor.ViewClasses; +using Microsoft.AspNetCore.Components; +using static System.Net.WebRequestMethods; + +namespace Blazor.Components +{ + public partial class DisplayQuestions + { + [Parameter] + public int QuestionId { get; set; } + + public Question question = new(); + + private List answers = new(); + public List questions; + + [Inject] + public HttpClient Http { get; set; } + + protected override async Task OnInitializedAsync() + { + + var response = Http.GetFromJsonAsync(API.API_URL + "questions/" + API.TOKEN).Result; + questions = new List(response); + + question = questions.Find(q => q.Id == QuestionId); + + IEnumerable foundQuestions = questions.Where(q => q.Id == QuestionId); + + + foreach (var q in foundQuestions) + { + answers.Add(new Answer(q.A_id, q.A_content, q.Id)); + + } + } + + } +} diff --git a/Blazor/Blazor/Pages/Error.cshtml.cs b/Blazor/Blazor/Pages/Error.cshtml.cs index 5372821..d9caa14 100644 --- a/Blazor/Blazor/Pages/Error.cshtml.cs +++ b/Blazor/Blazor/Pages/Error.cshtml.cs @@ -5,7 +5,6 @@ using System.Diagnostics; namespace Blazor.Pages; [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] -[IgnoreAntiforgeryToken] public class ErrorModel : PageModel { public string? RequestId { get; set; } diff --git a/Blazor/Blazor/Pages/Questions/AddQuestion.razor.cs b/Blazor/Blazor/Pages/Questions/AddQuestion.razor.cs index 05af2f2..ad7219c 100644 --- a/Blazor/Blazor/Pages/Questions/AddQuestion.razor.cs +++ b/Blazor/Blazor/Pages/Questions/AddQuestion.razor.cs @@ -18,13 +18,10 @@ namespace Blazor.Pages.Questions public List checkboxs; [Inject] - public IDataService DataService { get; set; } + public required IDataService DataService { get; set; } [Inject] - public HttpClient Http { get; set; } - - [Inject] - public NavigationManager NavigationManager { get; set; } + public required NavigationManager NavigationManager { get; set; } [Inject] public ILogger Logger { get; set; } diff --git a/Blazor/Blazor/Pages/Questions/EditQuestion.razor.cs b/Blazor/Blazor/Pages/Questions/EditQuestion.razor.cs index cf43cfc..897312c 100644 --- a/Blazor/Blazor/Pages/Questions/EditQuestion.razor.cs +++ b/Blazor/Blazor/Pages/Questions/EditQuestion.razor.cs @@ -1,5 +1,4 @@ using Blazor.Models; -using Blazor.Pages.Admins; using Blazor.Services; using Microsoft.AspNetCore.Components; @@ -14,16 +13,16 @@ namespace Blazor.Pages.Questions private QuestionModel questionModel = new(); [Inject] - public IDataService DataService { get; set; } + public required IDataService DataService { get; set; } [Inject] - public NavigationManager NavigationManager { get; set; } + public required NavigationManager NavigationManager { get; set; } [Inject] - public IWebHostEnvironment WebHostEnvironment { get; set; } + public required IWebHostEnvironment WebHostEnvironment { get; set; } [Inject] - public ILogger Logger { get; set; } + public required ILogger Logger { get; set; } private async Task HandleValidSubmit() diff --git a/Blazor/Blazor/Pages/Questions/Questions.razor b/Blazor/Blazor/Pages/Questions/Questions.razor index b5fc630..d8ca62d 100644 --- a/Blazor/Blazor/Pages/Questions/Questions.razor +++ b/Blazor/Blazor/Pages/Questions/Questions.razor @@ -21,16 +21,14 @@ PageSize="10" ShowPager Responsive> - - + + - + - Editer - + Editer + + Afficher - - - - \ No newline at end of file + \ No newline at end of file diff --git a/Blazor/Blazor/Pages/Questions/Questions.razor.cs b/Blazor/Blazor/Pages/Questions/Questions.razor.cs index 9922241..8236906 100644 --- a/Blazor/Blazor/Pages/Questions/Questions.razor.cs +++ b/Blazor/Blazor/Pages/Questions/Questions.razor.cs @@ -11,38 +11,39 @@ using Microsoft.AspNetCore.Components.Forms; using Blazor.Modals; using Blazored.Modal; using System.Text.RegularExpressions; +using Blazor.Components; namespace Blazor.Pages.Questions; public partial class Questions { - public List questions; + public List questions = new(); private int totalQuestion; [Inject] - public NavigationManager NavigationManager { get; set; } + public required NavigationManager NavigationManager { get; set; } [CascadingParameter] - public IModalService Modal { get; set; } + public required IModalService Modal { get; set; } [Inject] - public IDataService DataService { get; set; } - public IWebHostEnvironment WebHostEnvironment { get; set; } + public required IDataService DataService { get; set; } + public required IWebHostEnvironment WebHostEnvironment { get; set; } [Inject] - public HttpClient Http { get; set; } + public required HttpClient Http { get; set; } [Inject] - public ILocalStorageService LocalStorage { get; set; } + public required ILocalStorageService LocalStorage { get; set; } [Inject] - public IJSRuntime IJSRuntime { get; set; } + public required IJSRuntime IJSRuntime { get; set; } private async void OnDelete(int id) { var parameters = new ModalParameters(); - parameters.Add(nameof(Question.Q_id), id); + parameters.Add(nameof(Question.Id), id); var modal = Modal.Show("Delete Confirmation", parameters); var result = await modal.Result; @@ -81,9 +82,17 @@ public partial class Questions var response = Http.GetFromJsonAsync(API.API_URL+"questions/"+API.TOKEN).Result; - if (!e.CancellationToken.IsCancellationRequested) + if (!e.CancellationToken.IsCancellationRequested && response != null) { questions = new List(response); // an actual data for the current page + + List selectedQuestions = new List(); + for (int i = 0; i < questions.Count; i += 4) + { + selectedQuestions.Add(questions[i]); + } + questions = selectedQuestions; + totalQuestion = questions.Count; var currentData = await LocalStorage.GetItemAsync("data"); diff --git a/Blazor/Blazor/Services/DataLocalService.cs b/Blazor/Blazor/Services/DataLocalService.cs index f0d761b..d36c650 100644 --- a/Blazor/Blazor/Services/DataLocalService.cs +++ b/Blazor/Blazor/Services/DataLocalService.cs @@ -230,7 +230,7 @@ namespace Blazor.Services var currentData = _http.GetFromJsonAsync>(API.API_URL+"questions/"+API.TOKEN).Result; // Get the question int the list - var question = currentData.FirstOrDefault(w => w.Q_id == id); + var question = currentData.FirstOrDefault(w => w.Id == id); // Check if question exist if (question == null) @@ -247,7 +247,7 @@ namespace Blazor.Services var currentData = await _localStorage.GetItemAsync>("data"); // Get the admin int the list - var question = currentData.FirstOrDefault(w => w.Q_id == id); + var question = currentData.FirstOrDefault(w => w.Id == id); // Check if admin exist if (question == null) @@ -256,7 +256,7 @@ namespace Blazor.Services } // Modify the content of the adminnistrator - question.Q_content = model.Content; + question.Content = model.Content; // Save the data await _localStorage.SetItemAsync("data", currentData); @@ -268,13 +268,13 @@ namespace Blazor.Services var currentData = await _localStorage.GetItemAsync>("data"); // Simulate the Id - model.Id = currentData.Max(s => s.Q_id) + 1; + model.Id = currentData.Max(s => s.Id) + 1; // Add the admin to the current data currentData.Add(new Question { - Q_id = model.Id, - Q_content = model.Content + Id = model.Id, + Content = model.Content }); diff --git a/Blazor/Blazor/ViewClasses/Question.cs b/Blazor/Blazor/ViewClasses/Question.cs index 125455d..4ba3f58 100644 --- a/Blazor/Blazor/ViewClasses/Question.cs +++ b/Blazor/Blazor/ViewClasses/Question.cs @@ -1,12 +1,22 @@ -namespace Blazor.ViewClasses; +using System.Text.Json.Serialization; + +namespace Blazor.ViewClasses; public class Question { - public int Q_id { get; set; } - public string Q_content { get; set; } - public int IdChapter { get; set; } - public int IdAnswerGood { get; set; } + [JsonPropertyName("q_id")] + public int Id { get; set; } + + [JsonPropertyName("q_content")] + public string Content { get; set; } + public int Difficulty { get; set; } public int nbFails { get; set; } + public int IdChapter { get; set; } + public string ChapterName { get; set; } + public int IdAnswerGood { get; set; } + public int A_id { get; set; } + public string A_content { get; set; } + }