#106 Ajout du mode Repositories Local

This commit is contained in:
Loic Masi
2026-03-26 18:48:41 +01:00
parent c95f77b6e6
commit d65d21ea64
14 changed files with 302 additions and 495 deletions

View File

@@ -15,7 +15,9 @@ namespace Webzine.Repository
public class LocalArtisteRepository : IArtisteRepository
{
private readonly ILogger<LocalArtisteRepository> logger;
private readonly List<Artiste> artistes;
//private readonly List<Artiste> artistes;
private readonly InMemoryDataStore dataStore;
/// <summary>
/// Initializes a new instance of the <see cref="LocalArtisteRepository"/> class.
@@ -23,111 +25,54 @@ namespace Webzine.Repository
/// </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)
public LocalArtisteRepository(InMemoryDataStore dataStore, ILogger<LocalArtisteRepository> logger)
{
this.logger = logger;
this.artistes = artistes;
//this.artistes = artistes;
this.dataStore = dataStore;
}
/// <inheritdoc/>
public void Add(Artiste artiste)
{
try
{
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;
}
throw new NotSupportedException("Mode Local");
}
/// <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;
}
throw new NotSupportedException("Mode Local");
}
/// <inheritdoc/>
public Artiste Find(int id)
{
try
var artiste = this.dataStore.Artistes.First(a => a.IdArtiste == id);
if (artiste == null)
{
Artiste artiste = this.artistes.First(a => a.IdArtiste == id);
return artiste;
}
catch (Exception ex)
{
this.logger.LogError(ex, "Erreur lors de la recherche de l'artiste avec ID: {Id}", id);
throw;
return new Artiste();
}
return artiste;
}
/// <inheritdoc/>
public Artiste FindByName(string nom)
{
try
{
Artiste artiste = this.artistes.First(a => a.Nom == nom);
return artiste;
}
catch (Exception ex)
{
this.logger.LogError(ex, "Erreur lors de la recherche de l'artiste avec le nom: {Nom}", nom);
throw;
}
return this.dataStore.Artistes.First(a => a.Nom == nom);
}
/// <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;
return this.dataStore.Artistes;
}
/// <inheritdoc/>
public void Update(Artiste 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;
}
throw new NotSupportedException("Mode Local");
}
}
}