#106 Ajout du mode Repositories Local
This commit is contained in:
@@ -10,126 +10,48 @@ namespace Webzine.Repository;
|
||||
public class LocalStyleRepository : IStyleRepository
|
||||
{
|
||||
private readonly ILogger<LocalStyleRepository> logger;
|
||||
private readonly List<Style> styles;
|
||||
//private readonly List<Style> styles;
|
||||
private readonly InMemoryDataStore dataStore;
|
||||
|
||||
/// <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)
|
||||
public LocalStyleRepository(ILogger<LocalStyleRepository> logger, InMemoryDataStore dataStore)
|
||||
{
|
||||
this.logger = logger;
|
||||
this.styles = styles;
|
||||
this.dataStore = dataStore;
|
||||
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;
|
||||
}
|
||||
throw new NotSupportedException("Mode local");
|
||||
}
|
||||
|
||||
/// <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;
|
||||
}
|
||||
throw new NotSupportedException("Mode local");
|
||||
}
|
||||
|
||||
/// <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;
|
||||
}
|
||||
return this.dataStore.Styles.Find(s => s.IdStyle == id);
|
||||
}
|
||||
|
||||
/// <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;
|
||||
}
|
||||
return this.dataStore.Styles.ToList();
|
||||
}
|
||||
|
||||
/// <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;
|
||||
}
|
||||
throw new NotSupportedException("Mode local");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user