From 5ad531d6bb63cdf1bc4f5f7b300777a1b1d0c291 Mon Sep 17 00:00:00 2001 From: Louis GUICHARD-MONTGUERS Date: Sat, 8 Feb 2025 19:03:44 +0100 Subject: [PATCH] =?UTF-8?q?Mise=20=C3=A0=20jour=20de=20'WF-WebAdmin/WF-Web?= =?UTF-8?q?Admin/Pages/AddQuiz.razor.cs'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../WF-WebAdmin/Pages/AddQuiz.razor.cs | 280 +++++++++--------- 1 file changed, 140 insertions(+), 140 deletions(-) diff --git a/WF-WebAdmin/WF-WebAdmin/Pages/AddQuiz.razor.cs b/WF-WebAdmin/WF-WebAdmin/Pages/AddQuiz.razor.cs index 8a66ce0..c88d921 100644 --- a/WF-WebAdmin/WF-WebAdmin/Pages/AddQuiz.razor.cs +++ b/WF-WebAdmin/WF-WebAdmin/Pages/AddQuiz.razor.cs @@ -1,140 +1,140 @@ -using Microsoft.AspNetCore.Components; -using WF_WebAdmin.Service; -using WF_WebAdmin.Model; -using Microsoft.AspNetCore.Mvc; -using System.Text.RegularExpressions; -using Microsoft.Extensions.Localization; -using Microsoft.Extensions.Logging; -using System.Security.Claims; - - -namespace WF_WebAdmin.Pages -{ - public partial class AddQuiz - { - [Inject] - public ILogger Logger { get; set; } - - [Inject] - public IStringLocalizer Localizer { get; set; } - - [Inject] - private IQuizService quizService { get; set; } - - [Inject] - public NavigationManager NavigationManager { get; set; } - - private QuizModel QuizModel = new(); - - /// - /// Handles the valid submission of a quiz form. - /// This method is triggered when the form is successfully validated and the user submits the quiz data. - /// It retrieves the current quiz count, increments it, and then adds a new quiz entry to the quiz service. - /// Finally, it navigates to the "modifquiz" page. - /// - - private async void HandleValidSubmit() - { - // Declare a variable to hold the ID of the new quiz. - int id; - - // Get the current number of quizzes from the quiz service. - id = await quizService.getNbQuiz(); - - // Increment the quiz ID for the new quiz. - id++; - - // Create a new quiz and add it using the quiz service. - LoggerSaveStub.Log(Logger, LogLevel.Information, $"Creation of the {QuizModel.Question} question"); - await quizService.addQuiz(new Quiz( - id, // New quiz ID - validateInformation(QuizModel.Question), // Validated question - validateInformation(QuizModel.AnswerA), // Validated answer A - validateInformation(QuizModel.AnswerB), // Validated answer B - validateInformation(QuizModel.AnswerC), // Validated answer C - validateInformation(QuizModel.AnswerD), // Validated answer D - validateReponse(QuizModel.CAnswer) // Validated correct answer - )); - - // Navigate to the "modifquiz" page after adding the quiz. - NavigationManager.NavigateTo("modifquiz"); - } - - - - /// - /// Handles the change of the correct answer for the quiz. - /// This method is triggered when the user selects or changes the correct answer for the quiz question. - /// It updates the QuizModel's Correct Answer property with the selected answer. - /// - /// The selected answer that will be marked as the correct answer. - /// The value of the selected option, typically used for validation or additional logic. - private void OnCAwnserChange(string item, object checkedValue) - { - // Update the correct answer in the QuizModel with the selected answer. - QuizModel.CAnswer = item; - } - - - /// - /// Validates the provided string item. - /// This method is used to validate input data, but the validation logic is not yet implemented. - /// - /// The string input to be validated. - /// - /// Returns the input string as it is for now. The validation logic is yet to be implemented. - /// - private static string validateInformation(string item) - { - return item; // VALIDATION A FAIRE - } - - /// - /// Validates the provided answer item (A, B, C, or D) for the quiz. - /// This method ensures that the input corresponds to one of the allowed values for the correct answer. - /// If the input is invalid or null, it throws an exception and returns a default value ("A") in case of error. - /// - /// The answer item (A, B, C, or D) to be validated. - /// - /// Returns the input item if valid (A, B, C, or D). If the item is invalid or null, it returns a default value ("A"). - /// - private static string validateReponse(string item) - { - try - { - // Check if the item is not null or empty - if (!string.IsNullOrEmpty(item)) - { - // Validate that the item is one of the allowed values: A, B, C, or D - switch (item) - { - case "A": - break; - case "B": - break; - case "C": - break; - case "D": - break; - default: - // Throw exception if the item is not one of the allowed answers - throw new InvalidDataException("Invalid item (validateReponse) : item must be A,B,C or D " + item + " given."); - } - } - else - { - // Throw exception if the item is null or empty - throw new ArgumentNullException("Invalid item (validateReponse): null given."); - } - - // Return the validated item - return item; - } - catch (Exception ex) - { - // In case of an exception, return a default answer ("A") - return "A"; // Default Argument - } - } - } -} +using Microsoft.AspNetCore.Components; +using WF_WebAdmin.Service; +using WF_WebAdmin.Model; +using Microsoft.AspNetCore.Mvc; +using System.Text.RegularExpressions; +using Microsoft.Extensions.Localization; +using Microsoft.Extensions.Logging; +using System.Security.Claims; + + +namespace WF_WebAdmin.Pages +{ + public partial class AddQuiz + { + [Inject] + public ILogger Logger { get; set; } + + [Inject] + public IStringLocalizer Localizer { get; set; } + + [Inject] + private IQuizService quizService { get; set; } + + [Inject] + public NavigationManager NavigationManager { get; set; } + + private QuizModel QuizModel = new(); + + /// + /// Handles the valid submission of a quiz form. + /// This method is triggered when the form is successfully validated and the user submits the quiz data. + /// It retrieves the current quiz count, increments it, and then adds a new quiz entry to the quiz service. + /// Finally, it navigates to the "modifquiz" page. + /// + + private async Task HandleValidSubmit() + { + // Declare a variable to hold the ID of the new quiz. + int id; + + // Get the current number of quizzes from the quiz service. + id = await quizService.getNbQuiz(); + + // Increment the quiz ID for the new quiz. + id++; + + // Create a new quiz and add it using the quiz service. + LoggerSaveStub.Log(Logger, LogLevel.Information, $"Creation of the {QuizModel.Question} question"); + await quizService.addQuiz(new Quiz( + id, // New quiz ID + validateInformation(QuizModel.Question), // Validated question + validateInformation(QuizModel.AnswerA), // Validated answer A + validateInformation(QuizModel.AnswerB), // Validated answer B + validateInformation(QuizModel.AnswerC), // Validated answer C + validateInformation(QuizModel.AnswerD), // Validated answer D + validateReponse(QuizModel.CAnswer) // Validated correct answer + )); + + // Navigate to the "modifquiz" page after adding the quiz. + NavigationManager.NavigateTo("modifquiz"); + } + + + + /// + /// Handles the change of the correct answer for the quiz. + /// This method is triggered when the user selects or changes the correct answer for the quiz question. + /// It updates the QuizModel's Correct Answer property with the selected answer. + /// + /// The selected answer that will be marked as the correct answer. + /// The value of the selected option, typically used for validation or additional logic. + private void OnCAwnserChange(string item, object checkedValue) + { + // Update the correct answer in the QuizModel with the selected answer. + QuizModel.CAnswer = item; + } + + + /// + /// Validates the provided string item. + /// This method is used to validate input data, but the validation logic is not yet implemented. + /// + /// The string input to be validated. + /// + /// Returns the input string as it is for now. The validation logic is yet to be implemented. + /// + private static string validateInformation(string item) + { + return item; // VALIDATION A FAIRE + } + + /// + /// Validates the provided answer item (A, B, C, or D) for the quiz. + /// This method ensures that the input corresponds to one of the allowed values for the correct answer. + /// If the input is invalid or null, it throws an exception and returns a default value ("A") in case of error. + /// + /// The answer item (A, B, C, or D) to be validated. + /// + /// Returns the input item if valid (A, B, C, or D). If the item is invalid or null, it returns a default value ("A"). + /// + private static string validateReponse(string item) + { + try + { + // Check if the item is not null or empty + if (!string.IsNullOrEmpty(item)) + { + // Validate that the item is one of the allowed values: A, B, C, or D + switch (item) + { + case "A": + break; + case "B": + break; + case "C": + break; + case "D": + break; + default: + // Throw exception if the item is not one of the allowed answers + throw new InvalidDataException("Invalid item (validateReponse) : item must be A,B,C or D " + item + " given."); + } + } + else + { + // Throw exception if the item is null or empty + throw new ArgumentNullException("Invalid item (validateReponse): null given."); + } + + // Return the validated item + return item; + } + catch (Exception ex) + { + // In case of an exception, return a default answer ("A") + return "A"; // Default Argument + } + } + } +}