198 lines
7.0 KiB
C#
198 lines
7.0 KiB
C#
using Microsoft.Extensions.Logging;
|
|
using Webzine.Entity;
|
|
using Webzine.Repository.Contracts;
|
|
|
|
namespace Webzine.Repository;
|
|
|
|
/// <summary>
|
|
/// Classe qui implémente le repository pour les commentaires en utilisant une liste locale comme source de données.
|
|
/// </summary>
|
|
public class LocalCommentaireRepository : ICommentaireRepository
|
|
{
|
|
private readonly ILogger<LocalCommentaireRepository> logger;
|
|
private readonly List<Commentaire> commentaires;
|
|
|
|
/// <summary>
|
|
/// Initialise une nouvelle instance du <see cref="LocalCommentaireRepository"/> .
|
|
/// </summary>
|
|
/// <param name="logger">Le service de journalisation injecté pour suivre les opérations du repository.</param>
|
|
/// <param name="commentaires">La liste de commentaires à utiliser comme source de données pour le repository.</param>
|
|
public LocalCommentaireRepository(ILogger<LocalCommentaireRepository> logger, List<Commentaire> commentaires)
|
|
{
|
|
this.logger = logger;
|
|
this.commentaires = commentaires;
|
|
this.logger.LogDebug(1, "NLog injecté dans LocalCommentaireRepository");
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public void Add(Commentaire commentaire)
|
|
{
|
|
try
|
|
{
|
|
this.logger.LogDebug("Ajout du commentaire rédigé par: {Auteur}", commentaire.Auteur);
|
|
|
|
// Simulation d'auto-incrémentation pour l'ID si nécessaire (selon votre logique)
|
|
if (commentaire.IdCommentaire == 0 && this.commentaires.Any())
|
|
{
|
|
commentaire.IdCommentaire = this.commentaires.Max(c => c.IdCommentaire) + 1;
|
|
}
|
|
else if (commentaire.IdCommentaire == 0)
|
|
{
|
|
commentaire.IdCommentaire = 1;
|
|
}
|
|
|
|
this.commentaires.Add(commentaire);
|
|
this.logger.LogDebug("Commentaire ajouté avec succès, ID: {IdCommentaire}", commentaire.IdCommentaire);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
this.logger.LogError(ex, "Erreur lors de l'ajout du commentaire rédigé par: {Auteur}", commentaire.Auteur);
|
|
throw;
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public int Count()
|
|
{
|
|
try
|
|
{
|
|
var count = this.commentaires.Count;
|
|
this.logger.LogDebug("Nombre total de commentaires: {Count}", count);
|
|
return count;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
this.logger.LogError(ex, "Erreur lors du comptage des commentaires");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public void Delete(Commentaire commentaire)
|
|
{
|
|
try
|
|
{
|
|
this.logger.LogDebug("Suppression du commentaire ID: {IdCommentaire}", commentaire.IdCommentaire);
|
|
|
|
if (!this.commentaires.Contains(commentaire))
|
|
{
|
|
this.logger.LogWarning("Le commentaire avec l'identifiant {IdCommentaire} n'existe pas dans la liste.", commentaire.IdCommentaire);
|
|
}
|
|
|
|
this.commentaires.Remove(commentaire);
|
|
this.logger.LogDebug("Commentaire supprimé avec succès, ID: {IdCommentaire}", commentaire.IdCommentaire);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
this.logger.LogError(ex, "Erreur lors de la suppression du commentaire ID: {IdCommentaire}", commentaire.IdCommentaire);
|
|
throw;
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public Commentaire Find(int idCommentaire)
|
|
{
|
|
try
|
|
{
|
|
this.logger.LogDebug("Recherche du commentaire avec ID: {IdCommentaire}", idCommentaire);
|
|
|
|
// FirstOrDefault car le contrôleur s'attend potentiellement à un null si non trouvé
|
|
Commentaire commentaire = this.commentaires.FirstOrDefault(c => c.IdCommentaire == idCommentaire);
|
|
|
|
if (commentaire == null)
|
|
{
|
|
this.logger.LogWarning("Aucun commentaire trouvé avec l'ID: {IdCommentaire}", idCommentaire);
|
|
}
|
|
else
|
|
{
|
|
this.logger.LogDebug("Commentaire trouvé: ID {IdCommentaire} par {Auteur}", commentaire.IdCommentaire, commentaire.Auteur);
|
|
}
|
|
|
|
return commentaire;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
this.logger.LogError(ex, "Erreur lors de la recherche du commentaire avec ID: {IdCommentaire}", idCommentaire);
|
|
throw;
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public IEnumerable<Commentaire> FindAll()
|
|
{
|
|
try
|
|
{
|
|
this.logger.LogDebug("Récupération de tous les commentaires");
|
|
|
|
// on affiche les commentaires du plus récent au plus ancien et la je fais un commentaire super long pour que joséphine le voie et crois que ca va etre plus complexe que ce que c'est vraiment mais non en fait t'as eu peur hein :)
|
|
var result = this.commentaires.OrderByDescending(c => c.DateCreation).ToList();
|
|
|
|
this.logger.LogDebug("{Count} commentaires récupérés", result.Count);
|
|
return result;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
this.logger.LogError(ex, "Erreur lors de la récupération de tous les commentaires");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public IEnumerable<Commentaire> FindCommentaires(int offset, int limit)
|
|
{
|
|
try
|
|
{
|
|
this.logger.LogDebug("Recherche des commentaires avec offset: {Offset}, limit: {Limit}", offset, limit);
|
|
|
|
if (offset < 0 || limit <= 0)
|
|
{
|
|
this.logger.LogWarning("FindCommentaires appelé avec offset {Offset} ou limit {Limit} invalide.", offset, limit);
|
|
return Enumerable.Empty<Commentaire>();
|
|
}
|
|
|
|
var result = this.commentaires
|
|
.OrderByDescending(c => c.DateCreation)
|
|
.Skip(offset)
|
|
.Take(limit)
|
|
.ToList();
|
|
|
|
this.logger.LogDebug("{Count} commentaires trouvés", result.Count);
|
|
return result;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
this.logger.LogError(ex, "Erreur lors de la recherche des commentaires avec offset: {Offset}, limit: {Limit}", offset, limit);
|
|
throw;
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public IEnumerable<Commentaire> FindByIdTitre(int idTitre)
|
|
{
|
|
try
|
|
{
|
|
this.logger.LogDebug("Recherche des commentaires pour le titre ID: {IdTitre}", idTitre);
|
|
|
|
IEnumerable<Commentaire> result = this.commentaires
|
|
.Where(c => c.Titre != null && c.Titre.IdTitre == idTitre)
|
|
.OrderByDescending(c => c.DateCreation)
|
|
.ToList();
|
|
|
|
if (!result.Any())
|
|
{
|
|
this.logger.LogInformation("Aucun commentaire trouvé pour le titre ID '{IdTitre}'.", idTitre);
|
|
}
|
|
else
|
|
{
|
|
this.logger.LogDebug("{Count} commentaires trouvés pour le titre ID '{IdTitre}'", result.Count(), idTitre);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
this.logger.LogError(ex, "Erreur lors de la recherche des commentaires pour le titre ID: {IdTitre}", idTitre);
|
|
throw;
|
|
}
|
|
}
|
|
} |