feat : ajouter question

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

@ -3,14 +3,13 @@
public class AnswerModel
{
public int Id { get; private set; }
public int Id { 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;
Content = content;
IdQuestion = idQuestion;
}
}

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

@ -4,6 +4,8 @@ using Microsoft.AspNetCore.Components;
using Blazor.Models;
using Blazor.Services;
using Blazor.Pages.Admins;
using Blazor.ViewClasses;
using static System.Net.WebRequestMethods;
namespace Blazor.Pages.Questions
{
@ -11,27 +13,72 @@ namespace Blazor.Pages.Questions
{
private QuestionModel questionModel = new();
public List<Chapter> chapters;
private List<AnswerModel> answerModels;
public List<Checkbox> checkboxs;
[Inject]
public IDataService DataService { get; set; }
[Inject]
public HttpClient Http { get; set; }
[Inject]
public NavigationManager NavigationManager { get; set; }
[Inject]
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()
{
await DataService.Add(questionModel);
var formData = new List<KeyValuePair<string, string>>();
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);
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())
{
@ -47,9 +94,7 @@ namespace Blazor.Pages.Questions
}
}
Logger.LogInformation("Admin '{administratorsModelName}' added", questionModel.Content);
NavigationManager.NavigateTo("administrators");
NavigationManager.NavigateTo("questions");
}
}
}

@ -2,14 +2,8 @@
public class Answer
{
public int Id { get; private set; }
public int Id { 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