#199 : Modification de l'ajout de commentaire.

This commit is contained in:
Loic Masi
2026-04-08 19:25:15 +02:00
parent b59366ddc3
commit e27e2864f4

View File

@@ -20,16 +20,23 @@ namespace Webzine.WebApplication.Controllers
private readonly ILogger<TitreController> logger; private readonly ILogger<TitreController> logger;
private readonly ITitreRepository titreRepository; private readonly ITitreRepository titreRepository;
// Pour les commentaires.
private readonly ICommentaireRepository commentaireRepository;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="TitreController"/> class. /// Initializes a new instance of the <see cref="TitreController"/> class.
/// Initialise une nouvelle instance de la classe <see cref="TitreController"/>. /// Initialise une nouvelle instance de la classe <see cref="TitreController"/>.
/// </summary> /// </summary>
/// <param name="logger">Service de journalisation injecte.</param> /// <param name="logger">Service de journalisation injecte.</param>
/// <param name="titreRepository">Repository des titres injecte.</param> /// <param name="titreRepository">Repository des titres injecte.</param>
public TitreController(ILogger<TitreController> logger, ITitreRepository titreRepository) public TitreController(
ILogger<TitreController> logger,
ITitreRepository titreRepository,
ICommentaireRepository commentaireRepository)
{ {
this.logger = logger; this.logger = logger;
this.titreRepository = titreRepository; this.titreRepository = titreRepository;
this.commentaireRepository = commentaireRepository;
this.logger.LogInformation("Initialisation du controleur TitreController."); this.logger.LogInformation("Initialisation du controleur TitreController.");
} }
@@ -101,19 +108,19 @@ namespace Webzine.WebApplication.Controllers
[HttpPost] [HttpPost]
public IActionResult Comment([Bind(Prefix = "CommentForm")] TitreComment model) public IActionResult Comment([Bind(Prefix = "CommentForm")] TitreComment model)
{ {
var titreToUpdate = this.titreRepository.Find(model.IdTitre);
if (!this.ModelState.IsValid) if (!this.ModelState.IsValid)
{ {
var titre = this.titreRepository.Find(model.IdTitre); if (titreToUpdate == null)
if (titre == null)
{ {
this.logger.LogWarning("Titre avec ID {Id} introuvable pour ajout de commentaire.", model.IdTitre); this.logger.LogWarning("Titre avec ID {Id} introuvable pour ajout de commentaire.", model.IdTitre);
return this.RedirectToAction("Index", "Accueil"); return this.RedirectToAction("Index", "Accueil");
} }
return this.View("Index", this.BuildTitreDetailViewModel(titre, model)); return this.View("Index", this.BuildTitreDetailViewModel(titreToUpdate, model));
} }
var titreToUpdate = this.titreRepository.Find(model.IdTitre);
if (titreToUpdate != null) if (titreToUpdate != null)
{ {
var commentaire = new Commentaire var commentaire = new Commentaire
@@ -124,8 +131,7 @@ namespace Webzine.WebApplication.Controllers
IdTitre = model.IdTitre, IdTitre = model.IdTitre,
}; };
titreToUpdate.Commentaires.Add(commentaire); this.commentaireRepository.Add(commentaire);
this.titreRepository.Update(titreToUpdate);
} }
return this.RedirectToAction("Index", new { id = model.IdTitre }); return this.RedirectToAction("Index", new { id = model.IdTitre });