diff --git a/Blazor/Blazor/Modals/DeleteConfirmation.razor b/Blazor/Blazor/Modals/DeleteConfirmation.razor index 959fbda..3eab751 100644 --- a/Blazor/Blazor/Modals/DeleteConfirmation.razor +++ b/Blazor/Blazor/Modals/DeleteConfirmation.razor @@ -3,7 +3,7 @@

- Are you sure you want to delete @chapter.Name ? + Are you sure you want to delete ?

diff --git a/Blazor/Blazor/Modals/DeleteConfirmation.razor.cs b/Blazor/Blazor/Modals/DeleteConfirmation.razor.cs index bf38444..0e79b81 100644 --- a/Blazor/Blazor/Modals/DeleteConfirmation.razor.cs +++ b/Blazor/Blazor/Modals/DeleteConfirmation.razor.cs @@ -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() { diff --git a/Blazor/Blazor/Models/PlayerModel.cs b/Blazor/Blazor/Models/PlayerModel.cs index fef9ba3..43c37a0 100644 --- a/Blazor/Blazor/Models/PlayerModel.cs +++ b/Blazor/Blazor/Models/PlayerModel.cs @@ -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(); + } + } } diff --git a/Blazor/Blazor/Models/QuestionModel.cs b/Blazor/Blazor/Models/QuestionModel.cs index b4a0b22..ecdc5df 100644 --- a/Blazor/Blazor/Models/QuestionModel.cs +++ b/Blazor/Blazor/Models/QuestionModel.cs @@ -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; } diff --git a/Blazor/Blazor/Pages/Admins/AddAdministrator.razor b/Blazor/Blazor/Pages/Admins/AddAdministrator.razor index 5a940e3..b198619 100644 --- a/Blazor/Blazor/Pages/Admins/AddAdministrator.razor +++ b/Blazor/Blazor/Pages/Admins/AddAdministrator.razor @@ -12,6 +12,8 @@ Username: +

+

AddQuestion

+ + + +

+ +

+

+ @* *@ +

+ @*

+ + +

+

+ + +

+

+ + +

+

+ + +

*@ + + +
diff --git a/Blazor/Blazor/Pages/Questions/AddQuestion.razor.cs b/Blazor/Blazor/Pages/Questions/AddQuestion.razor.cs index 07704b9..e233d06 100644 --- a/Blazor/Blazor/Pages/Questions/AddQuestion.razor.cs +++ b/Blazor/Blazor/Pages/Questions/AddQuestion.razor.cs @@ -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 Logger { get; set; } + + + private async void HandleValidSubmit() + { + + await DataService.Add(questionModel); + + var formData = new List>(); + formData.Add(new KeyValuePair("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"); + } } } diff --git a/Blazor/Blazor/Pages/Questions/EditQuestion.razor b/Blazor/Blazor/Pages/Questions/EditQuestion.razor index e2584ff..5d4a6a6 100644 --- a/Blazor/Blazor/Pages/Questions/EditQuestion.razor +++ b/Blazor/Blazor/Pages/Questions/EditQuestion.razor @@ -1,5 +1,39 @@ -

EditQuestion

+@page "/editQuestion/{Id:int}" -@code { +

EditQuestion

+ + + + + + +

+ + + + + + +

+ + +
-} diff --git a/Blazor/Blazor/Pages/Questions/EditQuestion.razor.cs b/Blazor/Blazor/Pages/Questions/EditQuestion.razor.cs index 43cc64f..7c8130d 100644 --- a/Blazor/Blazor/Pages/Questions/EditQuestion.razor.cs +++ b/Blazor/Blazor/Pages/Questions/EditQuestion.razor.cs @@ -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 Logger { get; set; } + + + private async void HandleValidSubmit() + { + await DataService.Update(Id, questionModel); + + var formData = new List>(); + formData.Add(new KeyValuePair("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(); + } + } + + } } } diff --git a/Blazor/Blazor/Pages/Questions/Questions.razor b/Blazor/Blazor/Pages/Questions/Questions.razor index 5ae8448..cedb724 100644 --- a/Blazor/Blazor/Pages/Questions/Questions.razor +++ b/Blazor/Blazor/Pages/Questions/Questions.razor @@ -8,6 +8,10 @@ Ajouter + + Exporter + +
("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 filteredStrings = new List(); - 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>(); + formData.Add(new KeyValuePair("content", field[0])); + formData.Add(new KeyValuePair("answerContent1", field[2])); + formData.Add(new KeyValuePair("answerContent2", field[3])); + formData.Add(new KeyValuePair("answerContent3", field[4])); + formData.Add(new KeyValuePair("answerContent4", field[5])); + formData.Add(new KeyValuePair("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("idchapter", result.ToString())); } else { - if (filteredString.Length > 0) - { - filteredStrings.Add(filteredString.ToString()); - filteredString.Clear(); - } + var errorResponse = await response.Content.ReadAsStringAsync(); + formData.Add(new KeyValuePair("idchapter", "Unknown_Chapter_Error")); } - } - - if (filteredString.Length > 0) - { - filteredStrings.Add(filteredString.ToString()); - } - foreach (var filteredStr in filteredStrings) - { - var formData = new List>(); - formData.Add(new KeyValuePair("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(); } } } diff --git a/Blazor/Blazor/Pages/_Host.cshtml b/Blazor/Blazor/Pages/_Host.cshtml index e942420..158359b 100644 --- a/Blazor/Blazor/Pages/_Host.cshtml +++ b/Blazor/Blazor/Pages/_Host.cshtml @@ -28,7 +28,7 @@ Reload 🗙 - + diff --git a/Blazor/Blazor/Pages/_Layout.cshtml b/Blazor/Blazor/Pages/_Layout.cshtml index c993101..5e9991a 100644 --- a/Blazor/Blazor/Pages/_Layout.cshtml +++ b/Blazor/Blazor/Pages/_Layout.cshtml @@ -6,6 +6,8 @@ + + diff --git a/Blazor/Blazor/Services/DataLocalService.cs b/Blazor/Blazor/Services/DataLocalService.cs index d8b91cf..8f7c491 100644 --- a/Blazor/Blazor/Services/DataLocalService.cs +++ b/Blazor/Blazor/Services/DataLocalService.cs @@ -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 GetById(int id) { // Get the current data - var currentData = await _localStorage.GetItemAsync>("data"); + var currentData = _http.GetFromJsonAsync>($"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 GetAdminById(int id) { // Get the current data - var currentData = await _localStorage.GetItemAsync>("data"); + var currentData = _http.GetFromJsonAsync>($"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 GetQuestionById(int id) { // Get the current data - var currentData = await _localStorage.GetItemAsync>("data"); + var currentData = _http.GetFromJsonAsync>($"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>("data"); + public async Task Update(int id, QuestionModel model) + { + // Get the current data + var currentData = await _localStorage.GetItemAsync>("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>("data"); + public async Task Add(QuestionModel model) + { + // Get the current data + var currentData = await _localStorage.GetItemAsync>("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 CountQuestion() { @@ -322,7 +321,7 @@ namespace Blazor.Services public async Task GetPlayerById(int id) { // Get the current data - var currentData = await _localStorage.GetItemAsync>("data"); + var currentData = _http.GetFromJsonAsync>($"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); diff --git a/Blazor/Blazor/Services/IDataService.cs b/Blazor/Blazor/Services/IDataService.cs index f453f81..71c6111 100644 --- a/Blazor/Blazor/Services/IDataService.cs +++ b/Blazor/Blazor/Services/IDataService.cs @@ -24,9 +24,9 @@ namespace Blazor.Services Task CountAdmin(); Task> 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 GetQuestionById(int id); diff --git a/Blazor/Blazor/ViewClasses/Question.cs b/Blazor/Blazor/ViewClasses/Question.cs index db4e7bf..4586917 100644 --- a/Blazor/Blazor/ViewClasses/Question.cs +++ b/Blazor/Blazor/ViewClasses/Question.cs @@ -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; - } } diff --git a/Blazor/Blazor/wwwroot/Index.html b/Blazor/Blazor/wwwroot/Index.html index 3470e82..a72ec50 100644 --- a/Blazor/Blazor/wwwroot/Index.html +++ b/Blazor/Blazor/wwwroot/Index.html @@ -6,5 +6,4 @@ Welcome to your new app. - - + \ No newline at end of file