#175 Correction des repository : First remplacé par FirstOrDefault. Ajout de la documentation manquante. Les return Model() vides sont supprimés. La gestion d'erreurs est optimisée.

This commit is contained in:
josephine.vetu
2026-04-01 11:43:01 +02:00
parent 19b920ba4e
commit 03cc02b6f2
11 changed files with 54 additions and 110 deletions

View File

@@ -16,10 +16,9 @@ public class DbCommentaireRepository : ICommentaireRepository
private readonly WebzineDbContext context;
/// <summary>
/// Initializes a new instance of the <see cref="DbCommentaireRepository"/> class.
/// Initialisation de <see cref="DbCommentaireRepository"/>.
/// </summary>
/// <param name="logger">Le service de journalisation injecté pour suivre les opérations du repository.</param>
/// <param name="logger">Le service de journalisation.</param>
/// <param name="context">Le contexte de base de données injecté.</param>
public DbCommentaireRepository(ILogger<DbCommentaireRepository> logger, WebzineDbContext context)
{
@@ -33,7 +32,6 @@ public class DbCommentaireRepository : ICommentaireRepository
{
try
{
this.logger.LogDebug("Ajout d'un nouveau commentaire de l'auteur : {Auteur}", commentaire.Auteur);
this.context.Commentaires.Add(commentaire);
this.context.SaveChanges();
this.logger.LogDebug("Commentaire ajouté avec l'id : {Id}", commentaire.IdCommentaire);
@@ -55,11 +53,6 @@ public class DbCommentaireRepository : ICommentaireRepository
{
try
{
if (commentaire == null)
{
throw new ArgumentNullException(nameof(commentaire), "Le commentaire à supprimer ne peut pas être null.");
}
this.context.Commentaires.Remove(commentaire);
this.context.SaveChanges();
this.logger.LogDebug("Le commentaire {IdCommentaire} a bien été supprimé", commentaire.IdCommentaire);
@@ -76,14 +69,6 @@ public class DbCommentaireRepository : ICommentaireRepository
}
}
/// <inheritdoc/>
public int Count()
{
var count = this.context.Commentaires.Count();
this.logger.LogDebug("Compte total des commentaires : {Count}", count);
return count;
}
/// <inheritdoc/>
public Commentaire Find(int idCommentaire)
{
@@ -98,8 +83,6 @@ public class DbCommentaireRepository : ICommentaireRepository
/// <inheritdoc/>
public IEnumerable<Commentaire> FindAll()
{
this.logger.LogDebug("Récupération de tous les commentaires");
var commentaires = this.context.Commentaires
.Include(c => c.Titre)
.OrderByDescending(c => c.DateCreation)
@@ -110,7 +93,7 @@ public class DbCommentaireRepository : ICommentaireRepository
}
/// <inheritdoc/>
public IEnumerable<Commentaire> FindCommentaires(int offset, int limit)
public IEnumerable<Commentaire> Paginate(int offset, int limit)
{
this.logger.LogDebug("Recherche paginée des commentaires (offset : {Offset}, limit : {Limit})", offset, limit);
@@ -124,18 +107,4 @@ public class DbCommentaireRepository : ICommentaireRepository
this.logger.LogDebug("{Count} commentaires trouvés pour cette page", commentaires.Count);
return commentaires;
}
/// <inheritdoc/>
public IEnumerable<Commentaire> FindByIdTitre(int idTitre)
{
this.logger.LogDebug("Recherche des commentaires pour le titre ID : {IdTitre}", idTitre);
var commentaires = this.context.Commentaires
.Where(c => c.Titre.IdTitre == idTitre)
.OrderByDescending(c => c.DateCreation)
.ToList();
this.logger.LogDebug($"{commentaires.Count} commentaires trouvés pour l'ID de titre : {idTitre}");
return commentaires;
}
}