fix : add et edit de toute les pages
continuous-integration/drone/push Build is passing Details

pull/41/head
Maxence GUITARD 1 year ago
parent e9fbf7c15a
commit 50af63099e

@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Cryptography.KeyDerivation;
using System.Security.Cryptography;
using System.Text;
namespace Blazor.Models;
@ -8,4 +9,21 @@ public class PlayerModel
public int Id { get; set; }
public string Nickname { get; set; }
public string HashedPassword { get; set; }
public void HashPassword(string password)
{
using (MD5 md5 = MD5.Create())
{
byte[] inputBytes = Encoding.UTF8.GetBytes(password);
byte[] hashBytes = md5.ComputeHash(inputBytes);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hashBytes.Length; i++)
{
sb.Append(hashBytes[i].ToString("x2"));
}
HashedPassword = sb.ToString();
}
}
}

@ -2,22 +2,12 @@
public class QuestionModel
{
public int Id { get; private set; }
public int Id { get; set; }
public string Content { get; set; }
public int IdChapter { get; private set; }
public int IdChapter { get; set; }
public int? IdAnswerGood { get; set; }
public int Difficulty { get; set; }
public int NbFails { get; private set; }
public QuestionModel(int id, string content, int idChapter, int difficulty, int nbFails, int? idAnswerGood = null)
{
Id = id;
Content = content;
IdChapter = idChapter;
Difficulty = difficulty;
NbFails = nbFails;
IdAnswerGood = idAnswerGood;
}
public int NbFails { get; set; }
public void addFails(int nb) { NbFails += nb; }
public void removeFails(int nb) { NbFails -= nb; }

@ -12,6 +12,8 @@
Username:
<InputText id="username" @bind-Value="administratorModel.Username" />
</label>
</p>
<p>
<label for="hashedPassword">
Password:
<InputText id="hashedPassword" @bind-Value="administratorModel.HashedPassword" />

@ -24,7 +24,7 @@ namespace Blazor.Pages.Admins
{
administratorModel.HashPassword(administratorModel.HashedPassword);
await DataService.Add(administratorModel);
//await DataService.Add(administratorModel);
var formData = new List<KeyValuePair<string, string>>();
formData.Add(new KeyValuePair<string, string>("username", administratorModel.Username));

@ -44,7 +44,7 @@ namespace Blazor.Pages.Admins
{
administratorModel.HashPassword(administratorModel.HashedPassword);
await DataService.Update(Id, administratorModel);
//await DataService.Update(Id, administratorModel);
var formData = new List<KeyValuePair<string, string>>();
formData.Add(new KeyValuePair<string, string>("username", administratorModel.Username));
formData.Add(new KeyValuePair<string, string>("password", administratorModel.HashedPassword));

@ -21,7 +21,7 @@ public partial class AddChapter
private async void HandleValidSubmit()
{
await DataService.Add(chapterModel);
//await DataService.Add(chapterModel);
var formData = new List<KeyValuePair<string, string>>();
formData.Add(new KeyValuePair<string, string>("name", chapterModel.Name));

@ -40,7 +40,7 @@ public partial class EditChapter
private async void HandleValidSubmit()
{
await DataService.Update(Id, chapterModel);
//await DataService.Update(Id, chapterModel);
var formData = new List<KeyValuePair<string, string>>();
formData.Add(new KeyValuePair<string, string>("name", chapterModel.Name));

@ -17,7 +17,10 @@ namespace Blazor.Pages.Players
private async void HandleValidSubmit()
{
await DataService.Add(playerModel);
playerModel.HashPassword(playerModel.HashedPassword);
//await DataService.Add(playerModel);
var formData = new List<KeyValuePair<string, string>>();
formData.Add(new KeyValuePair<string, string>("nickname", playerModel.Nickname));
formData.Add(new KeyValuePair<string, string>("password", playerModel.HashedPassword));

@ -30,20 +30,24 @@ namespace Blazor.Pages.Players
playerModel = new PlayerModel
{
Id = player.Id,
Nickname = player.Nickname
Nickname = player.Nickname,
HashedPassword = player.HashedPassword
};
}
private async void HandleValidSubmit()
{
await DataService.Update(Id, playerModel);
playerModel.HashPassword(playerModel.HashedPassword);
//await DataService.Update(Id, playerModel);
var formData = new List<KeyValuePair<string, string>>();
formData.Add(new KeyValuePair<string, string>("nickname", playerModel.Nickname));
formData.Add(new KeyValuePair<string, string>("password", playerModel.HashedPassword));
var formContent = new FormUrlEncodedContent(formData);
string apiUri = "https://trusting-panini.87-106-126-109.plesk.page/api/update/"+playerModel.Id;
string apiUri = "https://trusting-panini.87-106-126-109.plesk.page/api/update/player/"+playerModel.Id;
using (var httpClient = new HttpClient())
{

@ -1,6 +1,52 @@
@page "/addAdministrator"
@page "/addQuestion"
@using Blazor.Models
<h3>AddQuestion</h3>
<EditForm Model="@questionModel" OnValidSubmit="@HandleValidSubmit">
<DataAnnotationsValidator />
<ValidationSummary />
<p>
<label for="content">
Content:
<InputText id="content" @bind-Value="questionModel.Content" />
</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>
<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> *@
<button type="submit">Submit</button>
</EditForm>

@ -1,6 +1,55 @@
namespace Blazor.Pages.Questions
using Blazored.LocalStorage;
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.AspNetCore.Components;
using Blazor.Models;
using Blazor.Services;
using Blazor.Pages.Admins;
namespace Blazor.Pages.Questions
{
public partial class AddQuestion
{
private QuestionModel questionModel = new();
[Inject]
public IDataService DataService { get; set; }
[Inject]
public NavigationManager NavigationManager { get; set; }
[Inject]
public ILogger<AddAdministrator> Logger { get; set; }
private async void HandleValidSubmit()
{
await DataService.Add(questionModel);
var formData = new List<KeyValuePair<string, string>>();
formData.Add(new KeyValuePair<string, string>("content", questionModel.Content));
var formContent = new FormUrlEncodedContent(formData);
string apiUri = "https://trusting-panini.87-106-126-109.plesk.page/api/add/administrator";
using (var httpClient = new HttpClient())
{
var response = await httpClient.PostAsync(apiUri, formContent);
if (response.IsSuccessStatusCode)
{
var responseBody = await response.Content.ReadAsStringAsync();
}
else
{
var errorResponse = await response.Content.ReadAsStringAsync();
}
}
Logger.LogInformation("Admin '{administratorsModelName}' added", questionModel.Content);
NavigationManager.NavigateTo("administrators");
}
}
}

@ -28,7 +28,7 @@ namespace Blazor.Services
public async Task<Chapter> GetById(int id)
{
// Get the current data
var currentData = await _localStorage.GetItemAsync<List<Chapter>>("data");
var currentData = _http.GetFromJsonAsync<List<Chapter>>($"https://trusting-panini.87-106-126-109.plesk.page/api/chapters").Result;
// Get the chapter int the list
var chapter = currentData.FirstOrDefault(w => w.Id == id);
@ -135,7 +135,7 @@ namespace Blazor.Services
public async Task<Administrator> GetAdminById(int id)
{
// Get the current data
var currentData = await _localStorage.GetItemAsync<List<Administrator>>("data");
var currentData = _http.GetFromJsonAsync<List<Administrator>>($"https://trusting-panini.87-106-126-109.plesk.page/api/administrators").Result;
// Get the admin int the list
var admin = currentData.FirstOrDefault(w => w.Id == id);
@ -228,7 +228,7 @@ namespace Blazor.Services
public async Task<Question> GetQuestionById(int id)
{
// Get the current data
var currentData = await _localStorage.GetItemAsync<List<Question>>("data");
var currentData = _http.GetFromJsonAsync<List<Question>>($"https://trusting-panini.87-106-126-109.plesk.page/api/questions").Result;
// Get the question int the list
var question = currentData.FirstOrDefault(w => w.Id == id);
@ -264,26 +264,25 @@ namespace Blazor.Services
// await _localStorage.SetItemAsync("data", currentData);
//}
//public async Task Add(QuestionsModel model)
//{
// // Get the current data
// var currentData = await _localStorage.GetItemAsync<List<Question>>("data");
public async Task Add(QuestionModel model)
{
// Get the current data
var currentData = await _localStorage.GetItemAsync<List<Question>>("data");
// // Simulate the Id
// model.Id = currentData.Max(s => s.Id) + 1;
// Simulate the Id
model.Id = currentData.Max(s => s.Id) + 1;
// // Add the admin to the current data
// currentData.Add(new Question
// {
// Id = model.Id,
// Username = model.Username,
// HashedPassword = model.HashedPassword
// });
// Add the admin to the current data
currentData.Add(new Question
{
Id = model.Id,
Content = model.Content
});
// // Save the data
// await _localStorage.SetItemAsync("data", currentData);
//}
// Save the data
await _localStorage.SetItemAsync("data", currentData);
}
public async Task<int> CountQuestion()
{
@ -322,7 +321,7 @@ namespace Blazor.Services
public async Task<Player> GetPlayerById(int id)
{
// Get the current data
var currentData = await _localStorage.GetItemAsync<List<Player>>("data");
var currentData = _http.GetFromJsonAsync<List<Player>>($"https://trusting-panini.87-106-126-109.plesk.page/api/players").Result;
// Get the player in the list
var player = currentData.FirstOrDefault(w => w.Id == id);

@ -24,9 +24,9 @@ namespace Blazor.Services
Task<int> CountAdmin();
Task<List<Administrator>> ListAdmin(int currentPage, int pageSize);
//Task Add(QuestionsModel model);
Task Add(QuestionModel model);
//Task Update(int id, QuestionsModel model);
//Task Update(int id, QuestionModel model);
Task<Question> GetQuestionById(int id);

@ -2,20 +2,11 @@
public class Question
{
public int Id { get; private set; }
public int Id { get; set; }
public string Content { get; set; }
public int IdChapter { get; set; }
public int IdAnswerGood { get; set; }
public int Difficulty { get; set; }
public int nbFails { get; set; }
public Question(int id, string content, int idChapter, int idAnswerGood, int difficulty, int nbFails = 0)
{
Id = id;
Content = content;
IdChapter = idChapter;
IdAnswerGood = idAnswerGood;
Difficulty = difficulty;
this.nbFails = nbFails;
}
}

Loading…
Cancel
Save