From 23da554720edad70790ab82f469ced167718b66c Mon Sep 17 00:00:00 2001 From: "jade.van_brabandt" Date: Wed, 20 Dec 2023 17:45:02 +0100 Subject: [PATCH] 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