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
if (!string.IsNullOrEmpty(item))
{ {
// Throw exception if the item is null or empty
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 // Validate that the item is one of the allowed values: A, B, C, or D
switch (item) switch (item)
{ {
case "A": case "A":
break;
case "B": case "B":
break;
case "C": case "C":
break;
case "D": case "D":
// Valid values, no action needed
break; break;
default: default:
// Throw exception if the item is not one of the allowed answers // 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."); throw new InvalidDataException($"Invalid item '{item}' provided. Item must be one of: A, B, C, or D.");
}
}
else
{
// Throw exception if the item is null or empty
throw new ArgumentNullException("Invalid item (validateReponse): null given.");
} }
// Return the validated item // Return the validated item
return item; return item;
} }
catch (Exception ex)
{
// In case of an exception, return a default answer ("A")
return "A"; // Default Argument
}
}
} }
} }

@ -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; }

@ -12,7 +12,7 @@ namespace WF_WebAdmin.Pages
{ {
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; }

Loading…
Cancel
Save