using Microsoft.Extensions.Logging; using Webzine.Entity; using Webzine.Repository.Contracts; namespace Webzine.Repository; /// /// Classe qui implémente le repository pour les titres en utilisant une liste locale comme source de données. /// public class LocalTitreRepository : ITitreRepository { private readonly ILogger logger; private readonly List titres; /// /// Initializes a new instance of the class. /// /// Le service de journalisation injecté pour suivre les opérations du repository. /// La liste de titres à utiliser comme source de données pour le repository. public LocalTitreRepository(ILogger logger, List titres) { this.logger = logger; this.titres = titres; this.logger.LogDebug(1, "NLog injected into LocalTitreRepository"); } /// /// Recherche les titres dont le libellé contient le mot spécifié, en ignorant la casse. /// /// Le mot à rechercher dans les libellés des titres. /// Une collection de titres correspondant au critère de recherche, triée par libellé. public IEnumerable Search(string mot) { if (string.IsNullOrWhiteSpace(mot)) { this.logger.LogWarning("Search called with an empty or whitespace string."); return Enumerable.Empty(); } IEnumerable 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; } /// /// Trouve un titre dans la liste des titres en fonction de son identifiant. /// /// L'identifiant du titre à trouver. /// Le titre correspondant à l'identifiant fourni, ou null si aucun titre n'est trouvé. 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; } /// /// Trouve tous les titres dans la liste des titres. /// /// Une collection de tous les titres présents dans la liste. public IEnumerable FindAll() { return this.titres; } /// /// Recherche les titres associés à un style dont le libellé contient la chaîne spécifiée, en ignorant la casse. /// /// Le libellé du style à rechercher dans les titres. /// Une collection de titres correspondant au critère de recherche, triée par libellé. public IEnumerable SearchByStyle(string libelle) { if (string.IsNullOrWhiteSpace(libelle)) { this.logger.LogWarning("SearchByStyle called with an empty or whitespace string."); return Enumerable.Empty(); } IEnumerable 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; } }