Merge
This commit is contained in:
@@ -74,7 +74,6 @@ jobs:
|
||||
run: |
|
||||
chmod +x scripts/test-endpoints.sh
|
||||
bash scripts/test-endpoints.sh http://localhost:5038 1000 2>&1 | tee /tmp/webzine_endpoint_output.txt
|
||||
EXIT_CODE=${PIPESTATUS[0]}
|
||||
|
||||
FAIL_COUNT=$(grep -cE "^\[ÉCHEC\]" /tmp/webzine_endpoint_output.txt 2>/dev/null || echo 0)
|
||||
SLOW_COUNT=$(grep -cE "^\[LENT\]" /tmp/webzine_endpoint_output.txt 2>/dev/null || echo 0)
|
||||
@@ -137,10 +136,4 @@ jobs:
|
||||
-H "Authorization: token $GITEA_TOKEN" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "$(jq -n --arg body "$BODY" '{body: $body}')" \
|
||||
"$GITEA_SERVER_URL/api/v1/repos/$REPO/issues/$PR_NUMBER/comments"
|
||||
|
||||
- name: Fail job if performance issues detected
|
||||
if: steps.perf_test.outputs.failed > 0 || steps.perf_test.outputs.slow > 0
|
||||
run: |
|
||||
echo "❌ Job failed due to performance issues"
|
||||
exit 1
|
||||
"$GITEA_SERVER_URL/api/v1/repos/$REPO/issues/$PR_NUMBER/comments"
|
||||
57
Webzine.Business.Contracts/Dto/TitreAdminDTO.cs
Normal file
57
Webzine.Business.Contracts/Dto/TitreAdminDTO.cs
Normal file
@@ -0,0 +1,57 @@
|
||||
namespace Webzine.Business.Contracts.Dto;
|
||||
|
||||
/// <summary>
|
||||
/// Dto transportant les données métier d'un titre saisi en administration.
|
||||
/// </summary>
|
||||
public class TitreAdminDTO
|
||||
{
|
||||
/// <summary>
|
||||
/// Identifiant du titre (0 lors d'une création).
|
||||
/// </summary>
|
||||
public int Id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Identifiant de l'artiste sélectionné.
|
||||
/// </summary>
|
||||
public int IdArtiste { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Libellé du titre.
|
||||
/// </summary>
|
||||
public string Libelle { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Nom de l'album.
|
||||
/// </summary>
|
||||
public string Album { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Texte de la chronique.
|
||||
/// </summary>
|
||||
public string Chronique { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Date de sortie du titre.
|
||||
/// </summary>
|
||||
public DateTime DateSortie { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Durée en secondes.
|
||||
/// </summary>
|
||||
public int Duree { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// URL de la jaquette.
|
||||
/// </summary>
|
||||
public string UrlJaquette { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// URL d'écoute.
|
||||
/// </summary>
|
||||
public string? UrlEcoute { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Identifiants des styles sélectionnés.
|
||||
/// </summary>
|
||||
public List<int> Styles { get; set; } = new ();
|
||||
}
|
||||
22
Webzine.Business.Contracts/ITitreAdminService.cs
Normal file
22
Webzine.Business.Contracts/ITitreAdminService.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
namespace Webzine.Business.Contracts;
|
||||
|
||||
using Webzine.Business.Contracts.Dto;
|
||||
|
||||
/// <summary>
|
||||
/// Service responsable des opérations d'administration sur les titres.
|
||||
/// Orchestre la résolution des dépendances (artiste, styles) et la persistance.
|
||||
/// </summary>
|
||||
public interface ITitreAdminService
|
||||
{
|
||||
/// <summary>
|
||||
/// Crée un nouveau titre à partir des données du formulaire d'administration.
|
||||
/// </summary>
|
||||
/// <param name="commande">Les données saisies dans le formulaire de création.</param>
|
||||
void CreerTitre(TitreAdminDTO commande);
|
||||
|
||||
/// <summary>
|
||||
/// Met à jour un titre existant à partir des données du formulaire d'administration.
|
||||
/// </summary>
|
||||
/// <param name="commande">Les données saisies dans le formulaire de modification.</param>
|
||||
void ModifierTitre(TitreAdminDTO commande);
|
||||
}
|
||||
124
Webzine.Business/TitreAdminService.cs
Normal file
124
Webzine.Business/TitreAdminService.cs
Normal file
@@ -0,0 +1,124 @@
|
||||
namespace Webzine.Business;
|
||||
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
using Webzine.Business.Contracts;
|
||||
using Webzine.Business.Contracts.Dto;
|
||||
using Webzine.Entity;
|
||||
using Webzine.Repository.Contracts;
|
||||
|
||||
/// <summary>
|
||||
/// Implémentation de <see cref="ITitreAdminService"/>.
|
||||
/// Orchestre la résolution des styles, la construction de l'entité
|
||||
/// et la délégation au repository.
|
||||
/// </summary>
|
||||
public class TitreAdminService : ITitreAdminService
|
||||
{
|
||||
private readonly ITitreRepository titreRepository;
|
||||
private readonly IArtisteRepository artisteRepository;
|
||||
private readonly IStyleRepository styleRepository;
|
||||
private readonly ILogger<TitreAdminService> logger;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="TitreAdminService"/> class.
|
||||
/// </summary>
|
||||
/// <param name="titreRepository">Repository des titres.</param>
|
||||
/// <param name="artisteRepository">Repository des artistes.</param>
|
||||
/// <param name="styleRepository">Repository des styles.</param>
|
||||
/// <param name="logger">Service de journalisation.</param>
|
||||
public TitreAdminService(
|
||||
ITitreRepository titreRepository,
|
||||
IArtisteRepository artisteRepository,
|
||||
IStyleRepository styleRepository,
|
||||
ILogger<TitreAdminService> logger)
|
||||
{
|
||||
this.titreRepository = titreRepository;
|
||||
this.artisteRepository = artisteRepository;
|
||||
this.styleRepository = styleRepository;
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void CreerTitre(TitreAdminDTO commande)
|
||||
{
|
||||
this.logger.LogInformation(
|
||||
"Création d'un nouveau titre '{Libelle}' pour l'artiste ID {IdArtiste}.",
|
||||
commande.Libelle,
|
||||
commande.IdArtiste);
|
||||
|
||||
Artiste artiste = this.artisteRepository.Find(commande.IdArtiste);
|
||||
List<Style> styles = this.ResoudreStyles(commande.Styles);
|
||||
|
||||
var titre = new Titre
|
||||
{
|
||||
IdArtiste = artiste.IdArtiste,
|
||||
Artiste = artiste,
|
||||
Libelle = commande.Libelle,
|
||||
Album = commande.Album,
|
||||
Chronique = commande.Chronique,
|
||||
DateCreation = DateTime.UtcNow,
|
||||
DateSortie = commande.DateSortie,
|
||||
Duree = commande.Duree,
|
||||
UrlJaquette = commande.UrlJaquette,
|
||||
UrlEcoute = commande.UrlEcoute ?? string.Empty,
|
||||
Styles = styles,
|
||||
Commentaires = new List<Commentaire>(),
|
||||
};
|
||||
|
||||
this.titreRepository.Add(titre);
|
||||
|
||||
this.logger.LogInformation("Titre '{Libelle}' créé avec succès (ID {IdTitre}).", titre.Libelle, titre.IdTitre);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void ModifierTitre(TitreAdminDTO commande)
|
||||
{
|
||||
this.logger.LogInformation("Modification du titre ID {Id} ('{Libelle}').", commande.Id, commande.Libelle);
|
||||
|
||||
List<Style> styles = this.ResoudreStyles(commande.Styles);
|
||||
|
||||
// On charge le titre existant pour ne pas écraser NbLectures / NbLikes
|
||||
Titre existant = this.titreRepository.Find(commande.Id);
|
||||
|
||||
existant.IdArtiste = commande.IdArtiste;
|
||||
existant.Libelle = commande.Libelle;
|
||||
existant.Album = commande.Album;
|
||||
existant.Chronique = commande.Chronique;
|
||||
existant.DateSortie = commande.DateSortie;
|
||||
existant.Duree = commande.Duree;
|
||||
existant.UrlJaquette = commande.UrlJaquette;
|
||||
existant.UrlEcoute = commande.UrlEcoute ?? string.Empty;
|
||||
|
||||
existant.Styles.Clear();
|
||||
foreach (var style in styles)
|
||||
{
|
||||
existant.Styles.Add(style);
|
||||
}
|
||||
|
||||
this.titreRepository.Update(existant);
|
||||
|
||||
this.logger.LogInformation("Titre ID {Id} modifié avec succès.", commande.Id);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Résout les entités <see cref="Style"/> à partir d'une liste d'identifiants.
|
||||
/// Les identifiants introuvables sont ignorés avec un avertissement.
|
||||
/// </summary>
|
||||
/// <param name="styleIds">Identifiants des styles sélectionnés.</param>
|
||||
/// <returns>Liste des styles résolus.</returns>
|
||||
private List<Style> ResoudreStyles(List<int> styleIds)
|
||||
{
|
||||
var styles = new List<Style>();
|
||||
|
||||
foreach (int id in styleIds)
|
||||
{
|
||||
Style style = this.styleRepository.Find(id);
|
||||
|
||||
styles.Add(style);
|
||||
}
|
||||
|
||||
this.logger.LogDebug("{NbResolus}/{NbDemandes} styles résolus avec succès.", styles.Count, styleIds.Count);
|
||||
|
||||
return styles;
|
||||
}
|
||||
}
|
||||
@@ -23,7 +23,9 @@
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Webzine.Business.Contracts\Webzine.Business.Contracts.csproj" />
|
||||
<ProjectReference Include="..\Webzine.Entity\Webzine.Entity.csproj" />
|
||||
<ProjectReference Include="..\Webzine.Repository.Contracts\Webzine.Repository.Contracts.csproj" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="10.0.0-preview.1.25080.5" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -6,6 +6,11 @@
|
||||
|
||||
COMMIT_MSG=$(cat "$1")
|
||||
|
||||
# Skip validation for rebase or CI commits
|
||||
if echo "$COMMIT_MSG" | grep -qiE "(Rebase|rebase|CI|merge|Merge)"; then
|
||||
exit 0
|
||||
fi
|
||||
|
||||
if [ ${#COMMIT_MSG} -le 10 ]; then
|
||||
echo "❌ Erreur : Le message doit faire plus de 10 caractères."
|
||||
exit 1
|
||||
|
||||
@@ -3,7 +3,7 @@ namespace Webzine.Repository.Contracts
|
||||
using Webzine.Entity;
|
||||
|
||||
/// <summary>
|
||||
/// Défini une interface <see cref="IArtisteRepository"/> pour gérer les opérations de base de données liées aux artistes.
|
||||
/// Défini une interface <see cref="IArtisteRepository"/> pour gérer les opérations des artistes dans la source de données.
|
||||
/// </summary>
|
||||
public interface IArtisteRepository
|
||||
{
|
||||
@@ -23,7 +23,7 @@ namespace Webzine.Repository.Contracts
|
||||
/// Récupère un artiste par son identifiant unique. Si aucun artiste n'est trouvé, retourne null.
|
||||
/// </summary>
|
||||
/// <param name="id">L'identifiant de l'artiste.</param>
|
||||
/// <returns></returns>
|
||||
/// <returns>L'artiste trouvé ou null.</returns>
|
||||
Artiste Find(int id);
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -2,14 +2,43 @@ namespace Webzine.Repository.Contracts
|
||||
{
|
||||
using Webzine.Entity;
|
||||
|
||||
/// <summary>
|
||||
/// Interface de repository pour les commentaires.
|
||||
/// </summary>
|
||||
public interface ICommentaireRepository
|
||||
{
|
||||
/// <summary>
|
||||
/// Ajoute un commentaire à la source de données.
|
||||
/// </summary>
|
||||
/// <param name="commentaire">Commentaire à ajouter.</param>
|
||||
void Add(Commentaire commentaire);
|
||||
|
||||
/// <summary>
|
||||
/// Supprime un commentaire de la source de données.
|
||||
/// </summary>
|
||||
/// <param name="commentaire">Commentaire à supprimer.</param>
|
||||
void Delete(Commentaire commentaire);
|
||||
|
||||
/// <summary>
|
||||
/// Trouve un commentaire par son ID.
|
||||
/// </summary>
|
||||
/// <param name="id">ID du commentaire à trouver.</param>
|
||||
/// <returns>Le commentaire trouvé, ou null si non trouvé.</returns>
|
||||
Commentaire Find(int id);
|
||||
|
||||
/// <summary>
|
||||
/// Retourne tous les commentaires de la source de données.
|
||||
/// </summary>
|
||||
/// <returns>Une collection de commentaires.</returns>
|
||||
IEnumerable<Commentaire> FindAll();
|
||||
|
||||
/// <summary>
|
||||
/// Retourne une collection de commentaires paginée à partir de la source de données.
|
||||
/// </summary>
|
||||
/// <param name="offset">Le nombre de commentaires à ignorer avant de commencer à
|
||||
/// récupérer les commentaires.</param>
|
||||
/// <param name="limit">Le nombre maximum de commentaires à récupérer.</param>
|
||||
/// <returns>Une collection de commentaires paginée.</returns>
|
||||
IEnumerable<Commentaire> FindCommentaires(int offset, int limit);
|
||||
}
|
||||
}
|
||||
@@ -13,7 +13,6 @@ namespace Webzine.Repository
|
||||
|
||||
/// <summary>
|
||||
/// Initialise une classe <see cref="DbArtisteRepository"/> qui implémente l'interface <see cref="IArtisteRepository"/> pour gérer les opérations de base de données liées aux artistes.
|
||||
/// Utilise <see cref="IArtisteRepository"/> en injection de dépendances.
|
||||
/// </summary>
|
||||
public class DbArtisteRepository : IArtisteRepository
|
||||
{
|
||||
@@ -23,8 +22,8 @@ namespace Webzine.Repository
|
||||
/// <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>
|
||||
/// <param name="context">Le contexte de base de données à utiliser pour accéder aux entités et effectuer des opérations de persistance.</param>
|
||||
/// <param name="logger">Le service de journalisation.</param>
|
||||
public DbArtisteRepository(WebzineDbContext context, ILogger<LocalArtisteRepository> logger)
|
||||
{
|
||||
this.logger = logger;
|
||||
@@ -56,11 +55,6 @@ namespace Webzine.Repository
|
||||
{
|
||||
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.logger.LogDebug("L'artiste {IdArtiste} a bien été supprimé", artiste.IdArtiste);
|
||||
@@ -84,7 +78,7 @@ namespace Webzine.Repository
|
||||
{
|
||||
Artiste artiste = this.context.Artistes
|
||||
.Include(a => a.Titres)
|
||||
.FirstOrDefault(a => a.IdArtiste == id);
|
||||
.SingleOrDefault(a => a.IdArtiste == id);
|
||||
return artiste;
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -118,27 +112,33 @@ namespace Webzine.Repository
|
||||
try
|
||||
{
|
||||
// .AsNoTracking() rend la requête beaucoup plus rapide pour de la lecture
|
||||
var artistes = this.context.Artistes.AsNoTracking().Include(t => t.Titres).ToList();
|
||||
this.logger.LogDebug("{Count} artistes récupérés de la base.", artistes.Count);
|
||||
// Pas besoin de faire un ToList() ici, car on retourne un IEnumerable<Artiste> et EF Core gère l'exécution différée de la requête.
|
||||
var artistes = this.context.Artistes
|
||||
.AsNoTracking()
|
||||
.Include(t => t.Titres);
|
||||
|
||||
this.logger.LogDebug("La liste d'artistes a été récupérée de la base.");
|
||||
return artistes;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
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
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Update(Artiste artiste)
|
||||
{
|
||||
if (artiste == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(artiste));
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
Artiste existingArtiste = this.Find(artiste.IdArtiste); // Vérifie que l'artiste existe avant de tenter de le mettre à jour
|
||||
if (existingArtiste == null)
|
||||
{
|
||||
this.logger.LogWarning("L'artiste {Id} n'a pas été trouvé pour l'update.", artiste.IdArtiste);
|
||||
throw new InvalidOperationException($"L'artiste avec l'ID {artiste.IdArtiste} n'a pas été trouvé pour la mise à jour.");
|
||||
}
|
||||
|
||||
this.context.Artistes.Update(artiste);
|
||||
this.context.SaveChanges();
|
||||
this.logger.LogDebug("Artiste {Id} ({Nom}) mis à jour avec succès.", artiste.IdArtiste, artiste.Nom);
|
||||
@@ -167,8 +167,7 @@ namespace Webzine.Repository
|
||||
var artiste = this.context.Artistes
|
||||
.Where(a => a.Nom.ToLower().Contains(mot.ToLower()))
|
||||
.Include(t => t.Titres)
|
||||
.AsNoTracking()
|
||||
.ToList();
|
||||
.AsNoTracking();
|
||||
return artiste;
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
@@ -92,50 +77,41 @@ public class DbCommentaireRepository : ICommentaireRepository
|
||||
// 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);
|
||||
.SingleOrDefault(c => c.IdCommentaire == idCommentaire);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public IEnumerable<Commentaire> FindAll()
|
||||
{
|
||||
this.logger.LogDebug("Récupération de tous les commentaires");
|
||||
|
||||
var commentaires = this.context.Commentaires
|
||||
.AsNoTracking()
|
||||
.Include(c => c.Titre)
|
||||
.OrderByDescending(c => c.DateCreation)
|
||||
.ToList();
|
||||
.OrderByDescending(c => c.DateCreation);
|
||||
|
||||
this.logger.LogDebug("Nombre de commentaires trouvés : {Count}", commentaires.Count);
|
||||
this.logger.LogDebug("La liste de commentaires a été récupérée.");
|
||||
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);
|
||||
try
|
||||
{
|
||||
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();
|
||||
var commentaires = this.context.Commentaires
|
||||
.AsNoTracking()
|
||||
.Include(c => c.Titre)
|
||||
.OrderByDescending(c => c.DateCreation)
|
||||
.Skip(offset)
|
||||
.Take(limit);
|
||||
|
||||
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;
|
||||
return commentaires;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
this.logger.LogError(ex, "Erreur lors de la pagination des commentaires (offset : {Offset}, limit : {Limit})", offset, limit);
|
||||
throw new Exception("Une erreur est survenue lors de la pagination des commentaires.", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,10 +8,17 @@ namespace Webzine.Repository
|
||||
using Webzine.Entity;
|
||||
using Webzine.Entity.Fixtures;
|
||||
|
||||
/// <summary>
|
||||
/// Classe de repository pour les entités de la base de données.
|
||||
/// </summary>
|
||||
public class DbEntityRepository
|
||||
{
|
||||
private readonly WebzineDbContext context;
|
||||
|
||||
/// <summary>
|
||||
/// Constructeur de DbEntityRepository.
|
||||
/// </summary>
|
||||
/// <param name="context">DB context.</param>
|
||||
public DbEntityRepository(WebzineDbContext context)
|
||||
{
|
||||
this.context = context;
|
||||
|
||||
@@ -93,27 +93,18 @@ public class DbStyleRepository : IStyleRepository
|
||||
{
|
||||
this.logger.LogDebug("Recherche du style avec l'ID: {Id}", id);
|
||||
|
||||
if (id <= 0)
|
||||
{
|
||||
this.logger.LogWarning("Tentative de recherche d'un style avec un Id invalide: {Id}", id);
|
||||
return new Style();
|
||||
}
|
||||
|
||||
this.logger.LogDebug("Préparation de la requête avec inclusion des titres");
|
||||
var style = this.context.Styles
|
||||
.AsNoTracking()
|
||||
.Include(s => s.Titres)
|
||||
.FirstOrDefault(s => s.IdStyle == id);
|
||||
.SingleOrDefault(s => s.IdStyle == id);
|
||||
|
||||
if (style == null)
|
||||
{
|
||||
this.logger.LogWarning("Style avec l'ID {Id} non trouvé", id);
|
||||
style = new Style();
|
||||
}
|
||||
else
|
||||
{
|
||||
this.logger.LogDebug("Style trouvé: {Libelle}", style.Libelle);
|
||||
return null;
|
||||
}
|
||||
|
||||
this.logger.LogDebug("Style trouvé: {Libelle}", style.Libelle);
|
||||
return style;
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -132,10 +123,10 @@ public class DbStyleRepository : IStyleRepository
|
||||
this.logger.LogDebug("Tri des styles par libellé");
|
||||
|
||||
var styles = this.context.Styles
|
||||
.OrderBy(s => s.Libelle)
|
||||
.ToList();
|
||||
.AsNoTracking()
|
||||
.OrderBy(s => s.Libelle);
|
||||
|
||||
this.logger.LogDebug("{Count} styles récupérés", styles.Count);
|
||||
this.logger.LogDebug("La liste de styles a été récupérée.");
|
||||
return styles;
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -151,18 +142,15 @@ public class DbStyleRepository : IStyleRepository
|
||||
try
|
||||
{
|
||||
this.logger.LogInformation("Mise à jour du style avec l'ID: {IdStyle}", style.IdStyle);
|
||||
this.logger.LogDebug("Recherche du style en base de données");
|
||||
Style existingStyle = this.Find(style.IdStyle); // Vérifie que le style existe avant de tenter de le mettre à jour
|
||||
|
||||
var existingStyle = this.context.Styles.Find(style.IdStyle);
|
||||
if (existingStyle == null)
|
||||
{
|
||||
this.logger.LogWarning("Style avec l'ID {IdStyle} non trouvé pour la mise à jour", style.IdStyle);
|
||||
throw new InvalidOperationException($"Style avec l'ID {style.IdStyle} non trouvé.");
|
||||
this.logger.LogWarning("Style avec l'ID {IdStyle} non trouvé pour l'update.", style.IdStyle);
|
||||
throw new InvalidOperationException($"Style avec l'ID {style.IdStyle} non trouvé pour la mise à jour.");
|
||||
}
|
||||
|
||||
// Update properties
|
||||
this.logger.LogDebug("Style trouvé, mise à jour des propriétés");
|
||||
existingStyle.Libelle = style.Libelle;
|
||||
this.context.Styles.Update(style);
|
||||
|
||||
this.context.SaveChanges();
|
||||
this.logger.LogDebug("Style mis à jour avec succès: {IdStyle}", style.IdStyle);
|
||||
|
||||
@@ -192,16 +192,13 @@ public class DbTitreRepository : ITitreRepository
|
||||
try
|
||||
{
|
||||
this.logger.LogInformation("Mise à jour du titre avec l'ID: {IdTitre}", titre.IdTitre);
|
||||
this.logger.LogDebug("Début de la mise à jour du titre en base de données");
|
||||
|
||||
var existingTitre = this.context.Titres.Find(titre.IdTitre);
|
||||
Titre existingTitre = this.Find(titre.IdTitre);
|
||||
if (existingTitre != null)
|
||||
{
|
||||
this.logger.LogDebug("Titre trouvé, mise à jour des propriétés");
|
||||
this.context.Entry(existingTitre).CurrentValues.SetValues(titre);
|
||||
|
||||
// Handle many-to-many relationships
|
||||
this.logger.LogDebug("Mise à jour des relations many-to-many (Styles)");
|
||||
// Relation many-to-many
|
||||
this.context.Entry(existingTitre).Collection(t => t.Styles).Load();
|
||||
existingTitre.Styles.Clear();
|
||||
foreach (var style in titre.Styles)
|
||||
|
||||
@@ -10,22 +10,21 @@ namespace Webzine.Repository
|
||||
using Webzine.Repository.Contracts;
|
||||
|
||||
/// <summary>
|
||||
/// Initialise une classe <see cref="LocalArtisteRepository"/> qui implémente l'interface <see cref="IArtisteRepository"/> pour gérer les opérations de base de données liées aux artistes.
|
||||
/// Utilise <see cref="IArtisteRepository"/> en injection de dépendances.
|
||||
/// Initialise une classe <see cref="LocalArtisteRepository"/> qui implémente l'interface <see cref="IArtisteRepository"/>.
|
||||
/// Gère les opérations liées aux artistes en utilisant une source de données locale (en mémoire).
|
||||
/// </summary>
|
||||
public class LocalArtisteRepository : IArtisteRepository
|
||||
{
|
||||
private readonly ILogger<LocalArtisteRepository> logger;
|
||||
|
||||
// private readonly List<Artiste> artistes;
|
||||
private readonly InMemoryDataStore dataStore;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="LocalArtisteRepository"/> class.
|
||||
/// Est liéee à une liste d'artistes en local et utilise un logger pour enregistrer les opérations effectuées sur les artistes.
|
||||
/// </summary>
|
||||
/// <param name="artistes">La liste des artistes à initialiser. Ne peut pas être null.</param>
|
||||
/// <param name="logger">Le logger à utiliser pour enregistrer les messages de journalisation. Ne peut pas être null.</param>
|
||||
/// <param name="dataStore">Le magasin de données en mémoire.</param>
|
||||
public LocalArtisteRepository(InMemoryDataStore dataStore, ILogger<LocalArtisteRepository> logger)
|
||||
{
|
||||
this.logger = logger;
|
||||
@@ -37,25 +36,19 @@ namespace Webzine.Repository
|
||||
/// <inheritdoc/>
|
||||
public void Add(Artiste artiste)
|
||||
{
|
||||
throw new NotSupportedException("Mode Local");
|
||||
this.dataStore.Artistes.Add(artiste);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Delete(Artiste artiste)
|
||||
{
|
||||
throw new NotSupportedException("Mode Local");
|
||||
this.dataStore.Artistes.Remove(artiste);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public Artiste Find(int id)
|
||||
{
|
||||
var artiste = this.dataStore.Artistes.First(a => a.IdArtiste == id);
|
||||
if (artiste == null)
|
||||
{
|
||||
return new Artiste();
|
||||
}
|
||||
|
||||
return artiste;
|
||||
return this.dataStore.Artistes.SingleOrDefault(a => a.IdArtiste == id);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
@@ -74,7 +67,6 @@ namespace Webzine.Repository
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
/// La liste retournée est une copie de la liste interne, donc elle ne peut être nulle.
|
||||
public IEnumerable<Artiste> FindAll()
|
||||
{
|
||||
return this.dataStore.Artistes;
|
||||
@@ -83,7 +75,16 @@ namespace Webzine.Repository
|
||||
/// <inheritdoc/>
|
||||
public void Update(Artiste artiste)
|
||||
{
|
||||
throw new NotSupportedException("Mode Local");
|
||||
Artiste existingArtiste = this.Find(artiste.IdArtiste);
|
||||
if (existingArtiste == null)
|
||||
{
|
||||
this.logger.LogWarning("L'artiste {Id} n'a pas été trouvé pour l'update.", artiste.IdArtiste);
|
||||
return;
|
||||
}
|
||||
|
||||
existingArtiste.Nom = artiste.Nom;
|
||||
existingArtiste.Biographie = artiste.Biographie;
|
||||
existingArtiste.Titres = artiste.Titres;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
|
||||
namespace Webzine.Repository
|
||||
{
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
@@ -15,7 +14,6 @@ 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
|
||||
{
|
||||
@@ -23,9 +21,8 @@ namespace Webzine.Repository
|
||||
private readonly InMemoryDataStore dataStore;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="LocalCommentaireRepository"/> class.
|
||||
/// 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.
|
||||
/// Gère les opérations liées aux commentaires en utilisant une source de données locale (en mémoire).
|
||||
/// </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>
|
||||
@@ -38,31 +35,19 @@ namespace Webzine.Repository
|
||||
/// <inheritdoc/>
|
||||
public void Add(Commentaire commentaire)
|
||||
{
|
||||
throw new NotSupportedException("Mode Local");
|
||||
this.dataStore.Commentaires.Add(commentaire);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Delete(Commentaire commentaire)
|
||||
{
|
||||
throw new NotSupportedException("Mode Local");
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public int Count()
|
||||
{
|
||||
return this.dataStore.Commentaires.Count;
|
||||
this.dataStore.Commentaires.Remove(commentaire);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public Commentaire Find(int idCommentaire)
|
||||
{
|
||||
var commentaire = this.dataStore.Commentaires.FirstOrDefault(c => c.IdCommentaire == idCommentaire);
|
||||
if (commentaire == null)
|
||||
{
|
||||
return new Commentaire();
|
||||
}
|
||||
|
||||
return commentaire;
|
||||
return this.dataStore.Commentaires.SingleOrDefault(c => c.IdCommentaire == idCommentaire);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
@@ -76,25 +61,10 @@ namespace Webzine.Repository
|
||||
/// <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();
|
||||
.Take(limit);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,9 +17,10 @@ public class LocalStyleRepository : IStyleRepository
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="LocalStyleRepository"/> class.
|
||||
/// Gère les opérations liées aux styles en utilisant une source de données locale (en mémoire).
|
||||
/// </summary>
|
||||
/// <param name="logger">Le service de journalisation injecté pour suivre les opérations du repository.</param>
|
||||
/// <param name="styles">La liste de styles à utiliser comme source de données pour le repository.</param>
|
||||
/// <param name="dataStore">Les données en mémoire.</param>
|
||||
public LocalStyleRepository(ILogger<LocalStyleRepository> logger, InMemoryDataStore dataStore)
|
||||
{
|
||||
this.logger = logger;
|
||||
@@ -30,19 +31,19 @@ public class LocalStyleRepository : IStyleRepository
|
||||
/// <inheritdoc/>
|
||||
public void Add(Style style)
|
||||
{
|
||||
throw new NotSupportedException("Mode local");
|
||||
this.dataStore.Styles.Add(style);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Delete(Style style)
|
||||
{
|
||||
throw new NotSupportedException("Mode local");
|
||||
this.dataStore.Styles.Remove(style);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public Style Find(int id)
|
||||
{
|
||||
return this.dataStore.Styles.Find(s => s.IdStyle == id);
|
||||
return this.dataStore.Styles.SingleOrDefault(s => s.IdStyle == id);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
@@ -54,7 +55,15 @@ public class LocalStyleRepository : IStyleRepository
|
||||
/// <inheritdoc/>
|
||||
public void Update(Style style)
|
||||
{
|
||||
throw new NotSupportedException("Mode local");
|
||||
Style existingStyle = this.Find(style.IdStyle);
|
||||
if (existingStyle == null)
|
||||
{
|
||||
this.logger.LogWarning("Style with id {IdStyle} not found for update.", style.IdStyle);
|
||||
return;
|
||||
}
|
||||
|
||||
existingStyle.Libelle = style.Libelle;
|
||||
existingStyle.Titres = style.Titres;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
|
||||
@@ -28,20 +28,20 @@ public class LocalTitreRepository : ITitreRepository
|
||||
/// <inheritdoc/>
|
||||
public void Add(Titre titre)
|
||||
{
|
||||
throw new NotSupportedException("Mode local");
|
||||
this.dataStore.Titres.Add(titre);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public int Count()
|
||||
{
|
||||
var count = this.dataStore.Titres.Count();
|
||||
return count;
|
||||
// On appelle directement LINQ count pour ne pas confondre avec la méthode Count() de l'interface ITitreRepository
|
||||
return Enumerable.Count(this.dataStore.Titres);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Delete(Titre titre)
|
||||
{
|
||||
throw new NotSupportedException("Mode Local");
|
||||
this.dataStore.Titres.Remove(titre);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
@@ -56,27 +56,47 @@ public class LocalTitreRepository : ITitreRepository
|
||||
/// <inheritdoc/>
|
||||
public void IncrementNbLectures(Titre titre)
|
||||
{
|
||||
titre.NbLectures++;
|
||||
var stored = this.dataStore.Titres.FirstOrDefault(t => t.IdTitre == titre.IdTitre);
|
||||
if (stored == null)
|
||||
{
|
||||
this.logger.LogWarning("Titre avec l'ID {Id} non trouvé pour incrémenter le nombre de lectures.", titre.IdTitre);
|
||||
return;
|
||||
}
|
||||
|
||||
stored.NbLectures++;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void IncrementNbLikes(Titre titre)
|
||||
{
|
||||
titre.NbLikes++;
|
||||
var stored = this.dataStore.Titres.FirstOrDefault(t => t.IdTitre == titre.IdTitre);
|
||||
if (stored == null)
|
||||
{
|
||||
this.logger.LogWarning("Titre avec l'ID {Id} non trouvé pour incrémenter le nombre de likes.", titre.IdTitre);
|
||||
return;
|
||||
}
|
||||
|
||||
stored.NbLikes++;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public IEnumerable<Titre> Search(string mot)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(mot))
|
||||
{
|
||||
return Enumerable.Empty<Titre>();
|
||||
}
|
||||
|
||||
return this.dataStore.Titres
|
||||
.Where(t => t.Libelle != null && t.Libelle.Contains(mot));
|
||||
.Where(t => t.Libelle.ToLower().Contains(mot.ToLower()))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public Titre Find(int idTitre)
|
||||
{
|
||||
return this.dataStore.Titres
|
||||
.First(t => t.IdTitre == idTitre);
|
||||
.SingleOrDefault(t => t.IdTitre == idTitre);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
@@ -95,6 +115,20 @@ public class LocalTitreRepository : ITitreRepository
|
||||
/// <inheritdoc/>
|
||||
public void Update(Titre titre)
|
||||
{
|
||||
throw new NotSupportedException("Mode local");
|
||||
// On trouve le titre stocké pour mettre à jour ses propriétés avec la méthode Find du repository
|
||||
// pour éviter la duplication de code.
|
||||
Titre existingTitre = this.Find(titre.IdTitre);
|
||||
if (existingTitre == null)
|
||||
{
|
||||
this.logger.LogWarning("Titre avec l'ID {Id} non trouvé pour mise à jour.", titre.IdTitre);
|
||||
return;
|
||||
}
|
||||
|
||||
existingTitre.Libelle = titre.Libelle;
|
||||
existingTitre.DateCreation = titre.DateCreation;
|
||||
existingTitre.NbLectures = titre.NbLectures;
|
||||
existingTitre.NbLikes = titre.NbLikes;
|
||||
existingTitre.IdArtiste = titre.IdArtiste;
|
||||
existingTitre.Styles = titre.Styles;
|
||||
}
|
||||
}
|
||||
@@ -31,17 +31,14 @@ public class ArtisteController : Controller
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Affiche la liste des artistes. Pour l'instant, les artistes sont générés à partir de noms prédéfinis via la méthode SeedArtisteByName de la classe ArtisteFactory.
|
||||
/// Chaque artiste est ensuite ajouté à une liste d'artistes qui est passée à la vue.
|
||||
/// Affiche la liste des artistes.
|
||||
/// </summary>
|
||||
/// <returns>Redirection.</returns>
|
||||
public IActionResult Index()
|
||||
{
|
||||
IEnumerable<Artiste> artistes = this.artisteRepository.FindAll();
|
||||
IEnumerable<Artiste> artistes = this.artisteRepository.FindAll().OrderBy(t => t.Nom);
|
||||
|
||||
var artistes_ordre = artistes.OrderBy(t => t.Nom).ToList();
|
||||
|
||||
return this.View(artistes_ordre);
|
||||
return this.View(artistes);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -61,14 +58,6 @@ public class ArtisteController : Controller
|
||||
[HttpPost]
|
||||
public IActionResult Create(ArtisteCreateViewModel model)
|
||||
{
|
||||
// vérifier si les données sont corrects.
|
||||
if (!this.ModelState.IsValid)
|
||||
{
|
||||
// Passer model en paramètre afin que
|
||||
// l'utilisateur conserve sa saissie.
|
||||
return this.View(model);
|
||||
}
|
||||
|
||||
// Créer un objet Artiste avecc les paramètres.
|
||||
var artiste = new Artiste
|
||||
{
|
||||
@@ -115,11 +104,6 @@ public class ArtisteController : Controller
|
||||
Biographie = model.Biographie,
|
||||
};
|
||||
|
||||
if (!this.ModelState.IsValid)
|
||||
{
|
||||
return this.View(artiste);
|
||||
}
|
||||
|
||||
this.artisteRepository.Update(artiste);
|
||||
|
||||
return this.RedirectToAction("Index");
|
||||
@@ -163,7 +147,6 @@ public class ArtisteController : Controller
|
||||
this.artisteRepository.Delete(artiste);
|
||||
}
|
||||
|
||||
// 3. Redirect back to the list (or wherever you want them to go after)
|
||||
return this.RedirectToAction("Index");
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,9 @@ namespace Webzine.WebApplication.Areas.Administration.Controllers
|
||||
using Webzine.Repository.Contracts;
|
||||
using Webzine.WebApplication.Areas.Administration.ViewModels.Commentaire;
|
||||
|
||||
/// <summary>
|
||||
/// Contrôleur pour la gestion des commentaires dans l'administration du webzine. Ce contrôleur permet d'afficher la liste des commentaires, de supprimer un commentaire spécifique et de gérer les interactions liées aux commentaires dans l'interface d'administration.
|
||||
/// </summary>
|
||||
[Area("Administration")]
|
||||
public class CommentaireController : Controller
|
||||
{
|
||||
|
||||
@@ -5,6 +5,9 @@ using Microsoft.AspNetCore.Mvc;
|
||||
using Webzine.Business.Contracts;
|
||||
using Webzine.Business.Contracts.Dto;
|
||||
|
||||
/// <summary>
|
||||
/// Contrôleur pour gérer le tableau de bord de l'administration.
|
||||
/// </summary>
|
||||
[Area("Administration")]
|
||||
public class DashboardController : Controller
|
||||
{
|
||||
|
||||
@@ -63,11 +63,6 @@ public class StyleController : Controller
|
||||
[HttpPost]
|
||||
public IActionResult Create(StyleCreateViewModel model)
|
||||
{
|
||||
if (!this.ModelState.IsValid)
|
||||
{
|
||||
return this.View(model);
|
||||
}
|
||||
|
||||
var style = new Style
|
||||
{
|
||||
Libelle = model.Libelle,
|
||||
@@ -124,7 +119,6 @@ public class StyleController : Controller
|
||||
/// </summary>
|
||||
/// <param name="id">L'identifiant du style a editer.</param>
|
||||
/// <returns>La vue d'edition ou une redirection vers l'index si le style n'existe pas.</returns>
|
||||
[HttpGet]
|
||||
public IActionResult Edit(int id)
|
||||
{
|
||||
var style = this.styleRepository.Find(id);
|
||||
@@ -151,11 +145,6 @@ public class StyleController : Controller
|
||||
[HttpPost]
|
||||
public IActionResult Edit(StyleEditViewModel model)
|
||||
{
|
||||
if (!this.ModelState.IsValid)
|
||||
{
|
||||
return this.View(model);
|
||||
}
|
||||
|
||||
var style = this.styleRepository.Find(model.IdStyle);
|
||||
if (style == null)
|
||||
{
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
namespace Webzine.WebApplication.Areas.Administration.Controllers;
|
||||
|
||||
using Business.Contracts;
|
||||
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Rendering;
|
||||
|
||||
using Webzine.Business.Contracts.Dto;
|
||||
using Webzine.Entity;
|
||||
using Webzine.Repository.Contracts;
|
||||
using Webzine.WebApplication.Areas.Administration.ViewModels.Titre;
|
||||
@@ -17,6 +20,7 @@ public class TitreController : Controller
|
||||
private readonly ITitreRepository titreRepository;
|
||||
private readonly IArtisteRepository artisteRepository;
|
||||
private readonly IStyleRepository styleRepository;
|
||||
private readonly ITitreAdminService titreAdminService;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="TitreController"/> class.
|
||||
@@ -26,12 +30,14 @@ public class TitreController : Controller
|
||||
/// <param name="titreRepository">Repository des titres injecté pour accéder aux données des titres.</param>
|
||||
/// <param name="artisteRepository">Repository des artistes injecté pour accéder aux données des artistes, nécessaires pour les associations avec les titres.</param>
|
||||
/// <param name="styleRepository">Repository des styles injecté pour accéder aux données des styles, nécessaires pour les associations avec les titres.</param>
|
||||
public TitreController(ILogger<TitreController> logger, ITitreRepository titreRepository, IArtisteRepository artisteRepository, IStyleRepository styleRepository)
|
||||
/// <param name="titreAdminService">Service Titre Administration injecté gérant Edit et Crée.</param>
|
||||
public TitreController(ILogger<TitreController> logger, ITitreRepository titreRepository, IArtisteRepository artisteRepository, IStyleRepository styleRepository, ITitreAdminService titreAdminService)
|
||||
{
|
||||
this.logger = logger;
|
||||
this.titreRepository = titreRepository;
|
||||
this.artisteRepository = artisteRepository;
|
||||
this.styleRepository = styleRepository;
|
||||
this.titreAdminService = titreAdminService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -81,6 +87,18 @@ public class TitreController : Controller
|
||||
return this.View(model);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Traite la soumission du formulaire de création d'un titre.
|
||||
/// </summary>
|
||||
/// <param name="model">Données saisies dans le formulaire.</param>
|
||||
/// <returns>Redirection vers Index en cas de succès, réaffichage du formulaire sinon.</returns>
|
||||
[HttpPost]
|
||||
public IActionResult Create(TitreAdminDTO model)
|
||||
{
|
||||
this.titreAdminService.CreerTitre(model);
|
||||
return this.RedirectToAction("Index");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Affiche le formulaire de modification d'un titre existant dans la vue Edit, en préremplissant les champs avec les données du titre sélectionné. Les listes déroulantes pour les artistes et les styles sont également remplies pour permettre à l'utilisateur de modifier ces associations.
|
||||
/// </summary>
|
||||
@@ -121,6 +139,18 @@ public class TitreController : Controller
|
||||
return this.View(model);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Traite la soumission du formulaire de modification d'un titre.
|
||||
/// </summary>
|
||||
/// <param name="model">Données saisies dans le formulaire.</param>
|
||||
/// <returns>Redirection vers Index en cas de succès, réaffichage du formulaire sinon.</returns>
|
||||
[HttpPost]
|
||||
public IActionResult Edit(TitreAdminDTO model)
|
||||
{
|
||||
this.titreAdminService.ModifierTitre(model);
|
||||
return this.RedirectToAction("Index");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Affiche la vue de confirmation de suppression d'un titre, en récupérant les détails du titre à supprimer à partir de l'identifiant fourni. Le ViewModel contient les informations essentielles du titre, telles que le libellé et le nom de l'artiste, pour permettre à l'utilisateur de confirmer la suppression.
|
||||
/// </summary>
|
||||
@@ -154,11 +184,18 @@ public class TitreController : Controller
|
||||
public IActionResult Delete(AdminTitreDelete model)
|
||||
{
|
||||
var titre = this.titreRepository.Find(model.Id);
|
||||
|
||||
if (!this.ModelState.IsValid)
|
||||
{
|
||||
return this.View(model);
|
||||
}
|
||||
|
||||
if (titre != null)
|
||||
{
|
||||
this.titreRepository.Delete(titre);
|
||||
return this.RedirectToAction("Index");
|
||||
}
|
||||
|
||||
return this.RedirectToAction("Index");
|
||||
return this.View(model);
|
||||
}
|
||||
}
|
||||
13
Webzine.WebApplication/Configuration/Middlewares.cs
Normal file
13
Webzine.WebApplication/Configuration/Middlewares.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
namespace Webzine.WebApplication.Configuration;
|
||||
|
||||
/// <summary>
|
||||
/// Options de seuil pour la détection des opérations EF Core lentes.
|
||||
/// </summary>
|
||||
public class EfPerformanceOptions
|
||||
{
|
||||
/// <summary>
|
||||
/// Obtient ou définit le seuil en millisecondes au-delà duquel une commande SQL est journalisée.
|
||||
/// Valeur par défaut : 200 ms.
|
||||
/// </summary>
|
||||
public int SeuilMs { get; set; } = 200;
|
||||
}
|
||||
@@ -21,6 +21,7 @@
|
||||
/// </summary>
|
||||
/// <param name="logger">Service de journalisation injecté pour enregistrer les événements et les erreurs.</param>
|
||||
/// <param name="configuration">Service d'injection de configuration pour accéder aux paramètres de l'application.</param>
|
||||
/// <param name="titreRepository"></param>
|
||||
public AccueilController(
|
||||
ILogger<AccueilController> logger,
|
||||
IConfiguration configuration,
|
||||
|
||||
@@ -2,12 +2,14 @@ namespace Webzine.WebApplication.Controllers;
|
||||
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
/// <summary>
|
||||
/// Controller de version de l'API.
|
||||
/// </summary>
|
||||
public class ApiController : ControllerBase
|
||||
{
|
||||
private readonly ILogger<ApiController> logger;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ApiController"/> class.
|
||||
/// Initialise une nouvelle instance de la classe <see cref="ApiController"/>.
|
||||
/// </summary>
|
||||
/// <param name="logger">Service de journalisation injecté pour enregistrer les événements et les erreurs.</param>
|
||||
@@ -21,7 +23,6 @@ public class ApiController : ControllerBase
|
||||
/// Endpoint de test pour vérifier que l'API fonctionne correctement. Retourne un objet JSON contenant le nom et la version de l'application.
|
||||
/// </summary>
|
||||
/// <returns>Un objet JSON avec les propriétés "nom" et "version".</returns>
|
||||
[HttpGet]
|
||||
public IActionResult Version()
|
||||
{
|
||||
this.logger.LogInformation("Get Version was called");
|
||||
|
||||
@@ -5,6 +5,9 @@
|
||||
using Webzine.Repository.Contracts;
|
||||
using Webzine.WebApplication.ViewModels.Artiste;
|
||||
|
||||
/// <summary>
|
||||
/// Contrôleur pour la gestion des artistes dans l'administration du webzine. Ce contrôleur gère les opérations de création, modification, suppression et affichage des artistes dans l'interface d'administration du webzine. Chaque action du contrôleur prépare un ViewModel spécifique pour la vue correspondante, permettant ainsi une séparation claire entre la logique métier et la présentation des données.
|
||||
/// </summary>
|
||||
public class ArtisteController : Controller
|
||||
{
|
||||
// Injection du logger via le constructeur
|
||||
@@ -12,10 +15,10 @@
|
||||
private readonly IArtisteRepository artisteRepository;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ArtisteController"/> class.
|
||||
/// Initialise une nouvelle instance du <see cref="ArtisteController"/>. avec un service de journalisation injecté.
|
||||
/// </summary>
|
||||
/// <param name="logger">Service de journalisation injecté pour enregistrer les événements et les erreurs.</param>
|
||||
/// <param name="artisteRepository">Repository pour accéder aux données des artistes, injecté pour permettre les opérations de création, modification, suppression et affichage des artistes.</param>
|
||||
public ArtisteController(
|
||||
ILogger<ArtisteController> logger,
|
||||
IArtisteRepository artisteRepository)
|
||||
@@ -26,7 +29,7 @@
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Prend en paramètre le nom de l'artiste (ex: "fatal-bazooka"), utilise la factory pour trouver l'artiste correspondant, et affiche sa page dédiée.
|
||||
/// Affiche la liste des artistes.
|
||||
/// </summary>
|
||||
/// <param name="nom">Le nom de l'artiste à rechercher, formaté en kebab-case (ex: "fatal-bazooka").</param>
|
||||
/// <returns>La vue de l'artiste avec son ViewModel, ou une redirection vers l'accueil si le nom est vide, ou une erreur 404 si l'artiste n'est pas trouvé.</returns>
|
||||
@@ -40,14 +43,12 @@
|
||||
return this.RedirectToAction("Index", "Accueil");
|
||||
}
|
||||
|
||||
// On transforme "fatal-bazooka" en "Fatal Bazooka" pour la factory
|
||||
// On transforme "fatal-bazooka" en "Fatal Bazooka"
|
||||
string nomPropre = System.Globalization.CultureInfo.CurrentCulture.TextInfo
|
||||
.ToTitleCase(nom.Replace("-", " "));
|
||||
|
||||
// On appelle la factory pour obtenir l'artiste unique
|
||||
var artiste = this.artisteRepository.FindByName(nomPropre);
|
||||
|
||||
// Check if artiste was found
|
||||
if (artiste == null)
|
||||
{
|
||||
this.logger.LogWarning("Artiste non trouvé avec le nom : {NomArtiste}", nomPropre);
|
||||
|
||||
@@ -9,12 +9,21 @@ namespace Webzine.WebApplication.Controllers
|
||||
using Webzine.Repository.Contracts;
|
||||
using Webzine.WebApplication.ViewModels.Recherche;
|
||||
|
||||
/// <summary>
|
||||
/// Controller de la page de recherche d'artistes et de titres.
|
||||
/// </summary>
|
||||
public class RechercheController : Controller
|
||||
{
|
||||
private readonly ILogger<RechercheController> logger;
|
||||
private readonly ITitreRepository titreRepository;
|
||||
private readonly IArtisteRepository artisteRepository;
|
||||
|
||||
/// <summary>
|
||||
/// Constructeur du controller de la page de recherche d'artistes et de titres.
|
||||
/// </summary>
|
||||
/// <param name="logger">Le logger pour enregistrer les événements.</param>
|
||||
/// <param name="titreRepository">Le repository pour gérer les opérations sur les titres.</param>
|
||||
/// <param name="artisteRepository">Le repository pour gérer les opérations sur les artistes.</param>
|
||||
public RechercheController(
|
||||
ILogger<RechercheController> logger,
|
||||
ITitreRepository titreRepository,
|
||||
|
||||
@@ -51,6 +51,8 @@ namespace Webzine.WebApplication.Controllers
|
||||
return this.RedirectToAction("Index");
|
||||
}
|
||||
|
||||
this.titreRepository.IncrementNbLectures(titre);
|
||||
|
||||
var vm = new TitreDetail
|
||||
{
|
||||
Details = new TitreContent
|
||||
@@ -110,12 +112,13 @@ namespace Webzine.WebApplication.Controllers
|
||||
if (titre == null)
|
||||
{
|
||||
this.logger.LogWarning("Impossible d'ajouter un like. Titre ID {Id} introuvable.", model.IdTitre);
|
||||
return this.RedirectToAction("Index");
|
||||
}
|
||||
else
|
||||
{
|
||||
this.titreRepository.IncrementNbLikes(titre);
|
||||
}
|
||||
|
||||
titre.NbLikes++;
|
||||
|
||||
return this.RedirectToAction("Details", new { id = model.IdTitre });
|
||||
return this.RedirectToAction("Index", new { id = model.IdTitre });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -126,12 +129,6 @@ namespace Webzine.WebApplication.Controllers
|
||||
[HttpPost]
|
||||
public IActionResult Comment(TitreComment model)
|
||||
{
|
||||
if (!this.ModelState.IsValid)
|
||||
{
|
||||
this.logger.LogWarning("Echec de validation du modele de commentaire pour le titre ID {Id}.", model.IdTitre);
|
||||
return this.RedirectToAction("Details", new { id = model.IdTitre });
|
||||
}
|
||||
|
||||
var titre = this.titreRepository.Find(model.IdTitre);
|
||||
|
||||
if (titre == null)
|
||||
@@ -152,7 +149,7 @@ namespace Webzine.WebApplication.Controllers
|
||||
|
||||
this.logger.LogInformation("Commentaire ajoute avec succes au titre ID {Id}.", model.IdTitre);
|
||||
|
||||
return this.RedirectToAction("Details", new { id = model.IdTitre });
|
||||
return this.RedirectToAction("Index", new { id = model.IdTitre });
|
||||
}
|
||||
|
||||
private static TitreStyleItem MapTitreItem(Titre titre)
|
||||
|
||||
42
Webzine.WebApplication/Filters/GlobalExceptionFilter.cs
Normal file
42
Webzine.WebApplication/Filters/GlobalExceptionFilter.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
namespace Webzine.WebApplication.Filters;
|
||||
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
|
||||
/// <summary>
|
||||
/// Filtre d'exception global qui intercepte toute exception non gérée et la journalise automatiquement.
|
||||
/// </summary>
|
||||
public class GlobalExceptionFilter : IExceptionFilter
|
||||
{
|
||||
private readonly ILogger<GlobalExceptionFilter> logger;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="GlobalExceptionFilter"/> class.
|
||||
/// </summary>
|
||||
/// <param name="logger">Service de journalisation injecté.</param>
|
||||
public GlobalExceptionFilter(ILogger<GlobalExceptionFilter> logger)
|
||||
{
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void OnException(ExceptionContext context)
|
||||
{
|
||||
this.logger.LogError(
|
||||
context.Exception,
|
||||
"Erreur non gérée dans {Action} : {Message}",
|
||||
context.ActionDescriptor.DisplayName,
|
||||
context.Exception.Message);
|
||||
|
||||
context.Result = new ObjectResult(new
|
||||
{
|
||||
erreur = "Une erreur inattendue est survenue.",
|
||||
detail = context.Exception.Message,
|
||||
})
|
||||
{
|
||||
StatusCode = StatusCodes.Status500InternalServerError,
|
||||
};
|
||||
|
||||
context.ExceptionHandled = true;
|
||||
}
|
||||
}
|
||||
73
Webzine.WebApplication/Filters/ValidationActionFilter.cs
Normal file
73
Webzine.WebApplication/Filters/ValidationActionFilter.cs
Normal file
@@ -0,0 +1,73 @@
|
||||
namespace Webzine.WebApplication.Filters;
|
||||
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
|
||||
/// <summary>
|
||||
/// Filtre d'action qui valide automatiquement le ModelState avant l'exécution du contrôleur.
|
||||
/// Mesure également le temps d'exécution de chaque action (niveau Trace).
|
||||
/// </summary>
|
||||
public class ValidationActionFilter : IActionFilter
|
||||
{
|
||||
private readonly ILogger<ValidationActionFilter> logger;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ValidationActionFilter"/> class.
|
||||
/// </summary>
|
||||
/// <param name="logger">Service de journalisation injecté.</param>
|
||||
public ValidationActionFilter(ILogger<ValidationActionFilter> logger)
|
||||
{
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void OnActionExecuting(ActionExecutingContext context)
|
||||
{
|
||||
if (!context.ModelState.IsValid)
|
||||
{
|
||||
var erreurs = context.ModelState
|
||||
.Where(e => e.Value?.Errors.Count > 0)
|
||||
.Select(e => $"{e.Key}: {string.Join(", ", e.Value!.Errors.Select(err => err.ErrorMessage))}")
|
||||
.ToList();
|
||||
|
||||
this.logger.LogWarning(
|
||||
"Validation échouée pour {Action} : {Erreurs}",
|
||||
context.ActionDescriptor.DisplayName,
|
||||
string.Join(" | ", erreurs));
|
||||
|
||||
string actionName = context.RouteData.Values["action"]?.ToString() ?? string.Empty;
|
||||
|
||||
// cas spécial: titre details
|
||||
if (actionName.Equals("Index", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
context.Result = new RedirectResult("/");
|
||||
return;
|
||||
}
|
||||
|
||||
// Récupère le modèle soumis (premier argument de l'action, s'il existe)
|
||||
object? model = context.ActionArguments.Values.FirstOrDefault();
|
||||
|
||||
if (context.Controller is Controller controller)
|
||||
{
|
||||
context.Result = new ViewResult
|
||||
{
|
||||
ViewName = actionName,
|
||||
ViewData = new Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary(
|
||||
controller.ViewData)
|
||||
{
|
||||
Model = model,
|
||||
},
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
context.Result = new BadRequestObjectResult(context.ModelState);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void OnActionExecuted(ActionExecutedContext context)
|
||||
{
|
||||
}
|
||||
}
|
||||
146
Webzine.WebApplication/Interceptors/EfSlowQueryInterceptor.cs
Normal file
146
Webzine.WebApplication/Interceptors/EfSlowQueryInterceptor.cs
Normal file
@@ -0,0 +1,146 @@
|
||||
namespace Webzine.WebApplication.Interceptors;
|
||||
|
||||
using System.Data.Common;
|
||||
using System.Diagnostics;
|
||||
|
||||
using Configuration;
|
||||
|
||||
using Microsoft.EntityFrameworkCore.Diagnostics;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
/// <summary>
|
||||
/// Intercepteur EF Core qui journalise uniquement les commandes SQL dépassant le seuil configuré.
|
||||
/// Remonte la pile d'appels pour identifier la méthode repository (<c>Webzine.Repository.*</c>) à l'origine de la requête.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// <b>Références :</b>
|
||||
/// <list type="bullet">
|
||||
/// <item>
|
||||
/// EF Core interceptors (doc officielle) :
|
||||
/// <see href="https://learn.microsoft.com/en-us/ef/core/logging-events-diagnostics/interceptors"/>
|
||||
/// </item>
|
||||
/// <item>
|
||||
/// <see cref="DbCommandInterceptor"/> API :
|
||||
/// <see href="https://learn.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.diagnostics.dbcommandinterceptor"/>
|
||||
/// </item>
|
||||
/// <item>
|
||||
/// Exemple de slow-query interceptor (SO) :
|
||||
/// <see href="https://medium.com/@sudipdevdev/how-to-detect-and-log-slow-queries-in-entity-framework-core-e2ab71024849"/>
|
||||
/// </item>
|
||||
/// <item>
|
||||
/// <see cref="System.Diagnostics.StackTrace"/> pour remonter l'appelant :
|
||||
/// <see href="https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.stacktrace"/>
|
||||
/// </item>
|
||||
/// <item>
|
||||
/// Enregistrement via <c>AddInterceptors</c> :
|
||||
/// <see href="https://learn.microsoft.com/en-us/ef/core/logging-events-diagnostics/interceptors#registering-interceptors"/>
|
||||
/// </item>
|
||||
/// </list>
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public class EfSlowQueryInterceptor : DbCommandInterceptor
|
||||
{
|
||||
private readonly ILogger<EfSlowQueryInterceptor> logger;
|
||||
private readonly int seuilMs;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="EfSlowQueryInterceptor"/> class.
|
||||
/// </summary>
|
||||
/// <param name="logger">Le service de journalisation injecté pour suivre les opérations de l'intercepteur.</param>
|
||||
/// <param name="options">Les options de performance EF injectées pour récupérer le seuil de lenteur configuré.</param>
|
||||
public EfSlowQueryInterceptor(ILogger<EfSlowQueryInterceptor> logger, IOptions<EfPerformanceOptions> options)
|
||||
{
|
||||
this.logger = logger;
|
||||
this.seuilMs = options.Value.SeuilMs;
|
||||
|
||||
this.logger.LogDebug("[EfSlowQueryInterceptor] Constructeur appelé — seuil : {SeuilMs} ms.", this.seuilMs);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override DbDataReader ReaderExecuted(DbCommand command, CommandExecutedEventData eventData, DbDataReader result)
|
||||
{
|
||||
this.JournaliserSiLent(eventData.Duration);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override ValueTask<DbDataReader> ReaderExecutedAsync(DbCommand command, CommandExecutedEventData eventData, DbDataReader result, CancellationToken cancellationToken = default)
|
||||
{
|
||||
this.JournaliserSiLent(eventData.Duration);
|
||||
return ValueTask.FromResult(result);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override int NonQueryExecuted(DbCommand command, CommandExecutedEventData eventData, int result)
|
||||
{
|
||||
this.JournaliserSiLent(eventData.Duration);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override ValueTask<int> NonQueryExecutedAsync(DbCommand command, CommandExecutedEventData eventData, int result, CancellationToken cancellationToken = default)
|
||||
{
|
||||
this.JournaliserSiLent(eventData.Duration);
|
||||
return ValueTask.FromResult(result);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override object? ScalarExecuted(DbCommand command, CommandExecutedEventData eventData, object? result)
|
||||
{
|
||||
this.JournaliserSiLent(eventData.Duration);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override ValueTask<object?> ScalarExecutedAsync(DbCommand command, CommandExecutedEventData eventData, object? result, CancellationToken cancellationToken = default)
|
||||
{
|
||||
this.JournaliserSiLent(eventData.Duration);
|
||||
return ValueTask.FromResult(result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remonte la pile d'appels pour trouver la première méthode dans <c>Webzine.Repository</c>.
|
||||
/// Toutes les requêtes EF Core du projet transitent par ce namespace, ce qui garantit
|
||||
/// un résultat pertinent sans parcourir l'intégralité de la stack.
|
||||
/// </summary>
|
||||
/// <returns>Chaîne <c>Classe.Méthode</c> ou <c>"inconnu"</c> si rien trouvé.</returns>
|
||||
/// <remarks>
|
||||
/// <see cref="StackTrace"/> est instancié uniquement quand le seuil est dépassé,
|
||||
/// ce qui évite tout impact sur le chemin nominal.
|
||||
/// Ref : <see href="https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.stacktrace"/>.
|
||||
/// </remarks>
|
||||
private static string TrouverAppelantRepository()
|
||||
{
|
||||
// skipFrames: 1 pour sauter TrouverAppelantRepository elle-même
|
||||
// fNeedFileInfo: false — on ne veut pas les numéros de ligne (coût supplémentaire inutile)
|
||||
var frames = new StackTrace(skipFrames: 1, fNeedFileInfo: false).GetFrames();
|
||||
|
||||
foreach (var frame in frames)
|
||||
{
|
||||
var methode = frame.GetMethod();
|
||||
if (methode?.DeclaringType?.Namespace?.StartsWith("Webzine.Repository", StringComparison.Ordinal) == true)
|
||||
{
|
||||
return $"{methode.DeclaringType.Name}.{methode.Name}";
|
||||
}
|
||||
}
|
||||
|
||||
return "inconnu";
|
||||
}
|
||||
|
||||
private void JournaliserSiLent(TimeSpan duree)
|
||||
{
|
||||
if (duree.TotalMilliseconds > this.seuilMs)
|
||||
{
|
||||
var appelant = TrouverAppelantRepository();
|
||||
|
||||
this.logger.LogWarning(
|
||||
"[EfSlowQueryInterceptor] Opération EF Core lente détectée — durée réelle : {DureeMs} ms — seuil : {SeuilMs} ms — dépassement : +{Depassement} ms.{NewLine}Appelant : {Appelant}",
|
||||
duree.TotalMilliseconds.ToString("F2"),
|
||||
this.seuilMs,
|
||||
(duree.TotalMilliseconds - this.seuilMs).ToString("F2"),
|
||||
Environment.NewLine,
|
||||
appelant);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
namespace Webzine.WebApplication.Middlewares
|
||||
{
|
||||
using System.Diagnostics;
|
||||
|
||||
public class LogTempsExecutionMiddleware
|
||||
{
|
||||
/// <summary>
|
||||
/// log à chaque requete http.
|
||||
/// </summary>
|
||||
// _next représente le maillon suivant dans la chaîne (le prochain middleware ou le contrôleur)
|
||||
private readonly RequestDelegate next;
|
||||
private readonly ILogger<LogTempsExecutionMiddleware> logger;
|
||||
|
||||
// Le constructeur récupère "_next" et le Logger
|
||||
public LogTempsExecutionMiddleware(RequestDelegate next, ILogger<LogTempsExecutionMiddleware> logger)
|
||||
{
|
||||
this.next = next;
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
// méthode appelée à chaque requête HTTP
|
||||
|
||||
/// <summary>
|
||||
/// Middleware chargé de journaliser le cycle de vie d'une requête HTTP (entrée, exécution, sortie et temps de réponse).
|
||||
/// </summary>
|
||||
/// <param name="context">Le contexte HTTP encapsulant toutes les informations de la requête et de la réponse.</param>
|
||||
/// <returns>Une tâche (<see cref="Task"/>) représentant l'opération asynchrone.</returns>
|
||||
public async Task InvokeAsync(HttpContext context)
|
||||
{
|
||||
// (Avant le contrôleur)
|
||||
var chronometre = Stopwatch.StartNew(); // lance le chrono
|
||||
|
||||
// --- IN ---
|
||||
var methode = context.Request.Method;
|
||||
var endpoint = context.Request.Path;
|
||||
var traceId = context.TraceIdentifier; // Identifiant unique généré par .NET
|
||||
|
||||
this.logger.LogInformation("[IN] TraceId: {traceId} | Méthode: {methode} | Endpoint: {endpoint}", traceId, methode, endpoint);
|
||||
|
||||
await this.next(context);
|
||||
|
||||
// (Après le contrôleur)
|
||||
chronometre.Stop(); // arrête le chrono
|
||||
var tempsEcoule = chronometre.ElapsedMilliseconds;
|
||||
|
||||
var httpCode = context.Response.StatusCode; // exemple: 200, 404, 500
|
||||
|
||||
// --- OUT ---
|
||||
if (httpCode >= 500)
|
||||
{
|
||||
this.logger.LogError("[OUT] TraceId: {traceId} | HTTP {httpCode} | Temps: {tempsEcoule} ms | Endpoint: {endpoint}", traceId, httpCode, tempsEcoule, endpoint);
|
||||
}
|
||||
else if (httpCode >= 400)
|
||||
{
|
||||
this.logger.LogWarning("[OUT] TraceId: {traceId} | HTTP {httpCode} | Temps: {tempsEcoule} ms | Endpoint: {endpoint}", traceId, httpCode, tempsEcoule, endpoint);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.logger.LogInformation("[OUT] TraceId: {traceId} | HTTP {httpCode} | Temps: {tempsEcoule} ms | Endpoint: {endpoint}", traceId, httpCode, tempsEcoule, endpoint);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,8 @@ using Webzine.Repository;
|
||||
using Webzine.Repository.Contracts;
|
||||
using Webzine.WebApplication.Configuration;
|
||||
using Webzine.WebApplication.Extensions;
|
||||
using Webzine.WebApplication.Filters;
|
||||
using Webzine.WebApplication.Interceptors;
|
||||
|
||||
// Initiation du logger NLog pour la classe courante afin de pouvoir l'utiliser pour logger des messages d'information, d'erreur, etc avant la construction de l'application.
|
||||
var logger = LogManager.Setup().LoadConfigurationFromAppSettings().GetCurrentClassLogger();
|
||||
@@ -26,7 +28,11 @@ try
|
||||
|
||||
// Ajoute les services necessaires pour permettre l'utilisation des
|
||||
// controllers avec des vues.
|
||||
builder.Services.AddControllersWithViews()
|
||||
builder.Services.AddControllersWithViews(options =>
|
||||
{
|
||||
options.Filters.Add<ValidationActionFilter>();
|
||||
options.Filters.Add<GlobalExceptionFilter>();
|
||||
})
|
||||
|
||||
// Ajoute la compilation des vues lors de l'execution de l'application.
|
||||
// Cela nous evite de recompiler l'application a chaque modification de vue.
|
||||
@@ -37,6 +43,12 @@ try
|
||||
builder.Logging.ClearProviders();
|
||||
builder.Host.UseNLog();
|
||||
|
||||
builder.Services.Configure<EfPerformanceOptions>(options =>
|
||||
{
|
||||
options.SeuilMs = builder.Configuration.GetValue<int>("EfPerformance:SeuilMs");
|
||||
});
|
||||
builder.Services.AddSingleton<EfSlowQueryInterceptor>();
|
||||
|
||||
// En fonction de la configuration, utilise soit les repositories basés sur une base de données, soit les repositories basés sur des listes locales.
|
||||
var repositoryType = builder.Configuration.GetValue<RepositoryType>("Repository");
|
||||
var seederType = builder.Configuration.GetValue<SeederType>("Seeder");
|
||||
@@ -45,13 +57,21 @@ try
|
||||
{
|
||||
if (builder.Environment.IsProduction())
|
||||
{
|
||||
builder.Services.AddDbContext<WebzineDbContext>(options =>
|
||||
options.UseNpgsql(builder.Configuration.GetConnectionString("PostGreSQLConnection")));
|
||||
builder.Services.AddDbContext<WebzineDbContext>((serviceProvider, options) =>
|
||||
{
|
||||
options
|
||||
.UseNpgsql(builder.Configuration.GetConnectionString("PostGreSQLConnection"))
|
||||
.AddInterceptors(serviceProvider.GetRequiredService<EfSlowQueryInterceptor>());
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
builder.Services.AddDbContext<WebzineDbContext>(options =>
|
||||
options.UseSqlite(builder.Configuration.GetConnectionString("SqliteConnection")));
|
||||
builder.Services.AddDbContext<WebzineDbContext>((serviceProvider, options) =>
|
||||
{
|
||||
options
|
||||
.UseSqlite(builder.Configuration.GetConnectionString("SqliteConnection"))
|
||||
.AddInterceptors(serviceProvider.GetRequiredService<EfSlowQueryInterceptor>());
|
||||
});
|
||||
}
|
||||
|
||||
builder.Services.AddScoped<DbEntityRepository>();
|
||||
@@ -70,6 +90,10 @@ try
|
||||
}
|
||||
|
||||
builder.Services.AddScoped<IDashboardService, DashboardService>();
|
||||
builder.Services.AddScoped<ITitreAdminService, TitreAdminService>();
|
||||
|
||||
builder.Services.AddScoped<ValidationActionFilter>();
|
||||
builder.Services.AddScoped<GlobalExceptionFilter>();
|
||||
|
||||
// https://learn.microsoft.com/fr-fr/aspnet/core/performance/response-compression?view=aspnetcore-10.0#configuration
|
||||
// Ajoute le service de compression des réponses HTTP pour réduire la taille des données envoyées au client et améliorer les performances de l'application.
|
||||
@@ -77,6 +101,8 @@ try
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
app.UseMiddleware<Webzine.WebApplication.Middlewares.LogTempsExecutionMiddleware>();
|
||||
|
||||
if (repositoryType == RepositoryType.Db)
|
||||
{
|
||||
using (var scope = app.Services.CreateScope())
|
||||
|
||||
@@ -60,13 +60,14 @@
|
||||
<div class="d-flex gap-2">
|
||||
|
||||
<form asp-action="Like" method="post">
|
||||
<input type="hidden" name="IdTitre" value="@Model.Details.IdTitre" />
|
||||
<input type="hidden" name="IdTitre" value="@Model.Details.IdTitre"/>
|
||||
<button type="submit" class="btn btn-outline-primary btn-sm">
|
||||
<i class="fa fa-thumbs-up me-1"></i> Like
|
||||
</button>
|
||||
</form>
|
||||
|
||||
<a asp-area="Administration" asp-controller="Titre" asp-action="Edit" asp-route-id="@Model.Details.IdTitre" class="btn text-primary btn-sm">
|
||||
<a asp-area="Administration" asp-controller="Titre" asp-action="Edit"
|
||||
asp-route-id="@Model.Details.IdTitre" class="btn text-primary btn-sm">
|
||||
<i class="fa fa-pen-to-square me-1"></i> Editer
|
||||
</a>
|
||||
|
||||
@@ -88,7 +89,7 @@
|
||||
class="img-fluid rounded shadow"
|
||||
alt="Jaquette"
|
||||
loading="lazy"
|
||||
fetchpriority="high" />
|
||||
fetchpriority="high"/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -121,7 +122,7 @@
|
||||
<input name="Auteur"
|
||||
class="form-control input-full"
|
||||
placeholder="Votre nom"
|
||||
required />
|
||||
required/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -131,10 +132,10 @@
|
||||
</label>
|
||||
<div class="col">
|
||||
<textarea name="Contenu"
|
||||
rows="3"
|
||||
class="form-control input-full"
|
||||
placeholder="Votre commentaire..."
|
||||
required></textarea>
|
||||
rows="3"
|
||||
class="form-control input-full"
|
||||
placeholder="Votre commentaire..."
|
||||
required></textarea>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -156,7 +157,7 @@
|
||||
|
||||
<h4 class="mb-4">Commentaires</h4>
|
||||
|
||||
@if (Model.Details.Commentaires != null && Model.Details.Commentaires.Any())
|
||||
@if (Model.Details.Commentaires.Any())
|
||||
{
|
||||
foreach (var comment in Model.Details.Commentaires.OrderByDescending(c => c.DateCreation))
|
||||
{
|
||||
@@ -169,7 +170,7 @@
|
||||
width="50"
|
||||
height="50"
|
||||
class="rounded-circle me-3 shadow-sm"
|
||||
alt="avatar" />
|
||||
alt="avatar"/>
|
||||
|
||||
<div>
|
||||
<strong>@comment.Auteur</strong>,
|
||||
|
||||
@@ -26,7 +26,7 @@
|
||||
<div class="d-flex align-items-start my-3">
|
||||
|
||||
<!-- Image -->
|
||||
<a asp-action="Details"
|
||||
<a asp-action="Index"
|
||||
asp-route-id="@titre.IdTitre"
|
||||
class="me-3 text-black">
|
||||
<img src="@titre.UrlJaquette" alt="@titre.Libelle" width="70px" height="70px" class="object-fit-cover" loading="lazy"/>
|
||||
@@ -41,7 +41,7 @@
|
||||
@titre.ArtisteNom
|
||||
</a>
|
||||
-
|
||||
<a asp-action="Details"
|
||||
<a asp-action="Index"
|
||||
asp-route-id="@titre.IdTitre">
|
||||
@titre.Libelle
|
||||
</a>
|
||||
|
||||
@@ -13,5 +13,8 @@
|
||||
"SqliteConnection": "Data Source=Data/webzine.sqlite",
|
||||
"PostGreSQLConnection": "Host=localhost;Port=5432;Username=admin;Password=admin123;Database=webzine_db"
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
"AllowedHosts": "*",
|
||||
"EfPerformance": {
|
||||
"SeuilMs": 10
|
||||
}
|
||||
}
|
||||
@@ -28,7 +28,7 @@
|
||||
|
||||
<rules>
|
||||
<!-- Vos logs d'application en Debug+ -->
|
||||
<logger name="Webzine.WebApplication.*" minlevel="Debug" writeTo="allfile,ownfile-web,console" />
|
||||
<logger name="Webzine.WebApplication.*" minlevel="Info" writeTo="allfile,ownfile-web,console" />
|
||||
|
||||
<!-- Logs Microsoft en Warning+ sauf Hosting.Lifetime -->
|
||||
<logger name="Microsoft.*" minlevel="Warn" writeTo="allfile" final="true" />
|
||||
|
||||
@@ -221,7 +221,4 @@ cat >> "$RAPPORT_FICHIER" <<EOF
|
||||
Total : $TOTAL
|
||||
Réussis : $REUSSIS
|
||||
Échecs : $ECHECS
|
||||
EOF
|
||||
|
||||
# Code de sortie non nul si des échecs ont été détectés — permet à la CI de bloquer
|
||||
exit "$ECHECS"
|
||||
EOF
|
||||
Reference in New Issue
Block a user