Merge branch 'dev' into j2/test/pr
# Conflicts: # Webzine.WebApplication/appsettings.json
This commit is contained in:
139
Webzine.Repository/DbCommentaireRepository.cs
Normal file
139
Webzine.Repository/DbCommentaireRepository.cs
Normal file
@@ -0,0 +1,139 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Webzine.EntitiesContext;
|
||||
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 base de données.
|
||||
/// </summary>
|
||||
public class DbCommentaireRepository : ICommentaireRepository
|
||||
{
|
||||
private readonly ILogger<DbCommentaireRepository> logger;
|
||||
private readonly WebzineDbContext context;
|
||||
|
||||
/// <summary>
|
||||
/// Initialisation de <see cref="DbCommentaireRepository"/>.
|
||||
/// </summary>
|
||||
/// <param name="logger">Le service de journalisation injecté pour suivre les opérations du repository.</param>
|
||||
/// <param name="context">Le contexte de base de données injecté.</param>
|
||||
public DbCommentaireRepository(ILogger<DbCommentaireRepository> logger, WebzineDbContext context)
|
||||
{
|
||||
this.logger = logger;
|
||||
this.context = context;
|
||||
this.logger.LogDebug("NLog injecté dans DbCommentaireRepository");
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Add(Commentaire commentaire)
|
||||
{
|
||||
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);
|
||||
}
|
||||
catch (DbUpdateException dbex)
|
||||
{
|
||||
this.logger.LogError(dbex, "Erreur de base de données lors de l'ajout du commentaire de l'auteur : {Auteur}", commentaire?.Auteur);
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
this.logger.LogError(ex, "Une erreur est survenue lors de l'ajout d'un commentaire.");
|
||||
throw new Exception("Une erreur est survenue lors de l'ajout du commentaire.", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Delete(Commentaire commentaire)
|
||||
{
|
||||
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);
|
||||
}
|
||||
catch (DbUpdateException dbex)
|
||||
{
|
||||
this.logger.LogError(dbex, "Erreur de base de données lors de la suppression du commentaire : {Id}", commentaire.IdCommentaire);
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
this.logger.LogError(ex, "Une erreur est survenue lors de la suppression du commentaire {Id}.", commentaire?.IdCommentaire);
|
||||
throw new Exception("Une erreur est survenue lors de la suppression du commentaire.", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <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)
|
||||
{
|
||||
this.logger.LogDebug("Recherche du commentaire avec l'id : {Id}", idCommentaire);
|
||||
|
||||
// On inclut le titre car il est souvent affiché avec le commentaire
|
||||
return this.context.Commentaires
|
||||
.Include(c => c.Titre)
|
||||
.FirstOrDefault(c => c.IdCommentaire == idCommentaire);
|
||||
}
|
||||
|
||||
/// <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)
|
||||
.ToList();
|
||||
|
||||
this.logger.LogDebug("Nombre de commentaires trouvés : {Count}", commentaires.Count);
|
||||
return commentaires;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public IEnumerable<Commentaire> FindCommentaires(int offset, int limit)
|
||||
{
|
||||
this.logger.LogDebug("Recherche paginée des commentaires (offset : {Offset}, limit : {Limit})", offset, limit);
|
||||
|
||||
var commentaires = this.context.Commentaires
|
||||
.Include(c => c.Titre)
|
||||
.OrderByDescending(c => c.DateCreation)
|
||||
.Skip(offset)
|
||||
.Take(limit)
|
||||
.ToList();
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -2,42 +2,96 @@
|
||||
// Copyright (c) PlaceholderCompany. All rights reserved.
|
||||
// </copyright>
|
||||
|
||||
namespace Webzine.Repository
|
||||
{
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Webzine.Entity;
|
||||
using Webzine.Repository.Contracts;
|
||||
|
||||
namespace Webzine.Repository
|
||||
{
|
||||
/// <summary>
|
||||
/// 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>
|
||||
public class LocalCommentaireRepository : ICommentaireRepository
|
||||
{
|
||||
private readonly ILogger<LocalCommentaireRepository> logger;
|
||||
private readonly InMemoryDataStore dataStore;
|
||||
|
||||
public LocalCommentaireRepository(InMemoryDataStore dataStore)
|
||||
/// <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 = logger;
|
||||
this.dataStore = dataStore;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Add(Commentaire commentaire)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
throw new NotSupportedException("Mode Local");
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Delete(Commentaire commentaire)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
throw new NotSupportedException("Mode Local");
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public Commentaire Find(int id)
|
||||
public int Count()
|
||||
{
|
||||
return this.dataStore.Commentaires.Find(c => c.IdCommentaire == id);
|
||||
return this.dataStore.Commentaires.Count;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public Commentaire Find(int idCommentaire)
|
||||
{
|
||||
var commentaire = this.dataStore.Commentaires.FirstOrDefault(c => c.IdCommentaire == idCommentaire);
|
||||
if (commentaire == null)
|
||||
{
|
||||
return new Commentaire();
|
||||
}
|
||||
|
||||
return commentaire;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public IEnumerable<Commentaire> FindAll()
|
||||
{
|
||||
return this.dataStore.Commentaires.ToList();
|
||||
return this.dataStore.Commentaires
|
||||
.OrderByDescending(c => c.DateCreation)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public IEnumerable<Commentaire> FindCommentaires(int offset, int limit)
|
||||
{
|
||||
if (offset < 0 || limit <= 0)
|
||||
{
|
||||
return Enumerable.Empty<Commentaire>();
|
||||
}
|
||||
|
||||
return this.dataStore.Commentaires
|
||||
.OrderByDescending(c => c.DateCreation)
|
||||
.Skip(offset)
|
||||
.Take(limit)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public IEnumerable<Commentaire> FindByIdTitre(int idTitre)
|
||||
{
|
||||
return this.dataStore.Commentaires
|
||||
.Where(c => c.Titre != null && c.Titre.IdTitre == idTitre)
|
||||
.OrderByDescending(c => c.DateCreation)
|
||||
.ToList();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user