feat : update question

Multiplayer_Php
Maxence GUITARD 1 year ago
parent 45fde3b28f
commit 168699d4d9

@ -10,4 +10,6 @@ public class AnswerModel
{ {
Id = id; Id = id;
} }
public AnswerModel(){}
} }

@ -34,6 +34,8 @@ namespace Blazor.Pages.Questions
public bool IsCorrect { get; set; } public bool IsCorrect { get; set; }
public Checkbox(int id) { Id = id; IsCorrect = false; } public Checkbox(int id) { Id = id; IsCorrect = false; }
public Checkbox() { }
public Checkbox(int id, bool isCorrect) { Id = id; IsCorrect = isCorrect; }
} }
protected override async Task OnInitializedAsync() protected override async Task OnInitializedAsync()

@ -3,37 +3,46 @@
<h3>EditQuestion</h3> <h3>EditQuestion</h3>
<EditForm Model="@questionModel" OnValidSubmit="@HandleValidSubmit"> <EditForm Model="@question" OnValidSubmit="@HandleValidSubmit">
<DataAnnotationsValidator /> <DataAnnotationsValidator />
<ValidationSummary /> <ValidationSummary />
<p> <p>
<label for="content"> <label for="content">
Content: Content:
<InputText id="content" @bind-Value="questionModel.Content" /> <InputText id="content" @bind-Value="question.Content" />
</label>
<label for="chapter">
Chapter:
<InputText id="chapter"/>
</label>
<label for="reponse1">
Reponse n°1:
<InputText id="reponse1" />
</label> </label>
<label for="reponse2"> </p>
Reponse n°2: <p>
<InputText id="reponse2" /> <label for="idChapter">
Chapitre de la question :
<InputSelect id="idChapter" @bind-Value="question.IdChapter">
@if (chapters != null)
{
foreach (var chapter in chapters)
{
<option value="@chapter.Id">@chapter.Name</option>
}
}
</InputSelect>
</label> </label>
<label for="reponse3"> </p>
Reponse n°3:
<InputText id="reponse3" /> @foreach (var index in Enumerable.Range(0, answers.Count))
{
var answer = answers[index];
var checkbox = checkboxs[index];
<p>
<label for="@($"answer{answer.Id}")">
Réponse n°@(index + 1) :
<InputText id="@($"answer{answer.Id}")" @bind-Value="answer.Content" />
</label> </label>
<label for="reponse4"> <label for="@($"checkbox{checkbox.Id}")">
Reponse n°4: @checkbox.IsCorrect
<InputText id="reponse4" /> <InputCheckbox id="@($"checkbox{checkbox.Id}")" @bind-Value="checkbox.IsCorrect" /> Correcte
</label> </label>
</p> </p>
}
<button type="submit">Submit</button> <button type="submit">Submit</button>
</EditForm> </EditForm>

@ -1,7 +1,10 @@
using Blazor.Models; using Blazor.Models;
using Blazor.Services; using Blazor.Services;
using Blazor.ViewClasses;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
using System;
using static Blazor.Pages.Questions.AddQuestion; using static Blazor.Pages.Questions.AddQuestion;
using static System.Net.WebRequestMethods;
namespace Blazor.Pages.Questions namespace Blazor.Pages.Questions
{ {
@ -11,11 +14,22 @@ namespace Blazor.Pages.Questions
[Parameter] [Parameter]
public int Id { get; set; } public int Id { get; set; }
private QuestionModel? questionModel; public List<Chapter> chapters = new();
private List<Answer> answers = new();
public List<Checkbox> checkboxs = new();
public Question question = new();
public List<Question> questions = new();
[Inject] [Inject]
public required IDataService DataService { get; set; } public required IDataService DataService { get; set; }
[Inject]
public required HttpClient Http { get; set; }
[Inject] [Inject]
public required NavigationManager NavigationManager { get; set; } public required NavigationManager NavigationManager { get; set; }
@ -27,38 +41,57 @@ namespace Blazor.Pages.Questions
protected override async Task OnInitializedAsync() protected override async Task OnInitializedAsync()
{ {
var question = await DataService.GetQuestionById(Id); var response = Http.GetFromJsonAsync<Chapter[]>(API.API_URL + "chapters/" + API.TOKEN).Result;
if (response == null) chapters = new List<Chapter>();
else chapters = new List<Chapter>(response);
answers = new();
checkboxs = new();
var resp = Http.GetFromJsonAsync<Question[]>(API.API_URL + "questions/" + API.TOKEN).Result;
questions = new List<Question>(resp);
question = questions.Find(q => q.Id == Id);
IEnumerable<Question> foundQuestions = questions.Where(q => q.Id == Id);
foreach (var q in foundQuestions)
{
answers.Add(new Answer(q.A_id, q.A_content, q.Id));
checkboxs.Add(new Checkbox(q.A_id,false));
}
questionModel = new QuestionModel foreach (var checkbox in checkboxs)
{
if (question.IdAnswerGood == checkbox.Id)
{ {
Id = question.Id, checkbox.IsCorrect = true;
Content = question.Content }
}; }
} }
private async Task HandleValidSubmit() private async Task HandleValidSubmit()
{ {
await DataService.Update(Id, questionModel);
var formData = new List<KeyValuePair<string, string>>(); var formData = new List<KeyValuePair<string, string>>();
formData.Add(new KeyValuePair<string, string>("content", questionModel.Content)); formData.Add(new KeyValuePair<string, string>("content", question.Content));
formData.Add(new KeyValuePair<string, string>("idchapter", questionModel.IdChapter.ToString())); formData.Add(new KeyValuePair<string, string>("idchapter", question.IdChapter.ToString()));
//foreach (var answerModel in answerModels) foreach(var index in Enumerable.Range(0, answers.Count))
//{ {
// var answercontent = $"answerContent{answerModel.Id + 1}"; var answer = answers[index];
// formData.Add(new KeyValuePair<string, string>(answercontent, answerModel.Content)); var answercontent = $"answerContent{index + 1}";
//} formData.Add(new KeyValuePair<string, string>(answercontent, answer.Content));
//foreach (var checkbox in checkboxs) }
//{ foreach (var checkbox in checkboxs)
// if (checkbox.IsCorrect != false) {
// { if (checkbox.IsCorrect == true)
// var idgood = checkbox.Id + 1; {
// formData.Add(new KeyValuePair<string, string>("idanswergood", idgood.ToString())); formData.Add(new KeyValuePair<string, string>("idanswergood", checkbox.Id.ToString()));
// } }
//} }
var formContent = new FormUrlEncodedContent(formData); var formContent = new FormUrlEncodedContent(formData);
string apiUri = API.API_URL+"/update/questions/" + questionModel.Id + "/" + API.TOKEN; string apiUri = API.API_URL+"/update/question/" + question.Id + "/" + API.TOKEN;
using (var httpClient = new HttpClient()) using (var httpClient = new HttpClient())
{ {
@ -73,6 +106,7 @@ namespace Blazor.Pages.Questions
var errorResponse = await response.Content.ReadAsStringAsync(); var errorResponse = await response.Content.ReadAsStringAsync();
} }
} }
NavigationManager.NavigateTo("questions");
} }
} }

@ -241,6 +241,23 @@ namespace Blazor.Services
return question; return question;
} }
public async Task<List<Question>> GetQuestionsById(int id)
{
// Get the current data
var currentData = await _http.GetFromJsonAsync<List<Question>>(API.API_URL + "questions/" + API.TOKEN);
// Get all questions with the specified ID
var questions = currentData.Where(w => w.Id == id).ToList();
// Check if any questions were found
if (questions.Count == 0)
{
throw new Exception($"Unable to find questions with ID: {id}");
}
return questions;
}
public async Task Update(int id, QuestionModel model) public async Task Update(int id, QuestionModel model)
{ {
// Get the current data // Get the current data
@ -407,6 +424,23 @@ namespace Blazor.Services
return (await _localStorage.GetItemAsync<Player[]>("data")).Skip((currentPage - 1) * pageSize).Take(pageSize).ToList(); return (await _localStorage.GetItemAsync<Player[]>("data")).Skip((currentPage - 1) * pageSize).Take(pageSize).ToList();
} }
public async Task<Answer> GetAnswerByIdQuestion(int id)
{
// Get the current data
var currentData = _http.GetFromJsonAsync<List<Answer>>(API.API_URL + "answer/" + API.TOKEN).Result;
// Get the question int the list
var answer = currentData.FirstOrDefault(w => w.Id == id);
// Check if question exist
if (answer == null)
{
throw new Exception($"Unable to found the item with ID: {id}");
}
return answer;
}
} }
} }

@ -27,8 +27,8 @@ namespace Blazor.Services
Task Add(QuestionModel model); Task Add(QuestionModel model);
Task Update(int id, QuestionModel model); Task Update(int id, QuestionModel model);
Task<Question> GetQuestionById(int id); Task<Question> GetQuestionById(int id);
Task<List<Question>> GetQuestionsById(int id);
Task<int> CountQuestion(); Task<int> CountQuestion();
Task<List<Question>> ListQuestion(int currentPage, int pageSize); Task<List<Question>> ListQuestion(int currentPage, int pageSize);
@ -44,5 +44,6 @@ namespace Blazor.Services
Task<int> CountPlayer(); Task<int> CountPlayer();
Task<List<Player>> ListPlayer(int currentPage, int pageSize); Task<List<Player>> ListPlayer(int currentPage, int pageSize);
Task<Answer> GetAnswerByIdQuestion(int id);
} }
} }

Loading…
Cancel
Save