Merge branch 'j2/feat/artiste_repository' into j2/refactor/controler-style-titre
This commit is contained in:
142
Webzine.Repository/DbArtisteRepository.cs
Normal file
142
Webzine.Repository/DbArtisteRepository.cs
Normal file
@@ -0,0 +1,142 @@
|
||||
namespace Webzine.Repository
|
||||
{
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Webzine.EntitiesContext;
|
||||
using Webzine.Entity;
|
||||
using Webzine.Repository.Contracts;
|
||||
|
||||
/// <summary>
|
||||
/// Initialise une classe <see cref="DbArtisteRepository"/> 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 DbArtisteRepository : IArtisteRepository
|
||||
{
|
||||
private WebzineDbContext _context;
|
||||
private readonly ILogger<LocalArtisteRepository> _logger;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="DbArtisteRepository"/> class.
|
||||
/// </summary>
|
||||
/// <param name="context">Le contexte de base de données à utiliser pour accéder aux entités et effectuer des opérations de
|
||||
/// persistance. Ne peut pas être null.</param>
|
||||
public DbArtisteRepository(WebzineDbContext context, ILogger<LocalArtisteRepository> logger)
|
||||
{
|
||||
this._logger = logger;
|
||||
this._context = context;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Add(Artiste artiste)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (artiste == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(artiste), "L'artiste à ajouter ne peut pas être null.");
|
||||
}
|
||||
|
||||
this._context.Artistes.Add(artiste);
|
||||
this._context.SaveChanges();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
this._logger.LogError(ex, "Une erreur est survenue lors de l'ajout de l'artiste {Nom}.", artiste?.Nom);
|
||||
throw new Exception("Une erreur est survenue lors de l'ajout de l'artiste.", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Delete(Artiste artiste)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (artiste == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(artiste), "L'artiste à supprimer ne peut pas être null.");
|
||||
}
|
||||
|
||||
this._context.Artistes.Remove(artiste);
|
||||
this._context.SaveChanges();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
this._logger.LogError(ex, "Une erreur est survenue lors de la suppression de l'artiste {Nom}.", artiste?.Nom);
|
||||
throw new Exception("Une erreur est survenue lors de la suppression de l'artiste.", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public Artiste Find(int id)
|
||||
{
|
||||
Artiste artiste = this._context.Artistes
|
||||
.Include(a => a.Titres)
|
||||
.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)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(nom))
|
||||
{
|
||||
this._logger.LogWarning("Tentative de recherche avec un nom vide ou null.");
|
||||
return null;
|
||||
}
|
||||
|
||||
var artiste = this._context.Artistes
|
||||
.Include(a => a.Titres)
|
||||
.FirstOrDefault(a => a.Nom == nom);
|
||||
|
||||
if (artiste == null)
|
||||
{
|
||||
this._logger.LogWarning("Recherche Nom : Aucun artiste trouvé pour '{Nom}'.", nom);
|
||||
}
|
||||
|
||||
return artiste;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public IEnumerable<Artiste> FindAll()
|
||||
{
|
||||
try
|
||||
{
|
||||
// .AsNoTracking() rend la requête beaucoup plus rapide pour de la lecture
|
||||
var artistes = this._context.Artistes.AsNoTracking().ToList();
|
||||
this._logger.LogInformation("{Count} artistes récupérés de la base.", artistes.Count);
|
||||
return artistes;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
this._logger.LogError(ex, "Erreur lors de la récupération de tous les artistes.");
|
||||
return Enumerable.Empty<Artiste>(); // Retourne une liste vide au lieu de faire crash l'UI
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Update(Artiste artiste)
|
||||
{
|
||||
if (artiste == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(artiste));
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
this._context.Artistes.Update(artiste);
|
||||
this._context.SaveChanges();
|
||||
this._logger.LogInformation("Artiste {Id} ({Nom}) mis à jour avec succès.", artiste.IdArtiste, artiste.Nom);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
this._logger.LogError(ex, "Erreur lors de la mise à jour de l'artiste {Id}.", artiste.IdArtiste);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
133
Webzine.Repository/LocalArtisteRepository.cs
Normal file
133
Webzine.Repository/LocalArtisteRepository.cs
Normal file
@@ -0,0 +1,133 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user