Ajout de try catch pour une meilleure gestion d'erreurs.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
namespace Webzine.Repository
|
||||
{
|
||||
using System.Data.Common;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Webzine.EntitiesContext;
|
||||
@@ -15,7 +16,6 @@
|
||||
private WebzineDbContext _context;
|
||||
private readonly ILogger<LocalArtisteRepository> _logger;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="DbArtisteRepository"/> class.
|
||||
/// </summary>
|
||||
@@ -32,14 +32,14 @@
|
||||
{
|
||||
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 (DbUpdateException dbex)
|
||||
{
|
||||
this._logger.LogError(dbex, "Erreur de base de données lors de l'ajout de l'artiste: {id}", artiste.IdArtiste);
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
this._logger.LogError(ex, "Une erreur est survenue lors de l'ajout de l'artiste {Nom}.", artiste?.Nom);
|
||||
@@ -50,7 +50,7 @@
|
||||
/// <inheritdoc/>
|
||||
public void Delete(Artiste artiste)
|
||||
{
|
||||
try
|
||||
try
|
||||
{
|
||||
if (artiste == null)
|
||||
{
|
||||
@@ -60,6 +60,11 @@
|
||||
this._context.Artistes.Remove(artiste);
|
||||
this._context.SaveChanges();
|
||||
}
|
||||
catch (DbUpdateException dbex)
|
||||
{
|
||||
this._logger.LogError(dbex, "Erreur de base de données lors de la suppression de l'artiste: {Id}", artiste.IdArtiste);
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
this._logger.LogError(ex, "Une erreur est survenue lors de la suppression de l'artiste {Nom}.", artiste?.Nom);
|
||||
@@ -70,35 +75,41 @@
|
||||
/// <inheritdoc/>
|
||||
public Artiste Find(int id)
|
||||
{
|
||||
Artiste artiste = this._context.Artistes
|
||||
.Include(a => a.Titres)
|
||||
.FirstOrDefault(a => a.IdArtiste == id);
|
||||
if (artiste == null)
|
||||
try
|
||||
{
|
||||
this._logger.LogWarning("Aucun artiste trouvé avec l'identifiant {Id}", id);
|
||||
Artiste artiste = this._context.Artistes
|
||||
.Include(a => a.Titres)
|
||||
.First(a => a.IdArtiste == id);
|
||||
return artiste;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
this._logger.LogError(ex, "Erreur lors de la recherche de l'artiste: {Id}", id);
|
||||
throw;
|
||||
}
|
||||
return artiste;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public Artiste FindByName(string nom)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(nom))
|
||||
try
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
{
|
||||
this._logger.LogWarning("Pas d'artiste au nom {Nom}", nom);
|
||||
artiste = new Artiste();
|
||||
}
|
||||
return artiste;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
this._logger.LogError(ex, "Erreur lors de la recherche de l'artiste avec le nom: {Nom}", nom);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
@@ -132,6 +143,11 @@
|
||||
this._context.SaveChanges();
|
||||
this._logger.LogInformation("Artiste {Id} ({Nom}) mis à jour avec succès.", artiste.IdArtiste, artiste.Nom);
|
||||
}
|
||||
catch (DbUpdateException ex)
|
||||
{
|
||||
this._logger.LogError(ex, "Erreur de base de données lors de la mise à jour de l'artiste ID: {IdArtiste}", artiste.IdArtiste);
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
this._logger.LogError(ex, "Erreur lors de la mise à jour de l'artiste {Id}.", artiste.IdArtiste);
|
||||
|
||||
Reference in New Issue
Block a user