feat : ajouter question

Multiplayer_Php
Maxence GUITARD 1 year ago
parent 5401c208af
commit 3178f6a9e7

@ -3,14 +3,13 @@
public class AnswerModel public class AnswerModel
{ {
public int Id { get; private set; } public int Id { get; set; }
public string Content { get; set; } public string Content { get; set; }
public int IdQuestion { get; private set; } public int IdQuestion { get; set; }
public AnswerModel(int id, string content, int idQuestion) public AnswerModel() { }
public AnswerModel(int id)
{ {
Id = id; Id = id;
Content = content;
IdQuestion = idQuestion;
} }
} }

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

@ -4,6 +4,8 @@ using Microsoft.AspNetCore.Components;
using Blazor.Models; using Blazor.Models;
using Blazor.Services; using Blazor.Services;
using Blazor.Pages.Admins; using Blazor.Pages.Admins;
using Blazor.ViewClasses;
using static System.Net.WebRequestMethods;
namespace Blazor.Pages.Questions namespace Blazor.Pages.Questions
{ {
@ -11,27 +13,72 @@ namespace Blazor.Pages.Questions
{ {
private QuestionModel questionModel = new(); private QuestionModel questionModel = new();
public List<Chapter> chapters;
private List<AnswerModel> answerModels;
public List<Checkbox> checkboxs;
[Inject] [Inject]
public IDataService DataService { get; set; } public IDataService DataService { get; set; }
[Inject]
public HttpClient Http { get; set; }
[Inject] [Inject]
public NavigationManager NavigationManager { get; set; } public NavigationManager NavigationManager { get; set; }
[Inject] [Inject]
public ILogger<AddAdministrator> Logger { get; set; } public ILogger<AddAdministrator> Logger { get; set; }
public class Checkbox
{
public int Id { get; set; }
public bool IsCorrect { get; set; }
public Checkbox(int id) { id = Id; IsCorrect = false; }
}
protected override void OnInitialized()
{
answerModels = new();
checkboxs = new();
for(int i = 0; i < 4; i++)
{
answerModels.Add(new AnswerModel(i));
checkboxs.Add(new Checkbox(i));
}
}
private void SetCorrectAnswer(int Id)
{
questionModel.IdAnswerGood = Id;
}
protected override async Task OnInitializedAsync()
{
var response = Http.GetFromJsonAsync<Chapter[]>($"https://trusting-panini.87-106-126-109.plesk.page/api/chapters/qUOGkWdoPCgbmuqxIC8xiaX0rV1Pw1LoPafkaoHOgszEyD9P2vcOu493xCDZpAqO").Result;
chapters = new List<Chapter>(response);
}
private async void HandleValidSubmit() private async void HandleValidSubmit()
{ {
await DataService.Add(questionModel); await DataService.Add(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", questionModel.Content));
formData.Add(new KeyValuePair<string, string>("chapter", questionModel.IdChapter.ToString()));
for (int i = 0; i < answerModels.Count; i++)
{
formData.Add(new KeyValuePair<string, string>($"answercontent{i + 1}", answerModels[i].Content));
}
formData.Add(new KeyValuePair<string, string>("idanswergood", questionModel.IdAnswerGood.ToString()));
var formContent = new FormUrlEncodedContent(formData); var formContent = new FormUrlEncodedContent(formData);
string apiUri = "https://trusting-panini.87-106-126-109.plesk.page/api/add/administrator/qUOGkWdoPCgbmuqxIC8xiaX0rV1Pw1LoPafkaoHOgszEyD9P2vcOu493xCDZpAqO"; string apiUri = "https://trusting-panini.87-106-126-109.plesk.page/api/add/question/qUOGkWdoPCgbmuqxIC8xiaX0rV1Pw1LoPafkaoHOgszEyD9P2vcOu493xCDZpAqO";
using (var httpClient = new HttpClient()) using (var httpClient = new HttpClient())
{ {
@ -47,9 +94,7 @@ namespace Blazor.Pages.Questions
} }
} }
Logger.LogInformation("Admin '{administratorsModelName}' added", questionModel.Content); NavigationManager.NavigateTo("questions");
NavigationManager.NavigateTo("administrators");
} }
} }
} }

@ -2,14 +2,8 @@
public class Answer public class Answer
{ {
public int Id { get; private set; } public int Id { get; set; }
public string Content { get; set; } public string Content { get; set; }
public int IdQuestion { get; private set; } public int IdQuestion { get; set; }
public Answer(int id, string content, int idQuestion)
{
Id = id;
Content = content;
IdQuestion = idQuestion;
}
} }

Loading…
Cancel
Save