feat : update question

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

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

@ -34,6 +34,8 @@ namespace Blazor.Pages.Questions
public bool IsCorrect { get; set; }
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()

@ -3,37 +3,46 @@
<h3>EditQuestion</h3>
<EditForm Model="@questionModel" OnValidSubmit="@HandleValidSubmit">
<EditForm Model="@question" OnValidSubmit="@HandleValidSubmit">
<DataAnnotationsValidator />
<ValidationSummary />
<p>
<label for="content">
Content:
<InputText id="content" @bind-Value="questionModel.Content" />
</label>
<label for="chapter">
Chapter:
<InputText id="chapter"/>
</label>
<label for="reponse1">
Reponse n°1:
<InputText id="reponse1" />
</label>
<label for="reponse2">
Reponse n°2:
<InputText id="reponse2" />
<InputText id="content" @bind-Value="question.Content" />
</label>
<label for="reponse3">
Reponse n°3:
<InputText id="reponse3" />
</label>
<label for="reponse4">
Reponse n°4:
<InputText id="reponse4" />
</p>
<p>
<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>
</p>
@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 for="@($"checkbox{checkbox.Id}")">
@checkbox.IsCorrect
<InputCheckbox id="@($"checkbox{checkbox.Id}")" @bind-Value="checkbox.IsCorrect" /> Correcte
</label>
</p>
}
<button type="submit">Submit</button>
</EditForm>

@ -1,7 +1,10 @@
using Blazor.Models;
using Blazor.Services;
using Blazor.ViewClasses;
using Microsoft.AspNetCore.Components;
using System;
using static Blazor.Pages.Questions.AddQuestion;
using static System.Net.WebRequestMethods;
namespace Blazor.Pages.Questions
{
@ -11,11 +14,22 @@ namespace Blazor.Pages.Questions
[Parameter]
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]
public required IDataService DataService { get; set; }
[Inject]
public required HttpClient Http { get; set; }
[Inject]
public required NavigationManager NavigationManager { get; set; }
@ -27,38 +41,57 @@ namespace Blazor.Pages.Questions
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)
{
Id = question.Id,
Content = question.Content
};
if (question.IdAnswerGood == checkbox.Id)
{
checkbox.IsCorrect = true;
}
}
}
private async Task HandleValidSubmit()
{
await DataService.Update(Id, questionModel);
var formData = new List<KeyValuePair<string, string>>();
formData.Add(new KeyValuePair<string, string>("content", questionModel.Content));
formData.Add(new KeyValuePair<string, string>("idchapter", questionModel.IdChapter.ToString()));
//foreach (var answerModel in answerModels)
//{
// var answercontent = $"answerContent{answerModel.Id + 1}";
// formData.Add(new KeyValuePair<string, string>(answercontent, answerModel.Content));
//}
//foreach (var checkbox in checkboxs)
//{
// if (checkbox.IsCorrect != false)
// {
// var idgood = checkbox.Id + 1;
// formData.Add(new KeyValuePair<string, string>("idanswergood", idgood.ToString()));
// }
//}
formData.Add(new KeyValuePair<string, string>("content", question.Content));
formData.Add(new KeyValuePair<string, string>("idchapter", question.IdChapter.ToString()));
foreach(var index in Enumerable.Range(0, answers.Count))
{
var answer = answers[index];
var answercontent = $"answerContent{index + 1}";
formData.Add(new KeyValuePair<string, string>(answercontent, answer.Content));
}
foreach (var checkbox in checkboxs)
{
if (checkbox.IsCorrect == true)
{
formData.Add(new KeyValuePair<string, string>("idanswergood", checkbox.Id.ToString()));
}
}
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())
{
@ -73,6 +106,7 @@ namespace Blazor.Pages.Questions
var errorResponse = await response.Content.ReadAsStringAsync();
}
}
NavigationManager.NavigateTo("questions");
}
}

@ -241,6 +241,23 @@ namespace Blazor.Services
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)
{
// Get the current data
@ -407,6 +424,23 @@ namespace Blazor.Services
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 Update(int id, QuestionModel model);
Task<Question> GetQuestionById(int id);
Task<List<Question>> GetQuestionsById(int id);
Task<int> CountQuestion();
Task<List<Question>> ListQuestion(int currentPage, int pageSize);
@ -44,5 +44,6 @@ namespace Blazor.Services
Task<int> CountPlayer();
Task<List<Player>> ListPlayer(int currentPage, int pageSize);
Task<Answer> GetAnswerByIdQuestion(int id);
}
}

Loading…
Cancel
Save