diff --git a/Blazor/Blazor/Models/AnswerModel.cs b/Blazor/Blazor/Models/AnswerModel.cs
index 6b59e83..ec2d19c 100644
--- a/Blazor/Blazor/Models/AnswerModel.cs
+++ b/Blazor/Blazor/Models/AnswerModel.cs
@@ -10,4 +10,6 @@ public class AnswerModel
{
Id = id;
}
+
+ public AnswerModel(){}
}
diff --git a/Blazor/Blazor/Pages/Questions/AddQuestion.razor.cs b/Blazor/Blazor/Pages/Questions/AddQuestion.razor.cs
index 551d1fd..2c522f0 100644
--- a/Blazor/Blazor/Pages/Questions/AddQuestion.razor.cs
+++ b/Blazor/Blazor/Pages/Questions/AddQuestion.razor.cs
@@ -34,6 +34,8 @@ namespace Blazor.Pages.Questions
public bool IsCorrect { get; set; }
public Checkbox(int id) { Id = id; IsCorrect = false; }
+ public Checkbox() { }
+ public Checkbox(int id, bool isCorrect) { Id = id; IsCorrect = isCorrect; }
}
protected override async Task OnInitializedAsync()
diff --git a/Blazor/Blazor/Pages/Questions/EditQuestion.razor b/Blazor/Blazor/Pages/Questions/EditQuestion.razor
index 5d4a6a6..0dcba7b 100644
--- a/Blazor/Blazor/Pages/Questions/EditQuestion.razor
+++ b/Blazor/Blazor/Pages/Questions/EditQuestion.razor
@@ -3,37 +3,46 @@
EditQuestion
-
+
-
-
-
-
-
-
+
+
+ @foreach (var index in Enumerable.Range(0, answers.Count))
+ {
+ var answer = answers[index];
+ var checkbox = checkboxs[index];
+
+
+
+
+
+ }
diff --git a/Blazor/Blazor/Pages/Questions/EditQuestion.razor.cs b/Blazor/Blazor/Pages/Questions/EditQuestion.razor.cs
index 948ce85..0c5741e 100644
--- a/Blazor/Blazor/Pages/Questions/EditQuestion.razor.cs
+++ b/Blazor/Blazor/Pages/Questions/EditQuestion.razor.cs
@@ -1,7 +1,10 @@
using Blazor.Models;
using Blazor.Services;
+using Blazor.ViewClasses;
using Microsoft.AspNetCore.Components;
+using System;
using static Blazor.Pages.Questions.AddQuestion;
+using static System.Net.WebRequestMethods;
namespace Blazor.Pages.Questions
{
@@ -11,11 +14,22 @@ namespace Blazor.Pages.Questions
[Parameter]
public int Id { get; set; }
- private QuestionModel? questionModel;
+ public List chapters = new();
+
+ private List answers = new();
+
+ public List checkboxs = new();
+
+ public Question question = new();
+
+ public List questions = new();
[Inject]
public required IDataService DataService { get; set; }
+ [Inject]
+ public required HttpClient Http { get; set; }
+
[Inject]
public required NavigationManager NavigationManager { get; set; }
@@ -27,38 +41,57 @@ namespace Blazor.Pages.Questions
protected override async Task OnInitializedAsync()
{
- var question = await DataService.GetQuestionById(Id);
+ var response = Http.GetFromJsonAsync(API.API_URL + "chapters/" + API.TOKEN).Result;
+ if (response == null) chapters = new List();
+ else chapters = new List(response);
+
+ answers = new();
+ checkboxs = new();
+ var resp = Http.GetFromJsonAsync(API.API_URL + "questions/" + API.TOKEN).Result;
+ questions = new List(resp);
+
+ question = questions.Find(q => q.Id == Id);
+
+ IEnumerable foundQuestions = questions.Where(q => q.Id == Id);
+
+ foreach (var q in foundQuestions)
+ {
+ answers.Add(new Answer(q.A_id, q.A_content, q.Id));
+ checkboxs.Add(new Checkbox(q.A_id,false));
+ }
- questionModel = new QuestionModel
+ foreach (var checkbox in checkboxs)
{
- Id = question.Id,
- Content = question.Content
- };
+ if (question.IdAnswerGood == checkbox.Id)
+ {
+ checkbox.IsCorrect = true;
+ }
+ }
+
}
private async Task HandleValidSubmit()
{
- await DataService.Update(Id, questionModel);
var formData = new List>();
- formData.Add(new KeyValuePair("content", questionModel.Content));
- formData.Add(new KeyValuePair("idchapter", questionModel.IdChapter.ToString()));
- //foreach (var answerModel in answerModels)
- //{
- // var answercontent = $"answerContent{answerModel.Id + 1}";
- // formData.Add(new KeyValuePair(answercontent, answerModel.Content));
- //}
- //foreach (var checkbox in checkboxs)
- //{
- // if (checkbox.IsCorrect != false)
- // {
- // var idgood = checkbox.Id + 1;
- // formData.Add(new KeyValuePair("idanswergood", idgood.ToString()));
- // }
- //}
+ formData.Add(new KeyValuePair("content", question.Content));
+ formData.Add(new KeyValuePair("idchapter", question.IdChapter.ToString()));
+ foreach(var index in Enumerable.Range(0, answers.Count))
+ {
+ var answer = answers[index];
+ var answercontent = $"answerContent{index + 1}";
+ formData.Add(new KeyValuePair(answercontent, answer.Content));
+ }
+ foreach (var checkbox in checkboxs)
+ {
+ if (checkbox.IsCorrect == true)
+ {
+ formData.Add(new KeyValuePair("idanswergood", checkbox.Id.ToString()));
+ }
+ }
var formContent = new FormUrlEncodedContent(formData);
- string apiUri = API.API_URL+"/update/questions/" + questionModel.Id + "/" + API.TOKEN;
+ string apiUri = API.API_URL+"/update/question/" + question.Id + "/" + API.TOKEN;
using (var httpClient = new HttpClient())
{
@@ -73,6 +106,7 @@ namespace Blazor.Pages.Questions
var errorResponse = await response.Content.ReadAsStringAsync();
}
}
+ NavigationManager.NavigateTo("questions");
}
}
diff --git a/Blazor/Blazor/Services/DataLocalService.cs b/Blazor/Blazor/Services/DataLocalService.cs
index d36c650..8b5542c 100644
--- a/Blazor/Blazor/Services/DataLocalService.cs
+++ b/Blazor/Blazor/Services/DataLocalService.cs
@@ -241,6 +241,23 @@ namespace Blazor.Services
return question;
}
+ public async Task> GetQuestionsById(int id)
+ {
+ // Get the current data
+ var currentData = await _http.GetFromJsonAsync>(API.API_URL + "questions/" + API.TOKEN);
+
+ // Get all questions with the specified ID
+ var questions = currentData.Where(w => w.Id == id).ToList();
+
+ // Check if any questions were found
+ if (questions.Count == 0)
+ {
+ throw new Exception($"Unable to find questions with ID: {id}");
+ }
+
+ return questions;
+ }
+
public async Task Update(int id, QuestionModel model)
{
// Get the current data
@@ -407,6 +424,23 @@ namespace Blazor.Services
return (await _localStorage.GetItemAsync("data")).Skip((currentPage - 1) * pageSize).Take(pageSize).ToList();
}
+
+ public async Task GetAnswerByIdQuestion(int id)
+ {
+ // Get the current data
+ var currentData = _http.GetFromJsonAsync>(API.API_URL + "answer/" + API.TOKEN).Result;
+
+ // Get the question int the list
+ var answer = currentData.FirstOrDefault(w => w.Id == id);
+
+ // Check if question exist
+ if (answer == null)
+ {
+ throw new Exception($"Unable to found the item with ID: {id}");
+ }
+
+ return answer;
+ }
}
}
\ No newline at end of file
diff --git a/Blazor/Blazor/Services/IDataService.cs b/Blazor/Blazor/Services/IDataService.cs
index 71c6111..b5afc9e 100644
--- a/Blazor/Blazor/Services/IDataService.cs
+++ b/Blazor/Blazor/Services/IDataService.cs
@@ -27,8 +27,8 @@ namespace Blazor.Services
Task Add(QuestionModel model);
Task Update(int id, QuestionModel model);
-
Task GetQuestionById(int id);
+ Task> GetQuestionsById(int id);
Task CountQuestion();
Task> ListQuestion(int currentPage, int pageSize);
@@ -44,5 +44,6 @@ namespace Blazor.Services
Task CountPlayer();
Task> ListPlayer(int currentPage, int pageSize);
+ Task GetAnswerByIdQuestion(int id);
}
}