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 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();
logsService.addLogs(new Logs(logLevel, string.Format(message, args)));

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

@ -13,18 +13,18 @@ namespace WF_WebAdmin.Pages
public partial class AddQuiz
{
[Inject]
public ILogger<AddQuiz> Logger { get; set; }
public ILogger<AddQuiz>? Logger { get; set; }
[Inject]
public IStringLocalizer<AddQuiz> Localizer { get; set; }
public IStringLocalizer<AddQuiz>? Localizer { get; set; }
[Inject]
private IQuizService quizService { get; set; }
private IQuizService? QuizService { get; set; }
[Inject]
public NavigationManager NavigationManager { get; set; }
public NavigationManager? NavigationManager { get; set; }
private QuizModel QuizModel = new();
private readonly QuizModel _quizModel = new();
/// <summary>
/// Handles the valid submission of a quiz form.
@ -39,21 +39,21 @@ namespace WF_WebAdmin.Pages
int id;
// 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.
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(
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
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.
@ -69,10 +69,10 @@ namespace WF_WebAdmin.Pages
/// </summary>
/// <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>
private void OnCAwnserChange(string item, object checkedValue)
private void OnCAwnserChange(string item)
{
// 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 the input string as it is for now. The validation logic is yet to be implemented.
/// </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>
@ -98,43 +98,31 @@ namespace WF_WebAdmin.Pages
/// <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>
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))
{
// 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;
// Throw exception if the item is null or empty
throw new ArgumentNullException(nameof(item), "The item cannot be null or empty.");
}
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")
return "A"; // Default Argument
case "A":
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
{
[Inject]
public ILogger<DeleteUser> Logger { get; set; }
public ILogger<DeleteUser>? Logger { get; set; }
private List<User> users;

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

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

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

@ -12,7 +12,7 @@ namespace WF_WebAdmin.Pages
{
private List<Quiz> quizzes;
[Inject]
public ILogger<ValidQuiz> Logger { get; set; }
public ILogger<ValidQuiz>? Logger { get; set; }
[Inject]
public IStringLocalizer<ValidQuiz> Localizer { get; set; }

Loading…
Cancel
Save