refactor: amélioration de la journalisation et la gestion des exceptions dans les repositories de styles et titres
This commit is contained in:
@@ -21,70 +21,115 @@ public class LocalStyleRepository : IStyleRepository
|
||||
{
|
||||
this.logger = logger;
|
||||
this.styles = styles;
|
||||
this.logger.LogDebug(1, "NLog injected into LocalEntityRepository");
|
||||
this.logger.LogDebug(1, "NLog injecté dans LocalStyleRepository");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ajoute un style à la liste des styles.
|
||||
/// </summary>
|
||||
/// <param name="style">L'objet style à ajouter.</param>
|
||||
/// <inheritdoc/>
|
||||
public void Add(Style style)
|
||||
{
|
||||
this.styles.Add(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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Supprime un style de la liste des styles.
|
||||
/// </summary>
|
||||
/// <param name="style">L'objet style à supprimer.</param>
|
||||
/// <inheritdoc/>
|
||||
public void Delete(Style style)
|
||||
{
|
||||
if (!this.styles.Contains(style))
|
||||
try
|
||||
{
|
||||
this.logger.LogWarning("Le style avec l'identifiant {IdStyle} n'existe pas dans la liste.", style.IdStyle);
|
||||
}
|
||||
this.logger.LogDebug("Suppression du style ID: {IdStyle}", style.IdStyle);
|
||||
|
||||
this.styles.Remove(style);
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Trouve un style dans la liste des styles en fonction de son identifiant.
|
||||
/// </summary>
|
||||
/// <param name="id">L'identifiant du style à trouver.</param>
|
||||
/// <returns>Le style correspondant à l'identifiant fourni, ou null si aucun style n'est trouvé.</returns>
|
||||
/// <inheritdoc/>
|
||||
public Style Find(int id)
|
||||
{
|
||||
if (id <= 0)
|
||||
try
|
||||
{
|
||||
this.logger.LogWarning("Le style fourni est null.");
|
||||
}
|
||||
this.logger.LogDebug("Recherche du style avec ID: {Id}", id);
|
||||
|
||||
Style style = this.styles.First(s => s.IdStyle == id);
|
||||
return style;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Trouve tous les styles dans la liste des styles.
|
||||
/// </summary>
|
||||
/// <returns>Une collection de tous les styles présents dans la liste.</returns>
|
||||
/// <inheritdoc/>
|
||||
public IEnumerable<Style> FindAll()
|
||||
{
|
||||
return this.styles;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Met à jour un style dans la liste des styles en fonction de son identifiant.
|
||||
/// </summary>
|
||||
/// <param name="style">L'objet style à mettre à jour.</param>
|
||||
/// <inheritdoc/>
|
||||
public void Update(Style style)
|
||||
{
|
||||
int index = this.styles.FindIndex(s => s.IdStyle == style.IdStyle);
|
||||
|
||||
if (index == -1)
|
||||
try
|
||||
{
|
||||
this.logger.LogWarning("Aucun style trouvé avec l'identifiant {IdStyle}.", style.IdStyle);
|
||||
}
|
||||
this.logger.LogDebug("Mise à jour du style ID: {IdStyle}", style.IdStyle);
|
||||
|
||||
this.styles[index] = style;
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user