From 2ba9925d86b64fc3b9767215920a140509b853ee Mon Sep 17 00:00:00 2001 From: Loic Masi Date: Tue, 7 Apr 2026 10:03:34 +0200 Subject: [PATCH] #199 : Correction de bug sur l'ajout des commentaires si le ModelState n'est pas valide. --- .../Controllers/TitreController.cs | 67 ++++++++++++------- .../Filters/ValidationActionFilter.cs | 11 ++- .../Views/Titre/Index.cshtml | 19 +++--- 3 files changed, 60 insertions(+), 37 deletions(-) diff --git a/Webzine.WebApplication/Controllers/TitreController.cs b/Webzine.WebApplication/Controllers/TitreController.cs index 8f00a16..fbab2ae 100644 --- a/Webzine.WebApplication/Controllers/TitreController.cs +++ b/Webzine.WebApplication/Controllers/TitreController.cs @@ -38,6 +38,7 @@ namespace Webzine.WebApplication.Controllers /// Affiche le detail d'un titre specifique. /// /// Identifiant du titre. + /// Model de donnée pour un commentaire. /// Vue des details ou 404 si introuvable. public IActionResult Index(int id) { @@ -48,34 +49,11 @@ namespace Webzine.WebApplication.Controllers if (titre == null) { this.logger.LogWarning("Titre avec ID {Id} introuvable.", id); - return this.RedirectToAction("Index"); + return this.RedirectToAction("Index", "Accueil"); } this.titreRepository.IncrementNbLectures(titre); - - var vm = new TitreDetail - { - Details = new TitreContent - { - IdTitre = titre.IdTitre, - Libelle = titre.Libelle, - Chronique = titre.Chronique, - DateSortie = titre.DateSortie, - NbLikes = titre.NbLikes, - UrlJaquette = titre.UrlJaquette, - UrlEcoute = titre.UrlEcoute, - UrlEmbedEcoute = BuildSpotifyEmbedUrl(titre.UrlEcoute), - ArtisteNom = titre.Artiste.Nom, - Styles = titre.Styles, - Commentaires = titre.Commentaires, - }, - CommentForm = new TitreComment - { - IdTitre = titre.IdTitre, - }, - }; - - return this.View(vm); + return this.View(this.BuildTitreDetailViewModel(titre)); } /// @@ -121,8 +99,20 @@ namespace Webzine.WebApplication.Controllers /// Donnees du commentaire. /// Redirection vers la page detail. [HttpPost] - public IActionResult Comment(TitreComment model) + public IActionResult Comment([Bind(Prefix = "CommentForm")] TitreComment model) { + if (!this.ModelState.IsValid) + { + var titre = this.titreRepository.Find(model.IdTitre); + if (titre == null) + { + this.logger.LogWarning("Titre avec ID {Id} introuvable pour ajout de commentaire.", model.IdTitre); + return this.RedirectToAction("Index", "Accueil"); + } + + return this.View("Index", this.BuildTitreDetailViewModel(titre, model)); + } + var titreToUpdate = this.titreRepository.Find(model.IdTitre); if (titreToUpdate != null) { @@ -141,6 +131,31 @@ namespace Webzine.WebApplication.Controllers return this.RedirectToAction("Index", new { id = model.IdTitre }); } + private TitreDetail BuildTitreDetailViewModel(Titre titre, TitreComment? commentForm = null) + { + return new TitreDetail + { + Details = new TitreContent + { + IdTitre = titre.IdTitre, + Libelle = titre.Libelle, + Chronique = titre.Chronique, + DateSortie = titre.DateSortie, + NbLikes = titre.NbLikes, + UrlJaquette = titre.UrlJaquette, + UrlEcoute = titre.UrlEcoute, + UrlEmbedEcoute = BuildSpotifyEmbedUrl(titre.UrlEcoute), + ArtisteNom = titre.Artiste.Nom, + Styles = titre.Styles, + Commentaires = titre.Commentaires, + }, + CommentForm = commentForm ?? new TitreComment + { + IdTitre = titre.IdTitre, + }, + }; + } + /// /// Mappe une entite Titre vers un item de la liste de titres pour l'affichage dans la vue de style. /// diff --git a/Webzine.WebApplication/Filters/ValidationActionFilter.cs b/Webzine.WebApplication/Filters/ValidationActionFilter.cs index 94d0efa..f3613ec 100644 --- a/Webzine.WebApplication/Filters/ValidationActionFilter.cs +++ b/Webzine.WebApplication/Filters/ValidationActionFilter.cs @@ -23,6 +23,15 @@ public class ValidationActionFilter : IActionFilter /// public void OnActionExecuting(ActionExecutingContext context) { + string controllerName = context.RouteData.Values["controller"]?.ToString() ?? string.Empty; + string actionName = context.RouteData.Values["action"]?.ToString() ?? string.Empty; + + if (controllerName.Equals("Titre", StringComparison.OrdinalIgnoreCase) + && actionName.Equals("Comment", StringComparison.OrdinalIgnoreCase)) + { + return; + } + if (!context.ModelState.IsValid) { var erreurs = context.ModelState @@ -35,8 +44,6 @@ public class ValidationActionFilter : IActionFilter 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)) { diff --git a/Webzine.WebApplication/Views/Titre/Index.cshtml b/Webzine.WebApplication/Views/Titre/Index.cshtml index c5a1c6f..75bc6d3 100644 --- a/Webzine.WebApplication/Views/Titre/Index.cshtml +++ b/Webzine.WebApplication/Views/Titre/Index.cshtml @@ -125,30 +125,31 @@

Donne ton avis sur le titre

- + +
-
-