Ajout de try catch pour une meilleure gestion d'erreurs.
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
namespace Webzine.Repository
|
namespace Webzine.Repository
|
||||||
{
|
{
|
||||||
|
using System.Data.Common;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Webzine.EntitiesContext;
|
using Webzine.EntitiesContext;
|
||||||
@@ -15,7 +16,6 @@
|
|||||||
private WebzineDbContext _context;
|
private WebzineDbContext _context;
|
||||||
private readonly ILogger<LocalArtisteRepository> _logger;
|
private readonly ILogger<LocalArtisteRepository> _logger;
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="DbArtisteRepository"/> class.
|
/// Initializes a new instance of the <see cref="DbArtisteRepository"/> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -32,14 +32,14 @@
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (artiste == null)
|
|
||||||
{
|
|
||||||
throw new ArgumentNullException(nameof(artiste), "L'artiste à ajouter ne peut pas être null.");
|
|
||||||
}
|
|
||||||
|
|
||||||
this._context.Artistes.Add(artiste);
|
this._context.Artistes.Add(artiste);
|
||||||
this._context.SaveChanges();
|
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)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
this._logger.LogError(ex, "Une erreur est survenue lors de l'ajout de l'artiste {Nom}.", artiste?.Nom);
|
this._logger.LogError(ex, "Une erreur est survenue lors de l'ajout de l'artiste {Nom}.", artiste?.Nom);
|
||||||
@@ -60,6 +60,11 @@
|
|||||||
this._context.Artistes.Remove(artiste);
|
this._context.Artistes.Remove(artiste);
|
||||||
this._context.SaveChanges();
|
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)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
this._logger.LogError(ex, "Une erreur est survenue lors de la suppression de l'artiste {Nom}.", artiste?.Nom);
|
this._logger.LogError(ex, "Une erreur est survenue lors de la suppression de l'artiste {Nom}.", artiste?.Nom);
|
||||||
@@ -69,37 +74,43 @@
|
|||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public Artiste Find(int id)
|
public Artiste Find(int id)
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
Artiste artiste = this._context.Artistes
|
Artiste artiste = this._context.Artistes
|
||||||
.Include(a => a.Titres)
|
.Include(a => a.Titres)
|
||||||
.FirstOrDefault(a => a.IdArtiste == id);
|
.First(a => a.IdArtiste == id);
|
||||||
if (artiste == null)
|
|
||||||
{
|
|
||||||
this._logger.LogWarning("Aucun artiste trouvé avec l'identifiant {Id}", id);
|
|
||||||
}
|
|
||||||
return artiste;
|
return artiste;
|
||||||
}
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
this._logger.LogError(ex, "Erreur lors de la recherche de l'artiste: {Id}", id);
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public Artiste FindByName(string nom)
|
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
|
var artiste = this._context.Artistes
|
||||||
.Include(a => a.Titres)
|
.Include(a => a.Titres)
|
||||||
.FirstOrDefault(a => a.Nom == nom);
|
.FirstOrDefault(a => a.Nom == nom);
|
||||||
|
|
||||||
if (artiste == null)
|
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;
|
return artiste;
|
||||||
}
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
this._logger.LogError(ex, "Erreur lors de la recherche de l'artiste avec le nom: {Nom}", nom);
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public IEnumerable<Artiste> FindAll()
|
public IEnumerable<Artiste> FindAll()
|
||||||
@@ -132,6 +143,11 @@
|
|||||||
this._context.SaveChanges();
|
this._context.SaveChanges();
|
||||||
this._logger.LogInformation("Artiste {Id} ({Nom}) mis à jour avec succès.", artiste.IdArtiste, artiste.Nom);
|
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)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
this._logger.LogError(ex, "Erreur lors de la mise à jour de l'artiste {Id}.", artiste.IdArtiste);
|
this._logger.LogError(ex, "Erreur lors de la mise à jour de l'artiste {Id}.", artiste.IdArtiste);
|
||||||
|
|||||||
@@ -30,12 +30,6 @@
|
|||||||
{
|
{
|
||||||
try
|
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))
|
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);
|
this._logger.LogWarning("Un artiste avec l'ID {Id} existe déjà. L'ajout est ignoré.", artiste.IdArtiste);
|
||||||
@@ -70,24 +64,33 @@
|
|||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public Artiste Find(int id)
|
public Artiste Find(int id)
|
||||||
{
|
{
|
||||||
Artiste artiste = this._artistes.FirstOrDefault(a => a.IdArtiste == id);
|
try
|
||||||
if (artiste == null)
|
|
||||||
{
|
{
|
||||||
this._logger.LogWarning("Aucun artiste trouvé avec l'identifiant {Id}", id);
|
Artiste artiste = this._artistes.First(a => a.IdArtiste == id);
|
||||||
}
|
|
||||||
return artiste;
|
return artiste;
|
||||||
}
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
this._logger.LogError(ex, "Erreur lors de la recherche de l'artiste avec ID: {Id}", id);
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public Artiste FindByName(string nom)
|
public Artiste FindByName(string nom)
|
||||||
{
|
{
|
||||||
Artiste artiste = this._artistes.FirstOrDefault(a => a.Nom == nom);
|
try
|
||||||
if (artiste == null)
|
|
||||||
{
|
{
|
||||||
this._logger.LogWarning("Aucun artiste trouvé avec le nom {Nom}", nom);
|
Artiste artiste = this._artistes.First(a => a.Nom == nom);
|
||||||
}
|
|
||||||
return 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/>
|
/// <inheritdoc/>
|
||||||
/// La liste retournée est une copie de la liste interne, donc elle ne peut être nulle.
|
/// La liste retournée est une copie de la liste interne, donc elle ne peut être nulle.
|
||||||
@@ -99,12 +102,6 @@
|
|||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public void Update(Artiste artiste)
|
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
|
try
|
||||||
{
|
{
|
||||||
var artisteToUpdate = this._artistes.FirstOrDefault(a => a.IdArtiste == artiste.IdArtiste);
|
var artisteToUpdate = this._artistes.FirstOrDefault(a => a.IdArtiste == artiste.IdArtiste);
|
||||||
|
|||||||
Reference in New Issue
Block a user