From 41387fddd28fff0b7f119dc2e64947e1877d4842 Mon Sep 17 00:00:00 2001 From: "yvan.calatayud" Date: Mon, 8 Jan 2024 22:58:03 +0100 Subject: [PATCH] feat : Composant DisplayQuestions --- .../Blazor/Components/DisplayQuestions.razor | 32 ++++++++++++++ .../Components/DisplayQuestions.razor.cs | 42 +++++++++++++++++++ Blazor/Blazor/Pages/Questions/Questions.razor | 11 ++--- .../Blazor/Pages/Questions/Questions.razor.cs | 11 ++++- Blazor/Blazor/Services/DataLocalService.cs | 12 +++--- 5 files changed, 96 insertions(+), 12 deletions(-) create mode 100644 Blazor/Blazor/Components/DisplayQuestions.razor create mode 100644 Blazor/Blazor/Components/DisplayQuestions.razor.cs 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/Questions/Questions.razor b/Blazor/Blazor/Pages/Questions/Questions.razor index 3d04eb3..d8ca62d 100644 --- a/Blazor/Blazor/Pages/Questions/Questions.razor +++ b/Blazor/Blazor/Pages/Questions/Questions.razor @@ -21,13 +21,14 @@ PageSize="10" ShowPager Responsive> - - + + - + - Editer - + Editer + + Afficher \ 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 73e6d4e..18eb513 100644 --- a/Blazor/Blazor/Pages/Questions/Questions.razor.cs +++ b/Blazor/Blazor/Pages/Questions/Questions.razor.cs @@ -12,6 +12,7 @@ using Blazor.Modals; using Blazored.Modal; using Blazor.Pages.Admins; using System.Text.RegularExpressions; +using Blazor.Components; namespace Blazor.Pages.Questions; @@ -43,7 +44,7 @@ public partial class Questions 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; @@ -98,6 +99,14 @@ public partial class Questions if (!e.CancellationToken.IsCancellationRequested) { 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 c424361..93e8b62 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 });