You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
203 lines
9.4 KiB
203 lines
9.4 KiB
using System.Text.Json;
|
|
using WF_WebAdmin.Model;
|
|
|
|
namespace WF_WebAdmin.Service;
|
|
|
|
public class QuizServiceStub: IQuizService
|
|
{
|
|
private readonly string _jsonFilePath = Path.Combine(Environment.CurrentDirectory, "wwwroot", "fake_data_quiz.json");
|
|
|
|
|
|
/// <summary>
|
|
/// Asynchronously saves a list of quiz objects to a JSON file.
|
|
/// </summary>
|
|
/// <param name="quizzes">A list of <see cref="Quiz"/> objects to be serialized and saved.</param>
|
|
/// <returns>A task representing the asynchronous operation.</returns>
|
|
/// <remarks>
|
|
/// This method serializes the list of quizzes to a well-formatted JSON string and saves it
|
|
/// to a specified file path. The <paramref name="quizzes"/> list is serialized using
|
|
/// <see cref="JsonSerializer"/> with indented formatting to make the JSON human-readable.
|
|
/// </remarks>
|
|
public async Task saveQuizJson(List<Quiz> quizzes)
|
|
{
|
|
var json = JsonSerializer.Serialize(quizzes, new JsonSerializerOptions { WriteIndented = true });
|
|
await File.WriteAllTextAsync(_jsonFilePath, json);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Asynchronously adds a new quiz to the list of quizzes and saves the updated list to a JSON file.
|
|
/// </summary>
|
|
/// <param name="quiz">The <see cref="Quiz"/> object to be added to the list of quizzes.</param>
|
|
/// <returns>A task representing the asynchronous operation.</returns>
|
|
/// <remarks>
|
|
/// This method retrieves the current list of quizzes using <see cref="getQuizzes"/>, assigns a unique ID to the
|
|
/// new quiz (based on the highest existing ID), and adds the new quiz to the list. Afterward, the updated list
|
|
/// of quizzes is saved back to the JSON file using <see cref="saveQuizJson"/>. The new quiz will have an ID
|
|
/// that's one greater than the highest existing ID or 1 if no quizzes exist.
|
|
/// </remarks>
|
|
public async Task addQuiz(Quiz quiz)
|
|
{
|
|
var data = await getQuizzes();
|
|
quiz.Id = data.Count > 0 ? data.Max(p => p.Id) + 1 : 1;
|
|
data.Add(quiz);
|
|
await saveQuizJson(data);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Asynchronously updates an existing quiz in the list of quizzes and saves the updated list to a JSON file.
|
|
/// </summary>
|
|
/// <param name="quiz">The <see cref="Quiz"/> object containing the updated data.</param>
|
|
/// <returns>A task representing the asynchronous operation.</returns>
|
|
/// <remarks>
|
|
/// This method retrieves the current list of quizzes using <see cref="getQuizzes"/>, searches for the quiz
|
|
/// with the same ID as the one provided, and updates its properties with the new values from the given quiz object.
|
|
/// If the quiz is found, the updated list is saved back to the JSON file using <see cref="saveQuizJson"/>.
|
|
/// If no quiz with the matching ID is found, no update is performed.
|
|
/// </remarks>
|
|
public async Task updateQuiz(Quiz quiz)
|
|
{
|
|
var data = await getQuizzes();
|
|
var existingQuiz = data.FirstOrDefault(q => q.Id == quiz.Id);
|
|
if (existingQuiz != null)
|
|
{
|
|
existingQuiz.Question = quiz.Question;
|
|
existingQuiz.AnswerA = quiz.AnswerA;
|
|
existingQuiz.AnswerB = quiz.AnswerB;
|
|
existingQuiz.AnswerC = quiz.AnswerC;
|
|
existingQuiz.AnswerD = quiz.AnswerD;
|
|
existingQuiz.CAnswer = quiz.CAnswer;
|
|
existingQuiz.IsValid = quiz.IsValid;
|
|
existingQuiz.UserProposition = quiz.UserProposition;
|
|
await saveQuizJson(data);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Asynchronously removes a quiz from the list of quizzes by its ID and saves the updated list to a JSON file.
|
|
/// </summary>
|
|
/// <param name="id">The ID of the <see cref="Quiz"/> to be removed.</param>
|
|
/// <returns>A task representing the asynchronous operation.</returns>
|
|
/// <remarks>
|
|
/// This method retrieves the current list of quizzes using <see cref="getQuizzes"/>, searches for the quiz
|
|
/// with the specified ID, and removes it from the list if found. After removal, the updated list of quizzes is
|
|
/// saved back to the JSON file using <see cref="saveQuizJson"/>. If no quiz with the matching ID is found,
|
|
/// no changes are made.
|
|
/// </remarks>
|
|
public async Task removeQuiz(int id)
|
|
{
|
|
var data = await getQuizzes();
|
|
var quiz = data.FirstOrDefault(q => q.Id == id);
|
|
if (quiz != null)
|
|
{
|
|
data.Remove(quiz);
|
|
await saveQuizJson(data);
|
|
}
|
|
}
|
|
|
|
public Task validateQuiz(int id)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Asynchronously retrieves the list of quizzes from a JSON file.
|
|
/// </summary>
|
|
/// <returns>A task representing the asynchronous operation, with a <see cref="List{Quiz}"/> result containing the quizzes.</returns>
|
|
/// <remarks>
|
|
/// This method checks if the JSON file exists at the specified file path. If the file does not exist, it logs a
|
|
/// message to the console and returns an empty list of quizzes. If the file exists, it reads the JSON content,
|
|
/// deserializes it into a list of <see cref="Quiz"/> objects, and returns the list. If the deserialization is
|
|
/// unsuccessful or the file is empty, it returns an empty list instead.
|
|
/// </remarks>
|
|
public async Task<List<Quiz>> getQuizzes()
|
|
{
|
|
if (!File.Exists(_jsonFilePath))
|
|
{
|
|
Console.Out.WriteLine($"{_jsonFilePath} not found");
|
|
return new List<Quiz>();
|
|
}
|
|
|
|
var json = await File.ReadAllTextAsync(_jsonFilePath);
|
|
return JsonSerializer.Deserialize<List<Quiz>>(json) ?? new List<Quiz>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Asynchronously retrieves the list of quizzes that are marked as invalid and need validation.
|
|
/// </summary>
|
|
/// <returns>A task representing the asynchronous operation, with a <see cref="List{Quiz}"/> result containing quizzes that are not valid.</returns>
|
|
/// <remarks>
|
|
/// This method retrieves the full list of quizzes using <see cref="getQuizzes"/> and filters it to return only those
|
|
/// quizzes where the <see cref="Quiz.IsValid"/> property is set to <c>false</c>. The filtered list is then returned.
|
|
/// If no quizzes are invalid, an empty list will be returned.
|
|
/// </remarks>
|
|
public async Task<List<Quiz>> getQuizzesToValidate()
|
|
{
|
|
var quizzes = await getQuizzes();
|
|
return quizzes.Where(quiz => !quiz.IsValid).ToList();
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Asynchronously retrieves a specific quiz by its ID from the list of quizzes.
|
|
/// </summary>
|
|
/// <param name="id">The ID of the <see cref="Quiz"/> to retrieve.</param>
|
|
/// <returns>A task representing the asynchronous operation, with a <see cref="Quiz"/> result containing the matching quiz, or <c>null</c> if not found.</returns>
|
|
/// <remarks>
|
|
/// This method retrieves the full list of quizzes using <see cref="getQuizzes"/> and searches for a quiz with
|
|
/// the specified ID. If a quiz with the matching ID is found, it is returned; otherwise, the method returns <c>null</c>.
|
|
/// </remarks>
|
|
public async Task<Quiz> getQuiz(int id)
|
|
{
|
|
var data = await getQuizzes();
|
|
var q = data.FirstOrDefault(p => p.Id == id);
|
|
if (q == null)
|
|
{
|
|
throw new KeyNotFoundException($"Quiz with ID {id} not found.");
|
|
}
|
|
return q;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Asynchronously retrieves a paginated list of quizzes, returning a specific number of quizzes for the given page.
|
|
/// </summary>
|
|
/// <param name="nb">The number of quizzes to retrieve per page.</param>
|
|
/// <param name="page">The page number to retrieve, where the first page is 1.</param>
|
|
/// <returns>A task representing the asynchronous operation, with a <see cref="List{Quiz}"/> result containing the quizzes for the specified page.</returns>
|
|
/// <remarks>
|
|
/// This method retrieves the full list of quizzes using <see cref="getQuizzes"/> and returns a subset of quizzes based
|
|
/// on the specified page number and the number of quizzes per page. If the requested page exceeds the available quizzes,
|
|
/// the method returns the last page with the remaining quizzes. If the number of quizzes requested per page exceeds the
|
|
/// total number of quizzes, the method will return all quizzes available.
|
|
/// </remarks>
|
|
public async Task<List<Quiz>> getSommeQuiz(int nb, int page)
|
|
{
|
|
var data = await getQuizzes();
|
|
if ((page - 1) * nb + nb > data.Count)
|
|
{
|
|
if (nb > data.Count)
|
|
{
|
|
return data.GetRange(0, data.Count - 1);
|
|
}
|
|
return data.GetRange(data.Count - nb, nb);
|
|
}
|
|
return data.GetRange((page - 1) * nb, nb);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Asynchronously retrieves the total number of quizzes in the list.
|
|
/// </summary>
|
|
/// <returns>A task representing the asynchronous operation, with an <see cref="int"/> result containing the total number of quizzes.</returns>
|
|
/// <remarks>
|
|
/// This method retrieves the full list of quizzes using <see cref="getQuizzes"/> and returns the count of quizzes in the list.
|
|
/// It simply returns the number of quizzes available in the data source.
|
|
/// </remarks>
|
|
public async Task<int> getNbQuiz()
|
|
{
|
|
var data = await getQuizzes();
|
|
return data.Count;
|
|
}
|
|
|
|
}
|