Code Smells from Pages/AddQuiz
continuous-integration/drone/push Build is passing Details

master
tomivt 2 months ago
parent 04110bc503
commit cc616955bc

@ -12,7 +12,7 @@ namespace WF_WebAdmin.Model
{ {
public static partial class LoggerSaveStub public static partial class LoggerSaveStub
{ {
public static void Log(ILogger logs, LogLevel logLevel, string message, params object[] args) public static void Log(ILogger? logs, LogLevel logLevel, string message, params object[] args)
{ {
ILogsService logsService = new LogsServiceStub(); ILogsService logsService = new LogsServiceStub();
logsService.addLogs(new Logs(logLevel, string.Format(message, args))); logsService.addLogs(new Logs(logLevel, string.Format(message, args)));

@ -5,52 +5,52 @@
<h3>@Localizer["TitleAddQuiz"]</h3> <h3>@Localizer["TitleAddQuiz"]</h3>
<EditForm Model="@QuizModel" OnValidSubmit="@HandleValidSubmit"> <EditForm Model="@_quizModel" OnValidSubmit="@HandleValidSubmit">
<DataAnnotationsValidator /> <DataAnnotationsValidator />
<ValidationSummary /> <ValidationSummary />
<p> <p>
<label for="display-quest"> <label for="display-quest">
@Localizer["TitleQuestion"] @Localizer["TitleQuestion"]
<InputText id="display-quest" @bind-Value="QuizModel.Question" /> <InputText id="display-quest" @bind-Value="_quizModel.Question" />
</label> </label>
</p> </p>
<p> <p>
<label for="display-a"> <label for="display-a">
@Localizer["AnswerA"] @Localizer["AnswerA"]
<InputText id="display-a" @bind-Value="QuizModel.AnswerA" /> <InputText id="display-a" @bind-Value="_quizModel.AnswerA" />
</label> </label>
</p> </p>
<p> <p>
<label for="display-b"> <label for="display-b">
@Localizer["AnswerB"] @Localizer["AnswerB"]
<InputText id="display-b" @bind-Value="QuizModel.AnswerB" /> <InputText id="display-b" @bind-Value="_quizModel.AnswerB" />
</label> </label>
</p> </p>
<p> <p>
<label for="display-c"> <label for="display-c">
@Localizer["AnswerC"] @Localizer["AnswerC"]
<InputText id="display-c" @bind-Value="QuizModel.AnswerC" /> <InputText id="display-c" @bind-Value="_quizModel.AnswerC" />
</label> </label>
</p> </p>
<p> <p>
<label for="display-d"> <label for="display-d">
@Localizer["AnswerD"] @Localizer["AnswerD"]
<InputText id="display-d" @bind-Value="QuizModel.AnswerD" /> <InputText id="display-d" @bind-Value="_quizModel.AnswerD" />
</label> </label>
</p> </p>
<p> <p>
<label for="cA"> <label for="cA">
@Localizer["GoodAnser"] @Localizer["GoodAnswer"]
<input name="cA" type="radio" @onchange="@(e => OnCAwnserChange("A", e.Value))" /> A <input name="cA" type="radio" @onchange="@(e => OnCAwnserChange("A"))" /> A
<input name="cA" type="radio" @onchange="@(e => OnCAwnserChange("B", e.Value))" /> B <input name="cA" type="radio" @onchange="@(e => OnCAwnserChange("B"))" /> B
<input name="cA" type="radio" @onchange="@(e => OnCAwnserChange("C", e.Value))" /> C <input name="cA" type="radio" @onchange="@(e => OnCAwnserChange("C"))" /> C
<input name="cA" type="radio" @onchange="@(e => OnCAwnserChange("D", e.Value))" /> D <input name="cA" type="radio" @onchange="@(e => OnCAwnserChange("D"))" /> D
</label> </label>
</p> </p>

@ -13,18 +13,18 @@ namespace WF_WebAdmin.Pages
public partial class AddQuiz public partial class AddQuiz
{ {
[Inject] [Inject]
public ILogger<AddQuiz> Logger { get; set; } public ILogger<AddQuiz>? Logger { get; set; }
[Inject] [Inject]
public IStringLocalizer<AddQuiz> Localizer { get; set; } public IStringLocalizer<AddQuiz>? Localizer { get; set; }
[Inject] [Inject]
private IQuizService quizService { get; set; } private IQuizService? QuizService { get; set; }
[Inject] [Inject]
public NavigationManager NavigationManager { get; set; } public NavigationManager? NavigationManager { get; set; }
private QuizModel QuizModel = new(); private readonly QuizModel _quizModel = new();
/// <summary> /// <summary>
/// Handles the valid submission of a quiz form. /// Handles the valid submission of a quiz form.
@ -39,21 +39,21 @@ namespace WF_WebAdmin.Pages
int id; int id;
// Get the current number of quizzes from the quiz service. // Get the current number of quizzes from the quiz service.
id = await quizService.getNbQuiz(); id = await QuizService.getNbQuiz();
// Increment the quiz ID for the new quiz. // Increment the quiz ID for the new quiz.
id++; id++;
// Create a new quiz and add it using the quiz service. // Create a new quiz and add it using the quiz service.
LoggerSaveStub.Log(Logger, LogLevel.Information, $"Creation of the {QuizModel.Question} question"); LoggerSaveStub.Log(Logger, LogLevel.Information, $"Creation of the {_quizModel.Question} question");
await quizService.addQuiz(new Quiz( await QuizService.addQuiz(new Quiz(
id, // New quiz ID id, // New quiz ID
validateInformation(QuizModel.Question), // Validated question ValidateInformation(_quizModel.Question), // Validated question
validateInformation(QuizModel.AnswerA), // Validated answer A ValidateInformation(_quizModel.AnswerA), // Validated answer A
validateInformation(QuizModel.AnswerB), // Validated answer B ValidateInformation(_quizModel.AnswerB), // Validated answer B
validateInformation(QuizModel.AnswerC), // Validated answer C ValidateInformation(_quizModel.AnswerC), // Validated answer C
validateInformation(QuizModel.AnswerD), // Validated answer D ValidateInformation(_quizModel.AnswerD), // Validated answer D
validateReponse(QuizModel.CAnswer) // Validated correct answer ValidateReponse(_quizModel.CAnswer) // Validated correct answer
)); ));
// Navigate to the "modifquiz" page after adding the quiz. // Navigate to the "modifquiz" page after adding the quiz.
@ -69,10 +69,10 @@ namespace WF_WebAdmin.Pages
/// </summary> /// </summary>
/// <param name="item">The selected answer that will be marked as the correct answer.</param> /// <param name="item">The selected answer that will be marked as the correct answer.</param>
/// <param name="checkedValue">The value of the selected option, typically used for validation or additional logic.</param> /// <param name="checkedValue">The value of the selected option, typically used for validation or additional logic.</param>
private void OnCAwnserChange(string item, object checkedValue) private void OnCAwnserChange(string item)
{ {
// Update the correct answer in the QuizModel with the selected answer. // Update the correct answer in the QuizModel with the selected answer.
QuizModel.CAnswer = item; _quizModel.CAnswer = item;
} }
@ -84,9 +84,9 @@ namespace WF_WebAdmin.Pages
/// <returns> /// <returns>
/// Returns the input string as it is for now. The validation logic is yet to be implemented. /// Returns the input string as it is for now. The validation logic is yet to be implemented.
/// </returns> /// </returns>
private static string validateInformation(string item) private string ValidateInformation(string? item)
{ {
return item; // VALIDATION A FAIRE return string.IsNullOrWhiteSpace(item) ? "Valeur par défaut" : item;
} }
/// <summary> /// <summary>
@ -98,43 +98,31 @@ namespace WF_WebAdmin.Pages
/// <returns> /// <returns>
/// Returns the input item if valid (A, B, C, or D). If the item is invalid or null, it returns a default value ("A"). /// Returns the input item if valid (A, B, C, or D). If the item is invalid or null, it returns a default value ("A").
/// </returns> /// </returns>
private static string validateReponse(string item) private static string ValidateReponse(string item)
{ {
try // Check if the item is null or empty
if (string.IsNullOrEmpty(item))
{ {
// Check if the item is not null or empty // Throw exception if the item is null or empty
if (!string.IsNullOrEmpty(item)) throw new ArgumentNullException(nameof(item), "The item cannot be null or empty.");
{
// 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)
// Validate that the item is one of the allowed values: A, B, C, or D
switch (item)
{ {
// In case of an exception, return a default answer ("A") case "A":
return "A"; // Default Argument case "B":
case "C":
case "D":
// Valid values, no action needed
break;
default:
// Throw exception if the item is not one of the allowed answers
throw new InvalidDataException($"Invalid item '{item}' provided. Item must be one of: A, B, C, or D.");
} }
// Return the validated item
return item;
} }
} }
} }

@ -12,7 +12,7 @@ namespace WF_WebAdmin.Pages
public partial class DeleteUser public partial class DeleteUser
{ {
[Inject] [Inject]
public ILogger<DeleteUser> Logger { get; set; } public ILogger<DeleteUser>? Logger { get; set; }
private List<User> users; private List<User> users;

@ -11,7 +11,7 @@ namespace WF_WebAdmin.Pages
public int Id { get; set; } public int Id { get; set; }
[Inject] [Inject]
public ILogger<Edit> Logger { get; set; } public ILogger<Edit>? Logger { get; set; }
[Inject] [Inject]
private IQuoteService quoteService { get; set; } private IQuoteService quoteService { get; set; }

@ -24,7 +24,7 @@ namespace WF_WebAdmin.Pages
private int page = 1; private int page = 1;
[Inject] [Inject]
public ILogger<ModifQuiz> Logger { get; set; } public ILogger<ModifQuiz>? Logger { get; set; }
[Inject] [Inject]
public IStringLocalizer<ModifQuiz> Localizer { get; set; } public IStringLocalizer<ModifQuiz> Localizer { get; set; }

@ -25,7 +25,7 @@ namespace WF_WebAdmin.Pages
private int page = 1; private int page = 1;
[Inject] [Inject]
public ILogger<ModifQuote> Logger { get; set; } public ILogger<ModifQuote>? Logger { get; set; }
[Inject] [Inject]
public IStringLocalizer<ModifQuote> Localizer { get; set; } public IStringLocalizer<ModifQuote> Localizer { get; set; }

@ -10,10 +10,10 @@ namespace WF_WebAdmin.Pages
{ {
public partial class ValidQuiz public partial class ValidQuiz
{ {
private List<Quiz> quizzes; private List<Quiz> quizzes;
[Inject] [Inject]
public ILogger<ValidQuiz> Logger { get; set; } public ILogger<ValidQuiz>? Logger { get; set; }
[Inject] [Inject]
public IStringLocalizer<ValidQuiz> Localizer { get; set; } public IStringLocalizer<ValidQuiz> Localizer { get; set; }
@ -26,74 +26,74 @@ namespace WF_WebAdmin.Pages
[Inject] [Inject]
public IQuizService QuizService { get; set; } public IQuizService QuizService { get; set; }
/// <summary> /// <summary>
/// Initializes the component asynchronously by fetching the quizzes that need validation. /// Initializes the component asynchronously by fetching the quizzes that need validation.
/// This method retrieves a list of quizzes from the `QuizService` that are pending validation when the component is initialized. /// This method retrieves a list of quizzes from the `QuizService` that are pending validation when the component is initialized.
/// </summary> /// </summary>
protected override async Task OnInitializedAsync() protected override async Task OnInitializedAsync()
{ {
// Fetch quizzes that need validation // Fetch quizzes that need validation
quizzes = await QuizService.getQuizzesToValidate(); quizzes = await QuizService.getQuizzesToValidate();
} }
/// <summary> /// <summary>
/// Handles the event when the "Validate" button is clicked for a quiz. /// Handles the event when the "Validate" button is clicked for a quiz.
/// This method calls the `ValidateQuiz` method, passing the specified quiz for validation. /// This method calls the `ValidateQuiz` method, passing the specified quiz for validation.
/// </summary> /// </summary>
/// <param name="quiz">The quiz that is being validated.</param> /// <param name="quiz">The quiz that is being validated.</param>
private void OnValidButton(Quiz quiz) private void OnValidButton(Quiz quiz)
{ {
// Call the ValidateQuiz method to validate the quiz // Call the ValidateQuiz method to validate the quiz
ValidateQuiz(quiz); ValidateQuiz(quiz);
} }
/// <summary> /// <summary>
/// Validates the specified quiz by setting its `IsValid` property to true and updating its state in the service. /// Validates the specified quiz by setting its `IsValid` property to true and updating its state in the service.
/// This method logs a message to the console indicating the quiz has been validated, then updates the quiz's validation status. /// This method logs a message to the console indicating the quiz has been validated, then updates the quiz's validation status.
/// It then calls the `QuizService.updateQuiz` method to persist the changes. /// It then calls the `QuizService.updateQuiz` method to persist the changes.
/// </summary> /// </summary>
/// <param name="quiz">The quiz that is being validated.</param> /// <param name="quiz">The quiz that is being validated.</param>
private void ValidateQuiz(Quiz quiz) private void ValidateQuiz(Quiz quiz)
{ {
// Log the validation action to the console // Log the validation action to the console
LoggerSaveStub.Log(Logger, LogLevel.Information, $"Quiz {quiz.Id} validated!"); LoggerSaveStub.Log(Logger, LogLevel.Information, $"Quiz {quiz.Id} validated!");
Console.WriteLine($"Quiz {quiz.Id} validated!"); Console.WriteLine($"Quiz {quiz.Id} validated!");
// Create a new quiz instance (or modify the existing one) // Create a new quiz instance (or modify the existing one)
Quiz newQuiz = quiz; Quiz newQuiz = quiz;
newQuiz.IsValid = true; newQuiz.IsValid = true;
// Update the quiz state in the QuizService // Update the quiz state in the QuizService
QuizService.updateQuiz(quiz); QuizService.updateQuiz(quiz);
} }
/// <summary> /// <summary>
/// Handles the event when the "Reject" button is clicked for a quiz. /// Handles the event when the "Reject" button is clicked for a quiz.
/// This method calls the `RejectQuiz` method, passing the specified quiz to be rejected. /// This method calls the `RejectQuiz` method, passing the specified quiz to be rejected.
/// </summary> /// </summary>
/// <param name="quiz">The quiz that is being rejected.</param> /// <param name="quiz">The quiz that is being rejected.</param>
private void OnRejectButton(Quiz quiz) private void OnRejectButton(Quiz quiz)
{ {
// Call the RejectQuiz method to reject the quiz // Call the RejectQuiz method to reject the quiz
RejectQuiz(quiz); RejectQuiz(quiz);
} }
/// <summary> /// <summary>
/// Rejects the specified quiz by logging a rejection message and removing it from the QuizService. /// Rejects the specified quiz by logging a rejection message and removing it from the QuizService.
/// This method logs a message to the console indicating the quiz has been rejected, and then calls the `QuizService.removeQuiz` /// This method logs a message to the console indicating the quiz has been rejected, and then calls the `QuizService.removeQuiz`
/// method to remove the quiz from the system. /// method to remove the quiz from the system.
/// </summary> /// </summary>
/// <param name="quiz">The quiz that is being rejected.</param> /// <param name="quiz">The quiz that is being rejected.</param>
private void RejectQuiz(Quiz quiz) private void RejectQuiz(Quiz quiz)
{ {
// Log the rejection action to the console // Log the rejection action to the console
LoggerSaveStub.Log(Logger, LogLevel.Information, $"Quiz {quiz.Id} rejected"); LoggerSaveStub.Log(Logger, LogLevel.Information, $"Quiz {quiz.Id} rejected");
Console.WriteLine($"Quiz {quiz.Id} rejected!"); Console.WriteLine($"Quiz {quiz.Id} rejected!");
// Remove the rejected quiz from the QuizService // Remove the rejected quiz from the QuizService
QuizService.removeQuiz(quiz.Id); QuizService.removeQuiz(quiz.Id);
} }
} }

Loading…
Cancel
Save