@ -9,143 +9,285 @@ namespace WF_WebAdmin.Service;
private readonly string _char = Path . Combine ( Environment . CurrentDirectory , "wwwroot" , "fake-dataCaracter.json" ) ;
private readonly string _char = Path . Combine ( Environment . CurrentDirectory , "wwwroot" , "fake-dataCaracter.json" ) ;
private readonly string _src = Path . Combine ( Environment . CurrentDirectory , "wwwroot" , "fake-dataSource.json" ) ;
private readonly string _src = Path . Combine ( Environment . CurrentDirectory , "wwwroot" , "fake-dataSource.json" ) ;
/// <summary>
/// Asynchronously saves a list of quotes to a JSON file.
/// </summary>
/// <param name="quotes">The list of <see cref="Quote"/> objects to be serialized and saved to the file.</param>
/// <returns>A task representing the asynchronous operation of saving the quotes to the file.</returns>
/// <remarks>
/// This method serializes the provided list of <see cref="Quote"/> objects into JSON format using <see cref="JsonSerializer"/>.
/// The serialized JSON is then saved to the file path specified in the <see cref="_jsonFilePath"/> field. The JSON is written
/// with indentation for better readability.
/// If the file does not already exist, it will be created.
/// </remarks>
public async Task saveQuoteJson ( List < Quote > quotes )
public async Task saveQuoteJson ( List < Quote > quotes )
{
{
var json = JsonSerializer . Serialize ( quotes , new JsonSerializerOptions { WriteIndented = true } ) ;
var json = JsonSerializer . Serialize ( quotes , new JsonSerializerOptions { WriteIndented = true } ) ;
await File . WriteAllTextAsync ( _jsonFilePath , json ) ;
await File . WriteAllTextAsync ( _jsonFilePath , json ) ;
}
}
/// <summary>
/// Asynchronously adds a new quote to the list and saves it to a JSON file.
/// </summary>
/// <param name="quote">The <see cref="Quote"/> object to be added to the list.</param>
/// <returns>A task representing the asynchronous operation of adding the quote and saving the updated list to the file.</returns>
/// <remarks>
/// This method retrieves the current list of quotes using the <see cref="getAllQuote"/> method, assigns a new ID to the
/// provided quote (incremented from the maximum existing ID), and adds the quote to the list. After updating the list,
/// the method saves the updated list back to the JSON file using <see cref="saveQuoteJson"/>.
/// If the list is empty, the new quote is assigned an ID of 1.
/// </remarks>
public async Task addQuote ( Quote quote )
{
var data = await getAllQuote ( ) ;
quote . Id = data . Count > 0 ? data . Max ( p = > p . Id ) + 1 : 1 ;
data . Add ( quote ) ;
await saveQuoteJson ( data ) ;
}
public async Task addQuote ( Quote quote )
/// <summary>
/// Asynchronously removes a quote from the list and saves the updated list to a JSON file.
/// </summary>
/// <param name="quote">The <see cref="Quote"/> object to be removed from the list.</param>
/// <returns>A task representing the asynchronous operation of removing the quote and saving the updated list to the file.</returns>
/// <remarks>
/// This method retrieves the current list of quotes using the <see cref="getAllQuote"/> method.
/// It searches for the provided quote by its `Id` and, if found, removes it from the list.
/// After removing the quote, the method saves the updated list back to the JSON file using <see cref="saveQuoteJson"/>.
/// If the quote is not found in the list, no action is taken.
/// </remarks>
public async Task removeQuote ( Quote quote )
{
var data = await getAllQuote ( ) ;
var q = data . FirstOrDefault ( p = > p . Id = = quote . Id ) ;
if ( q ! = null )
{
{
var data = await getAllQuote ( ) ;
data . Remove ( q ) ;
quote . Id = data . Count > 0 ? data . Max ( p = > p . Id ) + 1 : 1 ;
data . Add ( quote ) ;
await saveQuoteJson ( data ) ;
await saveQuoteJson ( data ) ;
}
}
}
public async Task removeQuote ( Quote quote )
{
var data = await getAllQuote ( ) ;
var q = data . FirstOrDefault ( p = > p . Id = = quote . Id ) ;
if ( q ! = null )
{
data . Remove ( q ) ;
await saveQuoteJson ( data ) ;
}
}
public async Task validQuote ( Quote quote )
public async Task validQuote ( Quote quote )
{
throw new NotImplementedException ( ) ;
}
/// <summary>
/// Asynchronously updates the details of an existing quote and saves the updated list to a JSON file.
/// </summary>
/// <param name="quote">The <see cref="Quote"/> object containing the updated details of the quote.</param>
/// <returns>A task representing the asynchronous operation of updating the quote and saving the updated list to the file.</returns>
/// <remarks>
/// This method retrieves the current list of quotes using the <see cref="getAllQuote"/> method.
/// It searches for the quote with the provided `Id` and, if found, updates its properties (e.g., content, character, image path, etc.)
/// with the values from the provided `quote` object. After updating the quote, the method saves the updated list back to the JSON file
/// using <see cref="saveQuoteJson"/>. If the quote with the specified `Id` is not found, no action is taken.
/// </remarks>
public async Task updateQuote ( Quote quote )
{
var data = await getAllQuote ( ) ;
var q = data . FirstOrDefault ( p = > p . Id = = quote . Id ) ;
if ( q ! = null )
{
{
throw new NotImplementedException ( ) ;
q . Content = quote . Content ;
q . Charac = quote . Charac ;
q . ImgPath = quote . ImgPath ;
q . TitleSrc = quote . TitleSrc ;
q . DateSrc = quote . DateSrc ;
q . Langue = quote . Langue ;
await saveQuoteJson ( data ) ;
}
}
}
public async Task updateQuote ( Quote quote )
/// <summary>
/// Asynchronously retrieves all quotes from a JSON file.
/// </summary>
/// <returns>A task representing the asynchronous operation, with a result of a list of <see cref="Quote"/> objects.</returns>
/// <remarks>
/// This method checks if the JSON file exists at the specified file path (<see cref="_jsonFilePath"/>).
/// If the file does not exist, it logs a message and returns an empty list of quotes.
/// If the file exists, it reads the JSON content, deserializes it into a list of <see cref="Quote"/> objects,
/// and returns that list. If the deserialization results in a null value, an empty list is returned.
/// </remarks>
public async Task < List < Quote > > getAllQuote ( )
{
if ( ! File . Exists ( _jsonFilePath ) )
{
{
var data = await getAllQuote ( ) ;
Console . Out . WriteLine ( $"{_jsonFilePath} not found" ) ;
var q = data . FirstOrDefault ( p = > p . Id = = quote . Id ) ;
return new List < Quote > ( ) ;
if ( q ! = null )
{
q . Content = quote . Content ;
q . Charac = quote . Charac ;
q . ImgPath = quote . ImgPath ;
q . TitleSrc = quote . TitleSrc ;
q . DateSrc = quote . DateSrc ;
q . Langue = quote . Langue ;
await saveQuoteJson ( data ) ;
}
}
}
public async Task < List < Quote > > getAllQuote ( )
var json = await File . ReadAllTextAsync ( _jsonFilePath ) ;
{
return JsonSerializer . Deserialize < List < Quote > > ( json ) ? ? new List < Quote > ( ) ;
if ( ! File . Exists ( _jsonFilePath ) )
}
{
Console . Out . WriteLine ( $"{_jsonFilePath} not found" ) ;
return new List < Quote > ( ) ;
}
var json = await File . ReadAllTextAsync ( _jsonFilePath ) ;
return JsonSerializer . Deserialize < List < Quote > > ( json ) ? ? new List < Quote > ( ) ;
}
public async Task < List < Quote > > getSomeQuote ( int nb , int page )
/// <summary>
/// Asynchronously retrieves a subset of quotes based on the specified page number and the number of quotes per page.
/// </summary>
/// <param name="nb">The number of quotes to retrieve per page.</param>
/// <param name="page">The page number for pagination.</param>
/// <returns>A task representing the asynchronous operation, with a result of a list of <see cref="Quote"/> objects for the specified page.</returns>
/// <remarks>
/// This method retrieves all quotes using the <see cref="getAllQuote"/> method and then calculates the range of quotes
/// to be returned based on the provided `nb` (number of quotes per page) and `page` (the page number). It ensures that
/// the returned subset does not exceed the total number of quotes available.
///
/// If the calculated range is larger than the available quotes, it returns a subset of quotes from the end of the list.
/// If the requested page number exceeds the total number of pages, the method will return the last available page of quotes.
/// </remarks>
public async Task < List < Quote > > getSomeQuote ( int nb , int page )
{
var quotes = await getAllQuote ( ) ;
if ( ( page - 1 ) * nb + nb > quotes . Count ( ) )
{
{
var quotes = await getAllQuote ( ) ;
if ( nb > quotes . Count ( ) )
if ( ( page - 1 ) * nb + nb > quotes . Count ( ) )
{
{
if ( nb > quotes . Count ( ) )
return quotes . GetRange ( 0 , quotes . Count ( ) ) ;
{
return quotes . GetRange ( 0 , quotes . Count ( ) ) ;
}
return quotes . GetRange ( quotes . Count ( ) - nb , nb ) ;
}
}
return quotes . GetRange ( ( page - 1 ) * nb , nb ) ;
return quotes . GetRange ( quotes . Count ( ) - nb , nb ) ;
}
}
return quotes . GetRange ( ( page - 1 ) * nb , nb ) ;
}
public async Task < Quote > getOnequote ( int id )
/// <summary>
/// Asynchronously retrieves a single quote based on its ID.
/// </summary>
/// <param name="id">The unique identifier of the <see cref="Quote"/> to be retrieved.</param>
/// <returns>A task representing the asynchronous operation, with a result of the <see cref="Quote"/> object if found, otherwise null.</returns>
/// <remarks>
/// This method retrieves all quotes using the <see cref="getAllQuote"/> method and searches for the quote that matches the provided `id`.
/// If a matching quote is found, it returns that quote; otherwise, it returns `null`.
/// </remarks>
public async Task < Quote > getOnequote ( int id )
{
var data = await getAllQuote ( ) ;
var q = data . FirstOrDefault ( p = > p . Id = = id ) ;
if ( q ! = null )
{
{
var data = await getAllQuote ( ) ;
return q ;
var q = data . FirstOrDefault ( p = > p . Id = = id ) ;
if ( q ! = null )
{
return q ;
}
return null ;
}
}
return null ;
}
public async Task < List < Quote > > reserchQuote ( string reserch , List < string > argument )
public async Task < List < Quote > > reserchQuote ( string reserch , List < string > argument )
{
{
throw new NotImplementedException ( ) ;
throw new NotImplementedException ( ) ;
}
}
public async Task < List < Quote > > getAllQuoteInvalid ( )
{
var quotes = await getAllQuote ( ) ;
quotes = quotes . Where ( q = > q . IsValid = = false ) . ToList ( ) ;
return quotes ;
}
public async Task < List < Quote > > getSomeQuoteInvalid ( int nb , int page )
/// <summary>
/// Asynchronously retrieves all invalid quotes from the list.
/// </summary>
/// <returns>A task representing the asynchronous operation, with a result of a list of invalid <see cref="Quote"/> objects.</returns>
/// <remarks>
/// This method retrieves all quotes using the <see cref="getAllQuote"/> method and filters them by the `IsValid` property.
/// It returns only those quotes where `IsValid` is set to `false`.
/// If no invalid quotes are found, an empty list is returned.
/// </remarks>
public async Task < List < Quote > > getAllQuoteInvalid ( )
{
var quotes = await getAllQuote ( ) ;
quotes = quotes . Where ( q = > q . IsValid = = false ) . ToList ( ) ;
return quotes ;
}
/// <summary>
/// Asynchronously retrieves a subset of invalid quotes based on the specified page number and the number of quotes per page.
/// </summary>
/// <param name="nb">The number of invalid quotes to retrieve per page.</param>
/// <param name="page">The page number for pagination.</param>
/// <returns>A task representing the asynchronous operation, with a result of a list of invalid <see cref="Quote"/> objects for the specified page.</returns>
/// <remarks>
/// This method retrieves all invalid quotes using the <see cref="getAllQuoteInvalid"/> method and then calculates the range of invalid quotes
/// to be returned based on the provided `nb` (number of quotes per page) and `page` (the page number). It ensures that
/// the returned subset does not exceed the total number of invalid quotes available.
///
/// If the calculated range is larger than the available invalid quotes, it returns a subset of quotes from the end of the list.
/// If the requested page number exceeds the total number of pages, the method will return the last available page of invalid quotes.
/// </remarks>
public async Task < List < Quote > > getSomeQuoteInvalid ( int nb , int page )
{
var quotes = await getAllQuoteInvalid ( ) ;
if ( ( page - 1 ) * nb + nb > quotes . Count ( ) )
{
{
var quotes = await getAllQuoteInvalid ( ) ;
if ( nb > quotes . Count ( ) )
if ( ( page - 1 ) * nb + nb > quotes . Count ( ) )
{
{
if ( nb > quotes . Count ( ) )
return quotes . GetRange ( 0 , quotes . Count ( ) ) ;
{
return quotes . GetRange ( 0 , quotes . Count ( ) ) ;
}
return quotes . GetRange ( quotes . Count ( ) - nb , nb ) ;
}
}
return quotes . GetRange ( ( page - 1 ) * nb , nb ) ;
return quotes . GetRange ( quotes . Count ( ) - nb , nb ) ;
}
}
return quotes . GetRange ( ( page - 1 ) * nb , nb ) ;
}
public async Task < int > getNbQuote ( )
/// <summary>
/// Asynchronously retrieves the total number of quotes.
/// </summary>
/// <returns>A task representing the asynchronous operation, with a result of the total number of <see cref="Quote"/> objects.</returns>
/// <remarks>
/// This method retrieves all quotes using the <see cref="getAllQuote"/> method and returns the count of quotes.
/// It provides the total number of quotes currently available in the data source.
/// </remarks>
public async Task < int > getNbQuote ( )
{
var data = await getAllQuote ( ) ;
return data . Count ;
}
/// <summary>
/// Asynchronously retrieves a list of characters from a JSON file.
/// </summary>
/// <returns>A task representing the asynchronous operation, with a result of a list of <see cref="Character"/> objects.</returns>
/// <remarks>
/// This method checks if the JSON file containing character data exists at the specified file path (`_char`).
/// If the file does not exist, it logs a message to the console and returns an empty list of characters.
/// If the file exists, it reads the JSON content, deserializes it into a list of <see cref="Character"/> objects,
/// and returns that list. If the deserialization results in a null value, an empty list is returned.
/// </remarks>
public async Task < List < Character > > getChar ( )
{
if ( ! File . Exists ( _char ) )
{
{
var data = await getAllQuote ( ) ;
Console . Out . WriteLine ( $"{_char} not found" ) ;
return data . Count ;
return new List < Character > ( ) ;
}
}
public async Task < List < Character > > getChar ( )
var json = await File . ReadAllTextAsync ( _char ) ;
{
return JsonSerializer . Deserialize < List < Character > > ( json ) ? ? new List < Character > ( ) ;
if ( ! File . Exists ( _char ) )
}
{
Console . Out . WriteLine ( $"{_char} not found" ) ;
return new List < Character > ( ) ;
}
var json = await File . ReadAllTextAsync ( _char ) ;
return JsonSerializer . Deserialize < List < Character > > ( json ) ? ? new List < Character > ( ) ;
}
public async Task < List < Source > > getSrc ( )
/// <summary>
/// Asynchronously retrieves a list of sources from a JSON file.
/// </summary>
/// <returns>A task representing the asynchronous operation, with a result of a list of <see cref="Source"/> objects.</returns>
/// <remarks>
/// This method checks if the JSON file containing source data exists at the specified file path (`_src`).
/// If the file does not exist, it logs a message to the console and returns an empty list of sources.
/// If the file exists, it reads the JSON content, deserializes it into a list of <see cref="Source"/> objects,
/// and returns that list. If the deserialization results in a null value, an empty list is returned.
/// </remarks>
public async Task < List < Source > > getSrc ( )
{
if ( ! File . Exists ( _src ) )
{
{
if ( ! File . Exists ( _src ) )
Console . Out . WriteLine ( $"{_src} not found" ) ;
{
return new List < Source > ( ) ;
Console . Out . WriteLine ( $"{_src} not found" ) ;
return new List < Source > ( ) ;
}
var json = await File . ReadAllTextAsync ( _src ) ;
return JsonSerializer . Deserialize < List < Source > > ( json ) ? ? new List < Source > ( ) ;
}
}
}
var json = await File . ReadAllTextAsync ( _src ) ;
return JsonSerializer . Deserialize < List < Source > > ( json ) ? ? new List < Source > ( ) ;
}
}