|
|
|
@ -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<Chapter> chapters = new();
|
|
|
|
|
|
|
|
|
|
private List<Answer> answers = new();
|
|
|
|
|
|
|
|
|
|
public List<Checkbox> checkboxs = new();
|
|
|
|
|
|
|
|
|
|
public Question question = new();
|
|
|
|
|
|
|
|
|
|
public List<Question> 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<Chapter[]>(API.API_URL + "chapters/" + API.TOKEN).Result;
|
|
|
|
|
if (response == null) chapters = new List<Chapter>();
|
|
|
|
|
else chapters = new List<Chapter>(response);
|
|
|
|
|
|
|
|
|
|
answers = new();
|
|
|
|
|
checkboxs = new();
|
|
|
|
|
var resp = Http.GetFromJsonAsync<Question[]>(API.API_URL + "questions/" + API.TOKEN).Result;
|
|
|
|
|
questions = new List<Question>(resp);
|
|
|
|
|
|
|
|
|
|
question = questions.Find(q => q.Id == Id);
|
|
|
|
|
|
|
|
|
|
IEnumerable<Question> 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<KeyValuePair<string, string>>();
|
|
|
|
|
formData.Add(new KeyValuePair<string, string>("content", questionModel.Content));
|
|
|
|
|
formData.Add(new KeyValuePair<string, string>("idchapter", questionModel.IdChapter.ToString()));
|
|
|
|
|
//foreach (var answerModel in answerModels)
|
|
|
|
|
//{
|
|
|
|
|
// var answercontent = $"answerContent{answerModel.Id + 1}";
|
|
|
|
|
// formData.Add(new KeyValuePair<string, string>(answercontent, answerModel.Content));
|
|
|
|
|
//}
|
|
|
|
|
//foreach (var checkbox in checkboxs)
|
|
|
|
|
//{
|
|
|
|
|
// if (checkbox.IsCorrect != false)
|
|
|
|
|
// {
|
|
|
|
|
// var idgood = checkbox.Id + 1;
|
|
|
|
|
// formData.Add(new KeyValuePair<string, string>("idanswergood", idgood.ToString()));
|
|
|
|
|
// }
|
|
|
|
|
//}
|
|
|
|
|
formData.Add(new KeyValuePair<string, string>("content", question.Content));
|
|
|
|
|
formData.Add(new KeyValuePair<string, string>("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<string, string>(answercontent, answer.Content));
|
|
|
|
|
}
|
|
|
|
|
foreach (var checkbox in checkboxs)
|
|
|
|
|
{
|
|
|
|
|
if (checkbox.IsCorrect == true)
|
|
|
|
|
{
|
|
|
|
|
formData.Add(new KeyValuePair<string, string>("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");
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|