From 0e8c320478a0579f99815f886415abd546c7c110 Mon Sep 17 00:00:00 2001 From: "josephine.vetu" Date: Mon, 6 Apr 2026 13:33:54 +0200 Subject: [PATCH 1/3] =?UTF-8?q?#183=20Fix=20de=20la=20suppression=20des=20?= =?UTF-8?q?commentaires=20en=20local=20en=20modifiant=20l'incr=C3=A9mentat?= =?UTF-8?q?ion=20des=20id.=20Fix=20du=20controller=20titre=20pour=20liker,?= =?UTF-8?q?=20commenter=20et=20incr=C3=A9menter=20le=20nombre=20de=20lectu?= =?UTF-8?q?res.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/TitreController.cs | 57 ++++++++----------- .../Extensions/RouteConfiguration.cs | 12 +++- Webzine.WebApplication/Program.cs | 4 +- .../Views/Titre/Index.cshtml | 13 ++--- 4 files changed, 45 insertions(+), 41 deletions(-) diff --git a/Webzine.WebApplication/Controllers/TitreController.cs b/Webzine.WebApplication/Controllers/TitreController.cs index 3088dc5..8f00a16 100644 --- a/Webzine.WebApplication/Controllers/TitreController.cs +++ b/Webzine.WebApplication/Controllers/TitreController.cs @@ -101,25 +101,18 @@ namespace Webzine.WebApplication.Controllers /// /// Ajoute un like a un titre. /// - /// Modele contenant l'identifiant du titre. + /// Identifiant du titre a liker. /// Redirection vers la page detail. [HttpPost] - public IActionResult Like(TitreLike model) + public IActionResult Like(int id) { - this.logger.LogInformation("Ajout d'un like pour le titre ID {Id}.", model.IdTitre); - - var titre = this.titreRepository.Find(model.IdTitre); - - if (titre == null) - { - this.logger.LogWarning("Impossible d'ajouter un like. Titre ID {Id} introuvable.", model.IdTitre); - } - else + var titre = this.titreRepository.Find(id); + if (titre != null) { this.titreRepository.IncrementNbLikes(titre); } - return this.RedirectToAction("Index", new { id = model.IdTitre }); + return this.RedirectToAction("Index", new { id }); } /// @@ -130,29 +123,29 @@ namespace Webzine.WebApplication.Controllers [HttpPost] public IActionResult Comment(TitreComment model) { - var titre = this.titreRepository.Find(model.IdTitre); - - if (titre == null) + var titreToUpdate = this.titreRepository.Find(model.IdTitre); + if (titreToUpdate != null) { - this.logger.LogWarning("Impossible d'ajouter le commentaire. Titre ID {Id} introuvable.", model.IdTitre); - return this.RedirectToAction("Index"); + var commentaire = new Commentaire + { + Auteur = model.Auteur, + Contenu = model.Contenu, + DateCreation = DateTime.Now, + IdTitre = model.IdTitre, + }; + + titreToUpdate.Commentaires.Add(commentaire); + this.titreRepository.Update(titreToUpdate); } - var commentaire = new Commentaire - { - Auteur = model.Auteur, - Contenu = model.Contenu, - DateCreation = DateTime.Now, - IdTitre = model.IdTitre, - }; - - titre.Commentaires.Add(commentaire); - - this.logger.LogInformation("Commentaire ajoute avec succes au titre ID {Id}.", model.IdTitre); - return this.RedirectToAction("Index", new { id = model.IdTitre }); } + /// + /// Mappe une entite Titre vers un item de la liste de titres pour l'affichage dans la vue de style. + /// + /// Le titre à mapper. + /// L'item de la liste de titres. private static TitreStyleItem MapTitreItem(Titre titre) { return new TitreStyleItem @@ -166,10 +159,10 @@ namespace Webzine.WebApplication.Controllers } /// - /// + /// Construit une URL d'intégration Spotify à partir de l'URL d'écoute d'un titre. /// - /// - /// + /// L'URL d'écoute du titre. + /// L'URL d'intégration Spotify ou null si l'URL n'est pas valide. private static string? BuildSpotifyEmbedUrl(string? urlEcoute) { if (string.IsNullOrWhiteSpace(urlEcoute)) diff --git a/Webzine.WebApplication/Extensions/RouteConfiguration.cs b/Webzine.WebApplication/Extensions/RouteConfiguration.cs index 0b9b428..c499adb 100644 --- a/Webzine.WebApplication/Extensions/RouteConfiguration.cs +++ b/Webzine.WebApplication/Extensions/RouteConfiguration.cs @@ -23,6 +23,16 @@ public static class RouteConfiguration pattern: "artiste/{nom}", defaults: new { controller = "Artiste", action = "Index" }); + endpoints.MapControllerRoute( + name: "TitreLike", + pattern: "titre/{id}/like", + defaults: new { controller = "Titre", action = "Like" }); + + endpoints.MapControllerRoute( + name: "TitreComment", + pattern: "titre/{id}/comment", + defaults: new { controller = "Titre", action = "Comment" }); + // ----------- ADMIN ----------- var adminRoutes = new Dictionary { @@ -37,7 +47,7 @@ public static class RouteConfiguration defaults: new { area = "Administration", controller = route.Value, action = "Index" }); } - // --- AUTRE PROUTES --- + // --- AUTRES ROUTES --- endpoints.MapControllerRoute( name: "areas", pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}"); diff --git a/Webzine.WebApplication/Program.cs b/Webzine.WebApplication/Program.cs index 4ef5711..1022096 100644 --- a/Webzine.WebApplication/Program.cs +++ b/Webzine.WebApplication/Program.cs @@ -150,11 +150,13 @@ try var commentaires = new List(); var titres = SeedDataLocal.GenererListeTitre(500, artistes, styles, albums); + int commentaireIdStart = 1; foreach (var titre in titres) { - var commentairesForTitre = SeedDataLocal.GenererListeCommentaire(titre, 0, 5); + var commentairesForTitre = SeedDataLocal.GenererListeCommentaire(titre, 0, 5, commentaireIdStart); titre.Commentaires.AddRange(commentairesForTitre); commentaires.AddRange(commentairesForTitre); + commentaireIdStart += commentairesForTitre.Count; } store.Artistes.AddRange(artistes); diff --git a/Webzine.WebApplication/Views/Titre/Index.cshtml b/Webzine.WebApplication/Views/Titre/Index.cshtml index 6b410bf..c5a1c6f 100644 --- a/Webzine.WebApplication/Views/Titre/Index.cshtml +++ b/Webzine.WebApplication/Views/Titre/Index.cshtml @@ -59,12 +59,11 @@
-
- - -
+
+ +
@@ -125,7 +124,7 @@

Donne ton avis sur le titre

-
+
From 425ab13617840c99093a61ff064be1b667107cd8 Mon Sep 17 00:00:00 2001 From: "josephine.vetu" Date: Mon, 6 Apr 2026 19:08:56 +0200 Subject: [PATCH 2/3] =?UTF-8?q?#199=20La=20m=C3=A9thode=20pour=20supprimer?= =?UTF-8?q?=20un=20titre=20est=20renomm=C3=A9e=20et=20prend=20un=20id=20au?= =?UTF-8?q?=20lieu=20du=20mod=C3=A8le.=20Ajout=20d'un=20try=20catch=20sur?= =?UTF-8?q?=20artiste=20controller=20pour=20debugger=20l'ajout=20d'un=20ar?= =?UTF-8?q?tiste.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Webzine.Repository/DbTitreRepository.cs | 12 ------------ .../Controllers/ArtisteController.cs | 12 ++++++++++-- .../Administration/Controllers/TitreController.cs | 14 ++++---------- .../Areas/Administration/Views/Titre/Delete.cshtml | 6 +++++- 4 files changed, 19 insertions(+), 25 deletions(-) diff --git a/Webzine.Repository/DbTitreRepository.cs b/Webzine.Repository/DbTitreRepository.cs index 918d543..4b11261 100644 --- a/Webzine.Repository/DbTitreRepository.cs +++ b/Webzine.Repository/DbTitreRepository.cs @@ -33,7 +33,6 @@ public class DbTitreRepository : ITitreRepository try { this.logger.LogInformation("Ajout d'un nouveau titre: {Libelle}", titre.Libelle); - this.logger.LogDebug("Début de l'ajout du titre en base de données"); this.context.Titres.Add(titre); this.context.SaveChanges(); @@ -57,7 +56,6 @@ public class DbTitreRepository : ITitreRepository { try { - this.logger.LogDebug("Comptage des titres en base de données"); var count = this.context.Titres.Count(); this.logger.LogDebug("Nombre total de titres: {Count}", count); return count; @@ -75,7 +73,6 @@ public class DbTitreRepository : ITitreRepository try { this.logger.LogInformation("Suppression du titre avec l'ID: {IdTitre}", titre.IdTitre); - this.logger.LogDebug("Début de la suppression du titre en base de données"); this.context.Titres.Remove(titre); this.context.SaveChanges(); @@ -100,7 +97,6 @@ public class DbTitreRepository : ITitreRepository try { this.logger.LogDebug("Recherche des titres avec offset: {Offset}, limit: {Limit}", offset, limit); - this.logger.LogDebug("Préparation de la requête avec les inclusions Artiste et Styles"); var titres = this.context.Titres .OrderByDescending(t => t.DateCreation) @@ -125,12 +121,10 @@ public class DbTitreRepository : ITitreRepository try { this.logger.LogInformation("Incrémentation du nombre de lectures pour le titre ID: {IdTitre}", titre.IdTitre); - this.logger.LogDebug("Recherche du titre en base de données"); var existingTitre = this.context.Titres.Find(titre.IdTitre); if (existingTitre != null) { - this.logger.LogDebug("Titre trouvé, incrémentation du compteur de lectures"); existingTitre.NbLectures++; this.context.SaveChanges(); this.logger.LogDebug("Nouveau nombre de lectures: {NbLectures}", existingTitre.NbLectures); @@ -158,12 +152,10 @@ public class DbTitreRepository : ITitreRepository try { this.logger.LogInformation("Incrémentation du nombre de likes pour le titre ID: {IdTitre}", titre.IdTitre); - this.logger.LogDebug("Recherche du titre en base de données"); var existingTitre = this.context.Titres.Find(titre.IdTitre); if (existingTitre != null) { - this.logger.LogDebug("Titre trouvé, incrémentation du compteur de likes"); existingTitre.NbLikes++; this.context.SaveChanges(); this.logger.LogDebug("Nouveau nombre de likes: {NbLikes}", existingTitre.NbLikes); @@ -281,9 +273,6 @@ public class DbTitreRepository : ITitreRepository { try { - this.logger.LogDebug("Récupération de tous les titres"); - this.logger.LogDebug("Préparation de la requête avec les inclusions Artiste et Styles"); - var titres = this.context.Titres .Include(t => t.Artiste) .Include(t => t.Styles) @@ -307,7 +296,6 @@ public class DbTitreRepository : ITitreRepository try { this.logger.LogInformation("Recherche des titres par style: {Libelle}", libelle); - this.logger.LogDebug("Préparation de la requête de recherche par style"); var titres = this.context.Titres .Include(t => t.Artiste) diff --git a/Webzine.WebApplication/Areas/Administration/Controllers/ArtisteController.cs b/Webzine.WebApplication/Areas/Administration/Controllers/ArtisteController.cs index c5df960..9442b48 100644 --- a/Webzine.WebApplication/Areas/Administration/Controllers/ArtisteController.cs +++ b/Webzine.WebApplication/Areas/Administration/Controllers/ArtisteController.cs @@ -81,8 +81,16 @@ public class ArtisteController : Controller Biographie = model.Biographie, }; - // Persister les données. - this.artisteRepository.Add(artiste); + try + { + this.artisteRepository.Add(artiste); + this.logger.LogInformation("Création d'un nouvel artiste: {Nom}", artiste.Nom); + } + catch (Exception ex) + { + this.logger.LogError(ex, "Erreur lors de la création de l'artiste: {Nom}", artiste.Nom); + return this.View(model); + } // Renvoyer sur la page Index. return this.RedirectToAction("Index"); diff --git a/Webzine.WebApplication/Areas/Administration/Controllers/TitreController.cs b/Webzine.WebApplication/Areas/Administration/Controllers/TitreController.cs index a42bdb4..0f19327 100644 --- a/Webzine.WebApplication/Areas/Administration/Controllers/TitreController.cs +++ b/Webzine.WebApplication/Areas/Administration/Controllers/TitreController.cs @@ -252,24 +252,18 @@ public class TitreController : Controller /// /// Méthode POST pour supprimer un titre. /// - /// Le titre à supprimer. + /// L'identifiant du titre à supprimer, utilisé pour récupérer les données du titre à partir de la liste des titres générés. /// Redirige vers la page d'index d'admin titre. [HttpPost] - public IActionResult Delete(AdminTitreDelete model) + public IActionResult DeleteTitre(int id) { - var titre = this.titreRepository.Find(model.Id); - - if (!this.ModelState.IsValid) - { - return this.View(model); - } + var titre = this.titreRepository.Find(id); if (titre != null) { this.titreRepository.Delete(titre); - return this.RedirectToAction("Index"); } - return this.View(model); + return this.RedirectToAction("Index"); } } \ No newline at end of file diff --git a/Webzine.WebApplication/Areas/Administration/Views/Titre/Delete.cshtml b/Webzine.WebApplication/Areas/Administration/Views/Titre/Delete.cshtml index 63a22e3..86f123b 100644 --- a/Webzine.WebApplication/Areas/Administration/Views/Titre/Delete.cshtml +++ b/Webzine.WebApplication/Areas/Administration/Views/Titre/Delete.cshtml @@ -1,5 +1,9 @@ @model Webzine.WebApplication.Areas.Administration.ViewModels.Titre.AdminTitreDelete +@{ + ViewData["Title"] = "Supprimer un titre"; +} +

Supprimer un titre

@@ -13,7 +17,7 @@ @Model.Artiste ?

- + From 9430c54b82d93cddea0873083d1df263d518672f Mon Sep 17 00:00:00 2001 From: "josephine.vetu" Date: Mon, 6 Apr 2026 19:40:43 +0200 Subject: [PATCH 3/3] #199 Les logs des repository sont desormais disponibles sur la console et dans grafana pour faciliter le debuggage. --- .../Administration/Controllers/ArtisteController.cs | 12 ++---------- Webzine.WebApplication/appsettings.json | 3 ++- Webzine.WebApplication/nlog.config | 1 + 3 files changed, 5 insertions(+), 11 deletions(-) diff --git a/Webzine.WebApplication/Areas/Administration/Controllers/ArtisteController.cs b/Webzine.WebApplication/Areas/Administration/Controllers/ArtisteController.cs index 9442b48..fd3d7f2 100644 --- a/Webzine.WebApplication/Areas/Administration/Controllers/ArtisteController.cs +++ b/Webzine.WebApplication/Areas/Administration/Controllers/ArtisteController.cs @@ -81,16 +81,8 @@ public class ArtisteController : Controller Biographie = model.Biographie, }; - try - { - this.artisteRepository.Add(artiste); - this.logger.LogInformation("Création d'un nouvel artiste: {Nom}", artiste.Nom); - } - catch (Exception ex) - { - this.logger.LogError(ex, "Erreur lors de la création de l'artiste: {Nom}", artiste.Nom); - return this.View(model); - } + this.artisteRepository.Add(artiste); + this.logger.LogInformation("Création d'un nouvel artiste: {Nom}", artiste.Nom); // Renvoyer sur la page Index. return this.RedirectToAction("Index"); diff --git a/Webzine.WebApplication/appsettings.json b/Webzine.WebApplication/appsettings.json index cc61318..a3604ad 100644 --- a/Webzine.WebApplication/appsettings.json +++ b/Webzine.WebApplication/appsettings.json @@ -2,7 +2,8 @@ "Logging": { "LogLevel": { "Default": "Information", - "Microsoft.AspNetCore": "Warning" + "Microsoft.AspNetCore": "Warning", + "Webzine.Repository": "Debug" } }, "Webzine": { diff --git a/Webzine.WebApplication/nlog.config b/Webzine.WebApplication/nlog.config index c2ba55e..2ff20bb 100644 --- a/Webzine.WebApplication/nlog.config +++ b/Webzine.WebApplication/nlog.config @@ -29,6 +29,7 @@ +