From 50af63099e8f83e5b741f1c9e27e84ba32acbc92 Mon Sep 17 00:00:00 2001 From: "maxence.guitard" Date: Wed, 20 Dec 2023 17:34:35 +0100 Subject: [PATCH 1/4] fix : add et edit de toute les pages --- Blazor/Blazor/Models/PlayerModel.cs | 18 +++++++ Blazor/Blazor/Models/QuestionModel.cs | 16 ++---- .../Pages/Admins/AddAdministrator.razor | 2 + .../Pages/Admins/AddAdministrator.razor.cs | 2 +- .../Pages/Admins/EditAdministrator.razor.cs | 2 +- .../Blazor/Pages/Chapters/AddChapter.razor.cs | 2 +- .../Pages/Chapters/EditChapter.razor.cs | 2 +- .../Blazor/Pages/Players/AddPlayer.razor.cs | 5 +- .../Blazor/Pages/Players/EditPlayer.razor.cs | 10 ++-- .../Blazor/Pages/Questions/AddQuestion.razor | 48 ++++++++++++++++- .../Pages/Questions/AddQuestion.razor.cs | 51 ++++++++++++++++++- Blazor/Blazor/Services/DataLocalService.cs | 39 +++++++------- Blazor/Blazor/Services/IDataService.cs | 4 +- Blazor/Blazor/ViewClasses/Question.cs | 11 +--- 14 files changed, 157 insertions(+), 55 deletions(-) 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..368c8db 100644 --- a/Blazor/Blazor/Models/QuestionModel.cs +++ b/Blazor/Blazor/Models/QuestionModel.cs @@ -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; } 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/Services/DataLocalService.cs b/Blazor/Blazor/Services/DataLocalService.cs index d8b91cf..76ee321 100644 --- a/Blazor/Blazor/Services/DataLocalService.cs +++ b/Blazor/Blazor/Services/DataLocalService.cs @@ -28,7 +28,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 +135,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 +228,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); @@ -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>("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..f44dac1 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; - } } From 23da554720edad70790ab82f469ced167718b66c Mon Sep 17 00:00:00 2001 From: "jade.van_brabandt" Date: Wed, 20 Dec 2023 17:45:02 +0100 Subject: [PATCH 2/4] Feat : Import/export Question (API may not be implemented now) --- .../Blazor/Pages/Questions/AddQuestion.razor | 2 +- .../Blazor/Pages/Questions/EditQuestion.razor | 4 +- Blazor/Blazor/Pages/Questions/Questions.razor | 4 ++ .../Blazor/Pages/Questions/Questions.razor.cs | 72 +++++++++---------- Blazor/Blazor/Pages/_Host.cshtml | 2 +- Blazor/Blazor/Pages/_Layout.cshtml | 2 + Blazor/Blazor/wwwroot/Index.html | 3 +- 7 files changed, 48 insertions(+), 41 deletions(-) diff --git a/Blazor/Blazor/Pages/Questions/AddQuestion.razor b/Blazor/Blazor/Pages/Questions/AddQuestion.razor index a39c7f4..1fef3d7 100644 --- a/Blazor/Blazor/Pages/Questions/AddQuestion.razor +++ b/Blazor/Blazor/Pages/Questions/AddQuestion.razor @@ -1,4 +1,4 @@ -@page "/addAdministrator" +@page "/addQuestion" @using Blazor.Models

AddQuestion

diff --git a/Blazor/Blazor/Pages/Questions/EditQuestion.razor b/Blazor/Blazor/Pages/Questions/EditQuestion.razor index e2584ff..665bdbe 100644 --- a/Blazor/Blazor/Pages/Questions/EditQuestion.razor +++ b/Blazor/Blazor/Pages/Questions/EditQuestion.razor @@ -1,4 +1,6 @@ -

EditQuestion

+@page "/editQuestion" + +

EditQuestion

@code { 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 + + filteredStrings = new List(); - 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>(); + 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/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 From cf83d8d840f5b237b8a0d5f906db73edee76d485 Mon Sep 17 00:00:00 2001 From: "yvan.calatayud" Date: Wed, 20 Dec 2023 17:55:54 +0100 Subject: [PATCH 3/4] feat : delete --- Blazor/Blazor/Models/QuestionModel.cs | 15 ++---- .../Pages/Admins/Administrators.razor.cs | 21 +++++++-- .../Blazor/Pages/Chapters/Chapters.razor.cs | 25 ++++++++-- Blazor/Blazor/Pages/Players/Players.razor.cs | 22 +++++++-- .../Blazor/Pages/Questions/AddQuestion.razor | 2 +- .../Blazor/Pages/Questions/EditQuestion.razor | 40 ++++++++++++++-- .../Pages/Questions/EditQuestion.razor.cs | 46 +++++++++++++++++++ .../Blazor/Pages/Questions/Questions.razor.cs | 22 +++++++-- Blazor/Blazor/Services/DataLocalService.cs | 40 ++++++++-------- Blazor/Blazor/Services/IDataService.cs | 4 +- 10 files changed, 187 insertions(+), 50 deletions(-) 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/Administrators.razor.cs b/Blazor/Blazor/Pages/Admins/Administrators.razor.cs index 5773514..3eb7cda 100644 --- a/Blazor/Blazor/Pages/Admins/Administrators.razor.cs +++ b/Blazor/Blazor/Pages/Admins/Administrators.razor.cs @@ -85,14 +85,29 @@ public partial class Administrators parameters.Add(nameof(Administrator.Id), id); var modal = Modal.Show("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); diff --git a/Blazor/Blazor/Pages/Chapters/Chapters.razor.cs b/Blazor/Blazor/Pages/Chapters/Chapters.razor.cs index eec53b1..f79c412 100644 --- a/Blazor/Blazor/Pages/Chapters/Chapters.razor.cs +++ b/Blazor/Blazor/Pages/Chapters/Chapters.razor.cs @@ -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("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); diff --git a/Blazor/Blazor/Pages/Players/Players.razor.cs b/Blazor/Blazor/Pages/Players/Players.razor.cs index 7cb9b95..aa9bc5a 100644 --- a/Blazor/Blazor/Pages/Players/Players.razor.cs +++ b/Blazor/Blazor/Pages/Players/Players.razor.cs @@ -46,14 +46,30 @@ public partial class Players parameters.Add(nameof(Player.Id), id); var modal = Modal.Show("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); diff --git a/Blazor/Blazor/Pages/Questions/AddQuestion.razor b/Blazor/Blazor/Pages/Questions/AddQuestion.razor index a39c7f4..1fef3d7 100644 --- a/Blazor/Blazor/Pages/Questions/AddQuestion.razor +++ b/Blazor/Blazor/Pages/Questions/AddQuestion.razor @@ -1,4 +1,4 @@ -@page "/addAdministrator" +@page "/addQuestion" @using Blazor.Models

AddQuestion

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.cs b/Blazor/Blazor/Pages/Questions/Questions.razor.cs index b30e35e..3b7f8e8 100644 --- a/Blazor/Blazor/Pages/Questions/Questions.razor.cs +++ b/Blazor/Blazor/Pages/Questions/Questions.razor.cs @@ -45,14 +45,30 @@ public partial class Questions parameters.Add(nameof(Question.Id), id); var modal = Modal.Show("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); diff --git a/Blazor/Blazor/Services/DataLocalService.cs b/Blazor/Blazor/Services/DataLocalService.cs index d8b91cf..93e38a6 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 @@ -242,30 +243,29 @@ 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) - //{ + public async Task Add(QuestionModel model) + { // // Get the current data // var currentData = await _localStorage.GetItemAsync>("data"); @@ -283,7 +283,7 @@ namespace Blazor.Services // // Save the data // await _localStorage.SetItemAsync("data", currentData); - //} + } public async Task CountQuestion() { 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); From ef3940f44e371165840396b2c3b4c53378aeabd7 Mon Sep 17 00:00:00 2001 From: "yvan.calatayud" Date: Wed, 20 Dec 2023 18:10:17 +0100 Subject: [PATCH 4/4] fix : delete --- Blazor/Blazor/Modals/DeleteConfirmation.razor | 2 +- Blazor/Blazor/Modals/DeleteConfirmation.razor.cs | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) 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() {