Merge branch 'j2/ajout_repo' of https://10.4.0.131/gitea/DI1-P4-E1/Webzine into j2/feat/repository-commentaire
This commit is contained in:
@@ -1,5 +1,10 @@
|
||||
namespace Webzine.Repository
|
||||
// <copyright file="DbArtisteRepository.cs" company="PlaceholderCompany">
|
||||
// Copyright (c) PlaceholderCompany. All rights reserved.
|
||||
// </copyright>
|
||||
|
||||
namespace Webzine.Repository
|
||||
{
|
||||
using System.Data.Common;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Webzine.EntitiesContext;
|
||||
@@ -12,19 +17,18 @@
|
||||
/// </summary>
|
||||
public class DbArtisteRepository : IArtisteRepository
|
||||
{
|
||||
private WebzineDbContext _context;
|
||||
private readonly ILogger<LocalArtisteRepository> _logger;
|
||||
|
||||
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)
|
||||
public DbArtisteRepository(WebzineDbContext context, ILogger<LocalArtisteRepository> logger)
|
||||
{
|
||||
this._logger = logger;
|
||||
this._context = context;
|
||||
this.logger = logger;
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
@@ -32,17 +36,17 @@
|
||||
{
|
||||
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();
|
||||
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);
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -50,19 +54,24 @@
|
||||
/// <inheritdoc/>
|
||||
public void Delete(Artiste artiste)
|
||||
{
|
||||
try
|
||||
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();
|
||||
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);
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -70,35 +79,42 @@
|
||||
/// <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("Pas d'artiste au nom {Nom}", nom);
|
||||
artiste = new Artiste();
|
||||
}
|
||||
|
||||
return artiste;
|
||||
}
|
||||
|
||||
var artiste = this._context.Artistes
|
||||
.Include(a => a.Titres)
|
||||
.FirstOrDefault(a => a.Nom == nom);
|
||||
|
||||
if (artiste == null)
|
||||
catch (Exception ex)
|
||||
{
|
||||
this._logger.LogWarning("Recherche Nom : Aucun artiste trouvé pour '{Nom}'.", nom);
|
||||
this.logger.LogError(ex, "Erreur lors de la recherche de l'artiste avec le nom: {Nom}", nom);
|
||||
throw;
|
||||
}
|
||||
|
||||
return artiste;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
@@ -107,13 +123,13 @@
|
||||
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);
|
||||
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.");
|
||||
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
|
||||
}
|
||||
}
|
||||
@@ -128,13 +144,18 @@
|
||||
|
||||
try
|
||||
{
|
||||
this._context.Artistes.Update(artiste);
|
||||
this._context.SaveChanges();
|
||||
this._logger.LogInformation("Artiste {Id} ({Nom}) mis à jour avec succès.", artiste.IdArtiste, artiste.Nom);
|
||||
this.context.Artistes.Update(artiste);
|
||||
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);
|
||||
this.logger.LogError(ex, "Erreur lors de la mise à jour de l'artiste {Id}.", artiste.IdArtiste);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
63
Webzine.Repository/DbEntityRepository.cs
Normal file
63
Webzine.Repository/DbEntityRepository.cs
Normal file
@@ -0,0 +1,63 @@
|
||||
// <copyright file="DbEntityRepository.cs" company="PlaceholderCompany">
|
||||
// Copyright (c) PlaceholderCompany. All rights reserved.
|
||||
// </copyright>
|
||||
|
||||
namespace Webzine.Repository
|
||||
{
|
||||
using Webzine.EntitiesContext;
|
||||
using Webzine.Entity;
|
||||
using Webzine.Entity.Fixtures;
|
||||
using Webzine.Repository.Fake;
|
||||
|
||||
public class DbEntityRepository
|
||||
{
|
||||
private readonly WebzineDbContext context;
|
||||
|
||||
public DbEntityRepository(WebzineDbContext context)
|
||||
{
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
public void SeedBaseDeDonnees(
|
||||
int nbArtistes = 100,
|
||||
int nbTitres = 500,
|
||||
int minStyles = 15,
|
||||
int maxStyles = 20,
|
||||
int minCommentairesParTitre = 0,
|
||||
int maxCommentairesParTitre = 5)
|
||||
{
|
||||
if (this.context.Artistes.Any() ||
|
||||
this.context.Titres.Any() ||
|
||||
this.context.Styles.Any() ||
|
||||
this.context.Commentaires.Any())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
List<Artiste> artistes = ArtisteFactory.CreerListeArtiste(nbArtistes);
|
||||
List<Style> styles = StyleFactory.CreerListeStyle(minStyles, maxStyles);
|
||||
|
||||
this.context.Artistes.AddRange(artistes);
|
||||
this.context.Styles.AddRange(styles);
|
||||
this.context.SaveChanges();
|
||||
|
||||
List<Titre> titres = TitreFactory.CreerListeTitre(nbTitres, artistes, styles);
|
||||
this.context.Titres.AddRange(titres);
|
||||
this.context.SaveChanges();
|
||||
|
||||
List<Commentaire> commentaires = new List<Commentaire>();
|
||||
|
||||
foreach (Titre titre in titres)
|
||||
{
|
||||
commentaires.AddRange(
|
||||
CommentaireFactory.CreerListeCommentaire(
|
||||
titre,
|
||||
minCommentairesParTitre,
|
||||
maxCommentairesParTitre));
|
||||
}
|
||||
|
||||
this.context.Commentaires.AddRange(commentaires);
|
||||
this.context.SaveChanges();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,8 @@
|
||||
namespace Webzine.Repository
|
||||
// <copyright file="LocalArtisteRepository.cs" company="PlaceholderCompany">
|
||||
// Copyright (c) PlaceholderCompany. All rights reserved.
|
||||
// </copyright>
|
||||
|
||||
namespace Webzine.Repository
|
||||
{
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Webzine.Entity;
|
||||
@@ -10,8 +14,8 @@
|
||||
/// </summary>
|
||||
public class LocalArtisteRepository : IArtisteRepository
|
||||
{
|
||||
private readonly ILogger<LocalArtisteRepository> _logger;
|
||||
private readonly List<Artiste> _artistes;
|
||||
private readonly ILogger<LocalArtisteRepository> logger;
|
||||
private readonly List<Artiste> artistes;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="LocalArtisteRepository"/> class.
|
||||
@@ -21,8 +25,8 @@
|
||||
/// <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;
|
||||
this.logger = logger;
|
||||
this.artistes = artistes;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
@@ -30,24 +34,18 @@
|
||||
{
|
||||
try
|
||||
{
|
||||
if (artiste == null)
|
||||
if (this.artistes.Any(a => a.IdArtiste == artiste.IdArtiste))
|
||||
{
|
||||
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);
|
||||
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);
|
||||
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);
|
||||
this.logger.LogError(ex, "Erreur lors de l'ajout de l'artiste : {Nom}", artiste?.Nom);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
@@ -57,12 +55,12 @@
|
||||
{
|
||||
try
|
||||
{
|
||||
this._artistes.Remove(artiste);
|
||||
this._logger.LogInformation("Artiste supprimé : {Nom}", artiste.Nom);
|
||||
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);
|
||||
this.logger.LogError(ex, "Erreur lors de la suppression de l'artiste : {Nom}", artiste.Nom);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
@@ -70,44 +68,46 @@
|
||||
/// <inheritdoc/>
|
||||
public Artiste Find(int id)
|
||||
{
|
||||
Artiste artiste = this._artistes.FirstOrDefault(a => a.IdArtiste == id);
|
||||
if (artiste == null)
|
||||
try
|
||||
{
|
||||
this._logger.LogWarning("Aucun artiste trouvé avec l'identifiant {Id}", id);
|
||||
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 artiste;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public Artiste FindByName(string nom)
|
||||
{
|
||||
Artiste artiste = this._artistes.FirstOrDefault(a => a.Nom == nom);
|
||||
if (artiste == null)
|
||||
try
|
||||
{
|
||||
this._logger.LogWarning("Aucun artiste trouvé avec le nom {Nom}", nom);
|
||||
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 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;
|
||||
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);
|
||||
var artisteToUpdate = this.artistes.FirstOrDefault(a => a.IdArtiste == artiste.IdArtiste);
|
||||
|
||||
if (artisteToUpdate != null)
|
||||
{
|
||||
@@ -115,17 +115,17 @@
|
||||
artisteToUpdate.Biographie = artiste.Biographie;
|
||||
artisteToUpdate.Titres = artiste.Titres;
|
||||
|
||||
this._logger.LogInformation("Artiste {Id} mis à jour avec succès.", artiste.IdArtiste);
|
||||
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);
|
||||
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);
|
||||
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