fix : delete
continuous-integration/drone/push Build is passing Details

pull/41/head
Yvan CALATAYUD 1 year ago
commit 2e83563566

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

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

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

@ -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;
@ -114,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))
{
@ -131,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())
@ -139,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', ',', ' ' };
List<string> filteredStrings = new List<string>();
StringBuilder filteredString = new StringBuilder();
s = s.Replace("\r\n", "\n");
var rows = s.Split('\n');
rows = rows.Skip(1).ToArray();
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">

@ -29,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);
@ -136,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);
@ -229,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);
@ -266,23 +266,22 @@ namespace Blazor.Services
public async Task Add(QuestionModel model)
{
// // Get the current data
// var currentData = await _localStorage.GetItemAsync<List<Question>>("data");
// 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);

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

@ -6,5 +6,4 @@
Welcome to your new app.
<SurveyPrompt Title="How is Blazor working for you?" />
<script src="Pages/Chapters.razor.js"></script>
<SurveyPrompt Title="How is Blazor working for you?" />
Loading…
Cancel
Save