#123 refactor: refonte de DbCommentaireRepository et LocalCommentaireRepository en prenant comme model Joséphine:
- Nettoyage du code - ajout des <inheritdoc/> - InMemoryDataStore pour le mode local
This commit is contained in:
@@ -1,198 +1,98 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Webzine.Entity;
|
||||
using Webzine.Repository.Contracts;
|
||||
// <copyright file="LocalCommentaireRepository.cs" company="PlaceholderCompany">
|
||||
// Copyright (c) PlaceholderCompany. All rights reserved.
|
||||
// </copyright>
|
||||
|
||||
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
|
||||
namespace Webzine.Repository
|
||||
{
|
||||
private readonly ILogger<LocalCommentaireRepository> logger;
|
||||
private readonly List<Commentaire> commentaires;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Webzine.Entity;
|
||||
using Webzine.Repository.Contracts;
|
||||
|
||||
/// <summary>
|
||||
/// Initialise une nouvelle instance du <see cref="LocalCommentaireRepository"/> .
|
||||
/// Initialise une classe <see cref="LocalCommentaireRepository"/> qui implémente l'interface <see cref="ICommentaireRepository"/> pour gérer les opérations liées aux commentaires.
|
||||
/// Utilise <see cref="ICommentaireRepository"/> en injection de dépendances.
|
||||
/// </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)
|
||||
public class LocalCommentaireRepository : ICommentaireRepository
|
||||
{
|
||||
this.logger = logger;
|
||||
this.commentaires = commentaires;
|
||||
this.logger.LogDebug(1, "NLog injecté dans LocalCommentaireRepository");
|
||||
}
|
||||
private readonly ILogger<LocalCommentaireRepository> logger;
|
||||
private readonly InMemoryDataStore dataStore;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Add(Commentaire commentaire)
|
||||
{
|
||||
try
|
||||
/// <summary>
|
||||
/// Initialise une nouvelle instance du <see cref="LocalCommentaireRepository"/> .
|
||||
/// Est liée à un magasin de données en mémoire et utilise un logger pour enregistrer les opérations.
|
||||
/// </summary>
|
||||
/// <param name="dataStore">Le magasin de données en mémoire. Ne peut pas être null.</param>
|
||||
/// <param name="logger">Le logger à utiliser pour enregistrer les messages de journalisation. Ne peut pas être null.</param>
|
||||
public LocalCommentaireRepository(InMemoryDataStore dataStore, ILogger<LocalCommentaireRepository> logger)
|
||||
{
|
||||
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);
|
||||
this.logger = logger;
|
||||
this.dataStore = dataStore;
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Add(Commentaire commentaire)
|
||||
{
|
||||
this.logger.LogError(ex, "Erreur lors de l'ajout du commentaire rédigé par: {Auteur}", commentaire.Auteur);
|
||||
throw;
|
||||
throw new NotSupportedException("Mode Local");
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public int Count()
|
||||
{
|
||||
try
|
||||
/// <inheritdoc/>
|
||||
public void Delete(Commentaire commentaire)
|
||||
{
|
||||
var count = this.commentaires.Count;
|
||||
this.logger.LogDebug("Nombre total de commentaires: {Count}", count);
|
||||
return count;
|
||||
throw new NotSupportedException("Mode Local");
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
||||
/// <inheritdoc/>
|
||||
public int Count()
|
||||
{
|
||||
this.logger.LogError(ex, "Erreur lors du comptage des commentaires");
|
||||
throw;
|
||||
return this.dataStore.Commentaires.Count;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Delete(Commentaire commentaire)
|
||||
{
|
||||
try
|
||||
/// <inheritdoc/>
|
||||
public Commentaire Find(int idCommentaire)
|
||||
{
|
||||
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);
|
||||
|
||||
|
||||
var commentaire = this.dataStore.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 new Commentaire();
|
||||
}
|
||||
|
||||
return commentaire;
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
||||
/// <inheritdoc/>
|
||||
public IEnumerable<Commentaire> FindAll()
|
||||
{
|
||||
this.logger.LogError(ex, "Erreur lors de la recherche du commentaire avec ID: {IdCommentaire}", idCommentaire);
|
||||
throw;
|
||||
return this.dataStore.Commentaires
|
||||
.OrderByDescending(c => c.DateCreation)
|
||||
.ToList();
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public IEnumerable<Commentaire> FindAll()
|
||||
{
|
||||
try
|
||||
/// <inheritdoc/>
|
||||
public IEnumerable<Commentaire> FindCommentaires(int offset, int limit)
|
||||
{
|
||||
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
|
||||
return this.dataStore.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
|
||||
/// <inheritdoc/>
|
||||
public IEnumerable<Commentaire> FindByIdTitre(int idTitre)
|
||||
{
|
||||
this.logger.LogDebug("Recherche des commentaires pour le titre ID: {IdTitre}", idTitre);
|
||||
|
||||
IEnumerable<Commentaire> result = this.commentaires
|
||||
return this.dataStore.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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user