diff --git a/Webzine.WebApplication/Controllers/TitreController.cs b/Webzine.WebApplication/Controllers/TitreController.cs index fbab2ae..07caf80 100644 --- a/Webzine.WebApplication/Controllers/TitreController.cs +++ b/Webzine.WebApplication/Controllers/TitreController.cs @@ -20,16 +20,23 @@ namespace Webzine.WebApplication.Controllers private readonly ILogger logger; private readonly ITitreRepository titreRepository; + // Pour les commentaires. + private readonly ICommentaireRepository commentaireRepository; + /// /// Initializes a new instance of the class. /// Initialise une nouvelle instance de la classe . /// /// Service de journalisation injecte. /// Repository des titres injecte. - public TitreController(ILogger logger, ITitreRepository titreRepository) + public TitreController( + ILogger logger, + ITitreRepository titreRepository, + ICommentaireRepository commentaireRepository) { this.logger = logger; this.titreRepository = titreRepository; + this.commentaireRepository = commentaireRepository; this.logger.LogInformation("Initialisation du controleur TitreController."); } @@ -101,19 +108,19 @@ namespace Webzine.WebApplication.Controllers [HttpPost] public IActionResult Comment([Bind(Prefix = "CommentForm")] TitreComment model) { + var titreToUpdate = this.titreRepository.Find(model.IdTitre); + if (!this.ModelState.IsValid) { - var titre = this.titreRepository.Find(model.IdTitre); - if (titre == null) + if (titreToUpdate == 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)); + return this.View("Index", this.BuildTitreDetailViewModel(titreToUpdate, model)); } - var titreToUpdate = this.titreRepository.Find(model.IdTitre); if (titreToUpdate != null) { var commentaire = new Commentaire @@ -124,8 +131,7 @@ namespace Webzine.WebApplication.Controllers IdTitre = model.IdTitre, }; - titreToUpdate.Commentaires.Add(commentaire); - this.titreRepository.Update(titreToUpdate); + this.commentaireRepository.Add(commentaire); } return this.RedirectToAction("Index", new { id = model.IdTitre });