Files
webzine/Webzine.Repository/LocalStyleRepository.cs

135 lines
4.3 KiB
C#

using Microsoft.Extensions.Logging;
using Webzine.Entity;
using Webzine.Repository.Contracts;
namespace Webzine.Repository;
/// <summary>
/// Classe qui implémente le repository pour les styles en utilisant une liste locale comme source de données.
/// </summary>
public class LocalStyleRepository : IStyleRepository
{
private readonly ILogger<LocalStyleRepository> logger;
private readonly List<Style> styles;
/// <summary>
/// Initializes a new instance of the <see cref="LocalStyleRepository"/> class.
/// </summary>
/// <param name="logger">Le service de journalisation injecté pour suivre les opérations du repository.</param>
/// <param name="styles">La liste de styles à utiliser comme source de données pour le repository.</param>
public LocalStyleRepository(ILogger<LocalStyleRepository> logger, List<Style> styles)
{
this.logger = logger;
this.styles = styles;
this.logger.LogDebug(1, "NLog injecté dans LocalStyleRepository");
}
/// <inheritdoc/>
public void Add(Style style)
{
try
{
this.logger.LogDebug("Ajout du style: {Libelle}", style.Libelle);
this.styles.Add(style);
this.logger.LogDebug("Style ajouté avec succès, ID: {IdStyle}", style.IdStyle);
}
catch (Exception ex)
{
this.logger.LogError(ex, "Erreur lors de l'ajout du style: {Libelle}", style.Libelle);
throw;
}
}
/// <inheritdoc/>
public void Delete(Style style)
{
try
{
this.logger.LogDebug("Suppression du style ID: {IdStyle}", style.IdStyle);
if (!this.styles.Contains(style))
{
this.logger.LogWarning("Le style avec l'identifiant {IdStyle} n'existe pas dans la liste.", style.IdStyle);
}
this.styles.Remove(style);
this.logger.LogDebug("Style supprimé avec succès, ID: {IdStyle}", style.IdStyle);
}
catch (Exception ex)
{
this.logger.LogError(ex, "Erreur lors de la suppression du style ID: {IdStyle}", style.IdStyle);
throw;
}
}
/// <inheritdoc/>
public Style Find(int id)
{
try
{
this.logger.LogDebug("Recherche du style avec ID: {Id}", id);
if (id <= 0)
{
this.logger.LogWarning("Tentative de recherche d'un style avec un Id invalide: {Id}", id);
return new Style();
}
Style style = this.styles.First(s => s.IdStyle == id);
this.logger.LogDebug("Style trouvé: {Libelle}", style.Libelle);
return style;
}
catch (InvalidOperationException ex)
{
this.logger.LogWarning(ex, "Aucun style trouvé avec l'ID: {Id}", id);
throw;
}
catch (Exception ex)
{
this.logger.LogError(ex, "Erreur lors de la recherche du style avec ID: {Id}", id);
throw;
}
}
/// <inheritdoc/>
public IEnumerable<Style> FindAll()
{
try
{
this.logger.LogDebug("Récupération de tous les styles");
var result = this.styles;
this.logger.LogDebug("{Count} styles 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 styles");
throw;
}
}
/// <inheritdoc/>
public void Update(Style style)
{
try
{
this.logger.LogDebug("Mise à jour du style ID: {IdStyle}", style.IdStyle);
int index = this.styles.FindIndex(s => s.IdStyle == style.IdStyle);
if (index == -1)
{
this.logger.LogWarning("Aucun style trouvé avec l'identifiant {IdStyle}.", style.IdStyle);
throw new InvalidOperationException($"Aucun style trouvé avec l'identifiant {style.IdStyle}.");
}
this.styles[index] = style;
this.logger.LogDebug("Style mis à jour avec succès, ID: {IdStyle}", style.IdStyle);
}
catch (Exception ex)
{
this.logger.LogError(ex, "Erreur lors de la mise à jour du style ID: {IdStyle}", style.IdStyle);
throw;
}
}
}