refactor: renommer et implémenter les repositories pour les styles et titres avec journalisation
This commit is contained in:
106
Webzine.Repository/LocalTitreRepository.cs
Normal file
106
Webzine.Repository/LocalTitreRepository.cs
Normal file
@@ -0,0 +1,106 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Webzine.Entity;
|
||||
using Webzine.Repository.Contracts;
|
||||
|
||||
namespace Webzine.Repository;
|
||||
|
||||
/// <summary>
|
||||
/// Classe qui implémente le repository pour les titres en utilisant une liste locale comme source de données.
|
||||
/// </summary>
|
||||
public class LocalTitreRepository : ITitreRepository
|
||||
{
|
||||
private readonly ILogger<LocalTitreRepository> logger;
|
||||
private readonly List<Titre> titres;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="LocalTitreRepository"/> class.
|
||||
/// </summary>
|
||||
/// <param name="logger">Le service de journalisation injecté pour suivre les opérations du repository.</param>
|
||||
/// <param name="titres">La liste de titres à utiliser comme source de données pour le repository.</param>
|
||||
public LocalTitreRepository(ILogger<LocalTitreRepository> logger, List<Titre> titres)
|
||||
{
|
||||
this.logger = logger;
|
||||
this.titres = titres;
|
||||
this.logger.LogDebug(1, "NLog injected into LocalTitreRepository");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Recherche les titres dont le libellé contient le mot spécifié, en ignorant la casse.
|
||||
/// </summary>
|
||||
/// <param name="mot">Le mot à rechercher dans les libellés des titres.</param>
|
||||
/// <returns>Une collection de titres correspondant au critère de recherche, triée par libellé.</returns>
|
||||
public IEnumerable<Titre> Search(string mot)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(mot))
|
||||
{
|
||||
this.logger.LogWarning("Search called with an empty or whitespace string.");
|
||||
return Enumerable.Empty<Titre>();
|
||||
}
|
||||
|
||||
IEnumerable<Titre> list = this.titres
|
||||
.Where(t => !string.IsNullOrWhiteSpace(t.Libelle)
|
||||
&& t.Libelle.Contains(mot, StringComparison.OrdinalIgnoreCase))
|
||||
.OrderBy(t => t.Libelle)
|
||||
.ToList();
|
||||
|
||||
if (!list.Any())
|
||||
{
|
||||
this.logger.LogInformation("No titres found matching the search term '{Mot}'.", mot);
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Trouve un titre dans la liste des titres en fonction de son identifiant.
|
||||
/// </summary>
|
||||
/// <param name="idTitre">L'identifiant du titre à trouver.</param>
|
||||
/// <returns>Le titre correspondant à l'identifiant fourni, ou null si aucun titre n'est trouvé.</returns>
|
||||
public Titre? Find(int idTitre)
|
||||
{
|
||||
Titre? titre = titres.FirstOrDefault(t => t.IdTitre == idTitre);
|
||||
|
||||
if (titre == null)
|
||||
{
|
||||
this.logger.LogInformation("No titre found with IdTitre {IdTitre}.", idTitre);
|
||||
}
|
||||
|
||||
return titre;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Trouve tous les titres dans la liste des titres.
|
||||
/// </summary>
|
||||
/// <returns>Une collection de tous les titres présents dans la liste.</returns>
|
||||
public IEnumerable<Titre> FindAll()
|
||||
{
|
||||
return this.titres;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Recherche les titres associés à un style dont le libellé contient la chaîne spécifiée, en ignorant la casse.
|
||||
/// </summary>
|
||||
/// <param name="libelle">Le libellé du style à rechercher dans les titres.</param>
|
||||
/// <returns>Une collection de titres correspondant au critère de recherche, triée par libellé.</returns>
|
||||
public IEnumerable<Titre> SearchByStyle(string libelle)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(libelle))
|
||||
{
|
||||
this.logger.LogWarning("SearchByStyle called with an empty or whitespace string.");
|
||||
return Enumerable.Empty<Titre>();
|
||||
}
|
||||
|
||||
IEnumerable<Titre> list = this.titres
|
||||
.Where(t => t.Styles.Any(s => !string.IsNullOrWhiteSpace(s.Libelle)
|
||||
&& s.Libelle.Contains(libelle, StringComparison.OrdinalIgnoreCase)))
|
||||
.OrderBy(t => t.Libelle)
|
||||
.ToList();
|
||||
|
||||
if (!list.Any())
|
||||
{
|
||||
this.logger.LogInformation("No titres found matching the style '{Libelle}'.", libelle);
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user