133 lines
4.9 KiB
C#
133 lines
4.9 KiB
C#
namespace Webzine.Repository
|
|
{
|
|
using Microsoft.Extensions.Logging;
|
|
using Webzine.Entity;
|
|
using Webzine.Repository.Contracts;
|
|
|
|
/// <summary>
|
|
/// Initialise une classe <see cref="LocalArtisteRepository"/> qui implémente l'interface <see cref="IArtisteRepository"/> pour gérer les opérations de base de données liées aux artistes.
|
|
/// Utilise <see cref="IArtisteRepository"/> en injection de dépendances.
|
|
/// </summary>
|
|
public class LocalArtisteRepository : IArtisteRepository
|
|
{
|
|
private readonly ILogger<LocalArtisteRepository> _logger;
|
|
private readonly List<Artiste> _artistes;
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="LocalArtisteRepository"/> class.
|
|
/// Est liéee à une liste d'artistes en local et utilise un logger pour enregistrer les opérations effectuées sur les artistes.
|
|
/// </summary>
|
|
/// <param name="artistes">La liste des artistes à initialiser. Ne peut pas être null.</param>
|
|
/// <param name="logger">Le logger à utiliser pour enregistrer les messages de journalisation. Ne peut pas être null.</param>
|
|
public LocalArtisteRepository(List<Artiste> artistes, ILogger<LocalArtisteRepository> logger)
|
|
{
|
|
this._logger = logger;
|
|
this._artistes = artistes;
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public void Add(Artiste artiste)
|
|
{
|
|
try
|
|
{
|
|
if (artiste == null)
|
|
{
|
|
this._logger.LogError("L'artiste à ajouter ne peut pas être null.");
|
|
throw new ArgumentNullException(nameof(artiste));
|
|
}
|
|
|
|
if (this._artistes.Any(a => a.IdArtiste == artiste.IdArtiste))
|
|
{
|
|
this._logger.LogWarning("Un artiste avec l'ID {Id} existe déjà. L'ajout est ignoré.", artiste.IdArtiste);
|
|
return;
|
|
}
|
|
|
|
this._artistes.Add(artiste);
|
|
this._logger.LogInformation("Artiste ajouté : {Nom}", artiste.Nom);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
this._logger.LogError(ex, "Erreur lors de l'ajout de l'artiste : {Nom}", artiste?.Nom);
|
|
throw;
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public void Delete(Artiste artiste)
|
|
{
|
|
try
|
|
{
|
|
this._artistes.Remove(artiste);
|
|
this._logger.LogInformation("Artiste supprimé : {Nom}", artiste.Nom);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
this._logger.LogError(ex, "Erreur lors de la suppression de l'artiste : {Nom}", artiste.Nom);
|
|
throw;
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public Artiste Find(int id)
|
|
{
|
|
Artiste artiste = this._artistes.FirstOrDefault(a => a.IdArtiste == id);
|
|
if (artiste == null)
|
|
{
|
|
this._logger.LogWarning("Aucun artiste trouvé avec l'identifiant {Id}", id);
|
|
}
|
|
return artiste;
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public Artiste FindByName(string nom)
|
|
{
|
|
Artiste artiste = this._artistes.FirstOrDefault(a => a.Nom == nom);
|
|
if (artiste == null)
|
|
{
|
|
this._logger.LogWarning("Aucun artiste trouvé avec le nom {Nom}", nom);
|
|
}
|
|
return artiste;
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
/// La liste retournée est une copie de la liste interne, donc elle ne peut être nulle.
|
|
public IEnumerable<Artiste> FindAll()
|
|
{
|
|
return this._artistes;
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public void Update(Artiste artiste)
|
|
{
|
|
if (artiste == null)
|
|
{
|
|
this._logger.LogError("L'artiste à mettre à jour ne peut pas être null.");
|
|
throw new ArgumentNullException(nameof(artiste));
|
|
}
|
|
|
|
try
|
|
{
|
|
var artisteToUpdate = this._artistes.FirstOrDefault(a => a.IdArtiste == artiste.IdArtiste);
|
|
|
|
if (artisteToUpdate != null)
|
|
{
|
|
artisteToUpdate.Nom = artiste.Nom;
|
|
artisteToUpdate.Biographie = artiste.Biographie;
|
|
artisteToUpdate.Titres = artiste.Titres;
|
|
|
|
this._logger.LogInformation("Artiste {Id} mis à jour avec succès.", artiste.IdArtiste);
|
|
}
|
|
else
|
|
{
|
|
this._logger.LogWarning("Mise à jour impossible : l'artiste avec l'ID {Id} n'existe pas.", artiste.IdArtiste);
|
|
throw new KeyNotFoundException($"L'artiste {artiste.IdArtiste} est introuvable.");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
this._logger.LogError(ex, "Une erreur est survenue lors de la mise à jour de l'artiste {Id}.", artiste.IdArtiste);
|
|
throw;
|
|
}
|
|
}
|
|
}
|
|
} |