#106 Ajout du mode Repositories Local
This commit is contained in:
@@ -10,265 +10,90 @@ namespace Webzine.Repository;
|
||||
public class LocalTitreRepository : ITitreRepository
|
||||
{
|
||||
private readonly ILogger<LocalTitreRepository> logger;
|
||||
private readonly List<Titre> titres;
|
||||
private readonly InMemoryDataStore dataStore;
|
||||
|
||||
/// <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)
|
||||
/// <param name="dataStore">La liste de titres à utiliser comme source de données pour le repository.</param>
|
||||
public LocalTitreRepository(ILogger<LocalTitreRepository> logger, InMemoryDataStore dataStore)
|
||||
{
|
||||
this.logger = logger;
|
||||
this.titres = titres;
|
||||
this.dataStore = dataStore;
|
||||
this.logger.LogDebug(1, "NLog injecté dans LocalTitreRepository");
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Add(Titre titre)
|
||||
{
|
||||
try
|
||||
{
|
||||
this.logger.LogDebug("Ajout du titre: {Libelle}", titre.Libelle);
|
||||
this.titres.Add(titre);
|
||||
this.logger.LogDebug("Titre ajouté avec succès, ID: {IdTitre}", titre.IdTitre);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
this.logger.LogError(ex, "Erreur lors de l'ajout du titre: {Libelle}", titre.Libelle);
|
||||
throw;
|
||||
}
|
||||
throw new NotSupportedException("Mode local");
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public int Count()
|
||||
{
|
||||
try
|
||||
{
|
||||
var count = this.titres.Count;
|
||||
this.logger.LogDebug("Nombre total de titres: {Count}", count);
|
||||
return count;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
this.logger.LogError(ex, "Erreur lors du comptage des titres");
|
||||
throw;
|
||||
}
|
||||
var count = this.dataStore.Titres.Count();
|
||||
return count;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Delete(Titre titre)
|
||||
{
|
||||
try
|
||||
{
|
||||
this.logger.LogDebug("Suppression du titre ID: {IdTitre}", titre.IdTitre);
|
||||
|
||||
if (!this.titres.Contains(titre))
|
||||
{
|
||||
this.logger.LogWarning("Le titre avec l'identifiant {IdTitre} n'existe pas dans la liste.", titre.IdTitre);
|
||||
}
|
||||
|
||||
this.titres.Remove(titre);
|
||||
this.logger.LogDebug("Titre supprimé avec succès, ID: {IdTitre}", titre.IdTitre);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
this.logger.LogError(ex, "Erreur lors de la suppression du titre ID: {IdTitre}", titre.IdTitre);
|
||||
throw;
|
||||
}
|
||||
throw new NotSupportedException("Mode Local");
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public IEnumerable<Titre> FindTitres(int offset, int limit)
|
||||
{
|
||||
try
|
||||
{
|
||||
this.logger.LogDebug("Recherche des titres avec offset: {Offset}, limit: {Limit}", offset, limit);
|
||||
|
||||
if (offset < 0 || limit <= 0)
|
||||
{
|
||||
this.logger.LogWarning("FindTitres appelé avec offset {Offset} ou limit {Limit} invalide.", offset, limit);
|
||||
return Enumerable.Empty<Titre>();
|
||||
}
|
||||
|
||||
var result = this.titres.Skip(offset).Take(limit).ToList();
|
||||
this.logger.LogDebug("{Count} titres trouvés", result.Count);
|
||||
return result;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
this.logger.LogError(ex, "Erreur lors de la recherche des titres avec offset: {Offset}, limit: {Limit}", offset, limit);
|
||||
throw;
|
||||
}
|
||||
return this.dataStore.Titres
|
||||
.OrderByDescending(t => t.DateCreation)
|
||||
.Skip(offset)
|
||||
.Take(limit);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void IncrementNbLectures(Titre titre)
|
||||
{
|
||||
try
|
||||
{
|
||||
this.logger.LogDebug("Incrémentation du nombre de lectures pour le titre ID: {IdTitre}", titre.IdTitre);
|
||||
titre.NbLectures++;
|
||||
this.logger.LogDebug("Nouveau nombre de lectures: {NbLectures}", titre.NbLectures);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
this.logger.LogError(ex, "Erreur lors de l'incrémentation des lectures pour le titre ID: {IdTitre}", titre.IdTitre);
|
||||
throw;
|
||||
}
|
||||
titre.NbLectures++;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void IncrementNbLikes(Titre titre)
|
||||
{
|
||||
try
|
||||
{
|
||||
this.logger.LogDebug("Incrémentation du nombre de likes pour le titre ID: {IdTitre}", titre.IdTitre);
|
||||
titre.NbLikes++;
|
||||
this.logger.LogDebug("Nouveau nombre de likes: {NbLikes}", titre.NbLikes);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
this.logger.LogError(ex, "Erreur lors de l'incrémentation des likes pour le titre ID: {IdTitre}", titre.IdTitre);
|
||||
throw;
|
||||
}
|
||||
titre.NbLikes++;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public IEnumerable<Titre> Search(string mot)
|
||||
{
|
||||
try
|
||||
{
|
||||
this.logger.LogDebug("Recherche de titres avec le mot-clé: {Mot}", mot);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(mot))
|
||||
{
|
||||
this.logger.LogWarning("Search appelé avec une chaîne vide ou contenant uniquement des espaces.");
|
||||
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("Aucun titre trouvé correspondant au terme de recherche '{Mot}'.", mot);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.logger.LogDebug("{Count} titres trouvés pour le mot-clé '{Mot}'", list.Count(), mot);
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
this.logger.LogError(ex, "Erreur lors de la recherche des titres avec le mot-clé: {Mot}", mot);
|
||||
throw;
|
||||
}
|
||||
return this.dataStore.Titres
|
||||
.Where(t => t.Libelle != null && t.Libelle.Contains(mot));
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public Titre Find(int idTitre)
|
||||
{
|
||||
try
|
||||
{
|
||||
this.logger.LogDebug("Recherche du titre avec ID: {IdTitre}", idTitre);
|
||||
Titre titre = this.titres.First(t => t.IdTitre == idTitre);
|
||||
this.logger.LogDebug("Titre trouvé: {Libelle}", titre.Libelle);
|
||||
return titre;
|
||||
}
|
||||
catch (InvalidOperationException ex)
|
||||
{
|
||||
this.logger.LogWarning(ex, "Aucun titre trouvé avec l'ID: {IdTitre}", idTitre);
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
this.logger.LogError(ex, "Erreur lors de la recherche du titre avec ID: {IdTitre}", idTitre);
|
||||
throw;
|
||||
}
|
||||
return this.dataStore.Titres
|
||||
.First(t => t.IdTitre == idTitre);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public IEnumerable<Titre> FindAll()
|
||||
{
|
||||
try
|
||||
{
|
||||
this.logger.LogDebug("Récupération de tous les titres");
|
||||
var result = this.titres;
|
||||
this.logger.LogDebug("{Count} titres récupérés", result.Count);
|
||||
return result;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
this.logger.LogError(ex, "Erreur lors de la récupération de tous les titres");
|
||||
throw;
|
||||
}
|
||||
return this.dataStore.Titres;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public IEnumerable<Titre> SearchByStyle(string libelle)
|
||||
{
|
||||
try
|
||||
{
|
||||
this.logger.LogDebug("Recherche des titres par style: {Libelle}", libelle);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(libelle))
|
||||
{
|
||||
this.logger.LogWarning("SearchByStyle appelé avec une chaîne vide ou contenant uniquement des espaces.");
|
||||
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("Aucun titre trouvé correspondant au style '{Libelle}'.", libelle);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.logger.LogDebug("{Count} titres trouvés pour le style '{Libelle}'", list.Count(), libelle);
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
this.logger.LogError(ex, "Erreur lors de la recherche des titres par style: {Libelle}", libelle);
|
||||
throw;
|
||||
}
|
||||
return this.dataStore.Titres
|
||||
.Where(t => t.Styles.Any(s => s.Libelle == libelle));
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Update(Titre titre)
|
||||
{
|
||||
try
|
||||
{
|
||||
this.logger.LogDebug("Mise à jour du titre ID: {IdTitre}", titre.IdTitre);
|
||||
|
||||
int index = this.titres.FindIndex(t => t.IdTitre == titre.IdTitre);
|
||||
|
||||
if (index == -1)
|
||||
{
|
||||
this.logger.LogWarning("Aucun titre trouvé avec l'identifiant {IdTitre}.", titre.IdTitre);
|
||||
throw new InvalidOperationException($"Aucun titre trouvé avec l'identifiant {titre.IdTitre}.");
|
||||
}
|
||||
|
||||
this.titres[index] = titre;
|
||||
this.logger.LogDebug("Titre mis à jour avec succès, ID: {IdTitre}", titre.IdTitre);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
this.logger.LogError(ex, "Erreur lors de la mise à jour du titre ID: {IdTitre}", titre.IdTitre);
|
||||
throw;
|
||||
}
|
||||
throw new NotSupportedException("Mode local");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user