Merge branch 'master' of https://codefirst.iut.uca.fr/git/jade.van_brabandt/3.01-QCM_MuscuMaths
continuous-integration/drone/push Build is passing Details

Multiplayer_Php
Jeremy DUCOURTHIAL 2 years ago
commit 29c90af909

@ -3,7 +3,7 @@
<div class="simple-form">
<p>
Are you sure you want to delete @chapter.Name ?
Are you sure you want to delete ?
</p>
<button @onclick="ConfirmDelete" class="btn btn-primary">Delete</button>

@ -17,13 +17,13 @@ namespace Blazor.Modals
[Parameter]
public int Id { get; set; }
private Chapter chapter = new Chapter();
//private Chapter chapter = new Chapter();
protected override async Task OnInitializedAsync()
{
// Get the chapter
chapter = await DataService.GetById(Id);
}
//protected override async Task OnInitializedAsync()
//{
// // Get the chapter
// chapter = await DataService.GetById(Id);
//}
void ConfirmDelete()
{

@ -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,13 @@
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 int NbFails { get; 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 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));

@ -85,14 +85,29 @@ public partial class Administrators
parameters.Add(nameof(Administrator.Id), id);
var modal = Modal.Show<DeleteConfirmation>("Delete Confirmation", parameters);
var result = modal.Result;
var result = await modal.Result;
if (result.IsCanceled)
if (result.Cancelled)
{
return;
}
await DataService.Delete(id);
string apiUri = "https://trusting-panini.87-106-126-109.plesk.page/api/delete/administrator/" + id;
using (var httpClient = new HttpClient())
{
var response = await httpClient.DeleteAsync(apiUri);
if (response.IsSuccessStatusCode)
{
var responseBody = await response.Content.ReadAsStringAsync();
}
else
{
var errorResponse = await response.Content.ReadAsStringAsync();
}
}
//await DataService.Delete(id);
// Reload the page
NavigationManager.NavigateTo("administrators", true);

@ -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));

@ -10,6 +10,8 @@ using ChoETL;
using Microsoft.AspNetCore.Components.Forms;
using Blazor.Modals;
using Blazored.Modal;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Blazor.Models;
namespace Blazor.Pages.Chapters;
public partial class Chapters
@ -44,14 +46,31 @@ public partial class Chapters
parameters.Add(nameof(Chapter.Id), id);
var modal = Modal.Show<DeleteConfirmation>("Delete Confirmation", parameters);
var result = modal.Result;
var result = await modal.Result;
if (result.IsCanceled)
if (result.Cancelled)
{
return;
}
await DataService.Delete(id);
string apiUri = "https://trusting-panini.87-106-126-109.plesk.page/api/delete/chapter/" + id;
using (var httpClient = new HttpClient())
{
var response = await httpClient.DeleteAsync(apiUri);
if (response.IsSuccessStatusCode)
{
var responseBody = await response.Content.ReadAsStringAsync();
}
else
{
var errorResponse = await response.Content.ReadAsStringAsync();
}
}
// /api/delete/player/ +chapterid
//await DataService.Delete(id);
// Reload the page
NavigationManager.NavigateTo("chapters", true);

@ -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())
{

@ -46,14 +46,30 @@ public partial class Players
parameters.Add(nameof(Player.Id), id);
var modal = Modal.Show<DeleteConfirmation>("Delete Confirmation", parameters);
var result = modal.Result;
var result = await modal.Result;
if (result.IsCanceled)
if (result.Cancelled)
{
return;
}
await DataService.Delete(id);
string apiUri = "https://trusting-panini.87-106-126-109.plesk.page/api/delete/player/" + id;
using (var httpClient = new HttpClient())
{
var response = await httpClient.DeleteAsync(apiUri);
if (response.IsSuccessStatusCode)
{
var responseBody = await response.Content.ReadAsStringAsync();
}
else
{
var errorResponse = await response.Content.ReadAsStringAsync();
}
}
//await DataService.Delete(id);
// Reload the page
NavigationManager.NavigateTo("Players", true);

@ -3,4 +3,50 @@
<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");
}
}
}

@ -1,5 +1,39 @@
<h3>EditQuestion</h3>
@page "/editQuestion/{Id:int}"
@code {
<h3>EditQuestion</h3>
<EditForm Model="@questionModel" 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" />
</label>
<label for="reponse3">
Reponse n°3:
<InputText id="reponse3" />
</label>
<label for="reponse4">
Reponse n°4:
<InputText id="reponse4" />
</label>
</p>
<button type="submit">Submit</button>
</EditForm>
}

@ -1,4 +1,5 @@
using Blazor.Models;
using Blazor.Pages.Admins;
using Blazor.Services;
using Microsoft.AspNetCore.Components;
@ -6,5 +7,50 @@ namespace Blazor.Pages.Questions
{
public partial class EditQuestion
{
[Parameter]
public int Id { get; set; }
private QuestionModel questionModel = new();
[Inject]
public IDataService DataService { get; set; }
[Inject]
public NavigationManager NavigationManager { get; set; }
[Inject]
public IWebHostEnvironment WebHostEnvironment { get; set; }
[Inject]
public ILogger<EditQuestion> Logger { get; set; }
private async void HandleValidSubmit()
{
await DataService.Update(Id, 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/update/questions/" + questionModel.Id;
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();
}
}
}
}
}

@ -8,6 +8,10 @@
<NavLink class="btn btn-primary" href="addQuestion" Match="NavLinkMatch.All">
<i class="fa fa-plus"></i> Ajouter
</NavLink>
<NavLink class="btn btn-primary" @onclick="Export">
<i class="fa fa-plus"></i> Exporter
</NavLink>
<InputFile OnChange="@SingleUpload" />
</div>
<DataGrid TItem="Question"

@ -11,6 +11,7 @@ using Microsoft.AspNetCore.Components.Forms;
using Blazor.Modals;
using Blazored.Modal;
using Blazor.Pages.Admins;
using System.Text.RegularExpressions;
namespace Blazor.Pages.Questions;
@ -45,14 +46,30 @@ public partial class Questions
parameters.Add(nameof(Question.Id), id);
var modal = Modal.Show<DeleteConfirmation>("Delete Confirmation", parameters);
var result = modal.Result;
var result = await modal.Result;
if (result.IsCanceled)
if (result.Cancelled)
{
return;
}
await DataService.Delete(id);
string apiUri = "https://trusting-panini.87-106-126-109.plesk.page/api/delete/question/" + id;
using (var httpClient = new HttpClient())
{
var response = await httpClient.DeleteAsync(apiUri);
if (response.IsSuccessStatusCode)
{
var responseBody = await response.Content.ReadAsStringAsync();
}
else
{
var errorResponse = await response.Content.ReadAsStringAsync();
}
}
//await DataService.Delete(id);
// Reload the page
NavigationManager.NavigateTo("questions", true);
@ -98,7 +115,7 @@ public partial class Questions
private async void Export()
{
StringBuilder sb = new StringBuilder();
HttpResponseMessage response = await Http.GetAsync("https://trusting-panini.87-106-126-109.plesk.page/api/chapters");
HttpResponseMessage response = await Http.GetAsync("https://trusting-panini.87-106-126-109.plesk.page/api/questionsExport");
var json = await response.Content.ReadAsStringAsync();
using (var jsonFile = ChoJSONReader.LoadText(json))
{
@ -115,6 +132,7 @@ public partial class Questions
await IJSRuntime.InvokeVoidAsync("downloadFileFromStream", "data.csv", streamRef);
}
}
private async Task SingleUpload(InputFileChangeEventArgs e)
{
using (MemoryStream ms = new MemoryStream())
@ -123,53 +141,51 @@ public partial class Questions
var bytes = ms.ToArray();
string s = Encoding.UTF8.GetString(bytes);
char[] invalidChars = { '1', '2', '3', '4', '5', '6', '7', '8', '9', '\r', '\n', ',', ' ' };
s = s.Replace("\r\n", "\n");
var rows = s.Split('\n');
rows = rows.Skip(1).ToArray();
List<string> filteredStrings = new List<string>();
StringBuilder filteredString = new StringBuilder();
foreach (var c in s)
foreach (var row in rows)
{
if (!invalidChars.Contains(c))
var field = row.Split(';');
var formData = new List<KeyValuePair<string, string>>();
formData.Add(new KeyValuePair<string, string>("content", field[0]));
formData.Add(new KeyValuePair<string, string>("answerContent1", field[2]));
formData.Add(new KeyValuePair<string, string>("answerContent2", field[3]));
formData.Add(new KeyValuePair<string, string>("answerContent3", field[4]));
formData.Add(new KeyValuePair<string, string>("answerContent4", field[5]));
formData.Add(new KeyValuePair<string, string>("idanswergood", field[6]));
string apiUri = "https://trusting-panini.87-106-126-109.plesk.page/api/chapters/name/"+field[1];
var response = await Http.GetAsync(apiUri);
if (response.IsSuccessStatusCode)
{
filteredString.Append(c);
var responseBody = await response.Content.ReadAsStringAsync();
Match match = Regex.Match(responseBody, @"\d+");
int result = int.Parse(match.Value);
formData.Add(new KeyValuePair<string, string>("idchapter", result.ToString()));
}
else
{
if (filteredString.Length > 0)
{
filteredStrings.Add(filteredString.ToString());
filteredString.Clear();
}
var errorResponse = await response.Content.ReadAsStringAsync();
formData.Add(new KeyValuePair<string, string>("idchapter", "Unknown_Chapter_Error"));
}
}
if (filteredString.Length > 0)
{
filteredStrings.Add(filteredString.ToString());
}
foreach (var filteredStr in filteredStrings)
{
var formData = new List<KeyValuePair<string, string>>();
formData.Add(new KeyValuePair<string, string>("name", filteredStr));
var formContent = new FormUrlEncodedContent(formData);
apiUri = "https://trusting-panini.87-106-126-109.plesk.page/api/add/questions";
string apiUri = "https://trusting-panini.87-106-126-109.plesk.page/api/add/chapters";
response = await Http.PostAsync(apiUri, formContent);
using (var httpClient = new HttpClient())
if (response.IsSuccessStatusCode)
{
var responseBody = await response.Content.ReadAsStringAsync();
}
else
{
var response = await httpClient.PostAsync(apiUri, formContent);
if (response.IsSuccessStatusCode)
{
var responseBody = await response.Content.ReadAsStringAsync();
}
else
{
var errorResponse = await response.Content.ReadAsStringAsync();
}
var errorResponse = await response.Content.ReadAsStringAsync();
}
}
}

@ -28,7 +28,7 @@
<a href="" class="reload">Reload</a>
<a class="dismiss">🗙</a>
</div>
<script src="Pages/Chapters/Chapters.razor.js"></script>
<script src="_framework/blazor.server.js"></script>
</body>
</html>

@ -6,6 +6,8 @@
<script src="_content/Blazored.Modal/blazored.modal.js"></script>
<script src="_framework/blazor.server.js"></script>
<script src="Pages/Chapters/Chapters.razor.js"></script>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.15.4/css/all.css">

@ -2,6 +2,7 @@
using Blazor.ViewClasses;
using Blazored.LocalStorage;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using static System.Net.WebRequestMethods;
namespace Blazor.Services
@ -28,7 +29,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 +136,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 +229,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);
@ -242,48 +243,46 @@ namespace Blazor.Services
return question;
}
//public async Task Update(int id, QuestionsModel model)
//{
// // Get the current data
// var currentData = await _localStorage.GetItemAsync<List<Question>>("data");
public async Task Update(int id, QuestionModel model)
{
// Get the current data
var currentData = await _localStorage.GetItemAsync<List<Question>>("data");
// // Get the admin int the list
// var question = currentData.FirstOrDefault(w => w.Id == id);
// Get the admin int the list
var question = currentData.FirstOrDefault(w => w.Id == id);
// // Check if admin exist
// if (question == null)
// {
// throw new Exception($"Unable to found the item with ID: {id}");
// }
// Check if admin exist
if (question == null)
{
throw new Exception($"Unable to found the item with ID: {id}");
}
// // Modify the content of the adminnistrator
// question.Username = model.Username;
// question.HashedPassword = model.HashedPassword;
// Modify the content of the adminnistrator
question.Content = model.Content;
// // Save the data
// await _localStorage.SetItemAsync("data", currentData);
//}
// Save the data
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;
}
}

@ -7,4 +7,3 @@
Welcome to your new app.
<SurveyPrompt Title="How is Blazor working for you?" />
<script src="Pages/Chapters.razor.js"></script>

Loading…
Cancel
Save