#183 Fix de la suppression des commentaires en local en modifiant l'incrémentation des id. Fix du controller titre pour liker, commenter et incrémenter le nombre de lectures.

This commit is contained in:
josephine.vetu
2026-04-06 13:33:54 +02:00
parent 3859ba80a4
commit 0e8c320478
4 changed files with 45 additions and 41 deletions

View File

@@ -101,25 +101,18 @@ namespace Webzine.WebApplication.Controllers
/// <summary> /// <summary>
/// Ajoute un like a un titre. /// Ajoute un like a un titre.
/// </summary> /// </summary>
/// <param name="model">Modele contenant l'identifiant du titre.</param> /// <param name="id">Identifiant du titre a liker.</param>
/// <returns>Redirection vers la page detail.</returns> /// <returns>Redirection vers la page detail.</returns>
[HttpPost] [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(id);
if (titre != null)
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
{ {
this.titreRepository.IncrementNbLikes(titre); this.titreRepository.IncrementNbLikes(titre);
} }
return this.RedirectToAction("Index", new { id = model.IdTitre }); return this.RedirectToAction("Index", new { id });
} }
/// <summary> /// <summary>
@@ -130,29 +123,29 @@ namespace Webzine.WebApplication.Controllers
[HttpPost] [HttpPost]
public IActionResult Comment(TitreComment model) public IActionResult Comment(TitreComment model)
{ {
var titre = this.titreRepository.Find(model.IdTitre); var titreToUpdate = this.titreRepository.Find(model.IdTitre);
if (titreToUpdate != null)
if (titre == null)
{ {
this.logger.LogWarning("Impossible d'ajouter le commentaire. Titre ID {Id} introuvable.", model.IdTitre); var commentaire = new Commentaire
return this.RedirectToAction("Index"); {
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 }); return this.RedirectToAction("Index", new { id = model.IdTitre });
} }
/// <summary>
/// Mappe une entite Titre vers un item de la liste de titres pour l'affichage dans la vue de style.
/// </summary>
/// <param name="titre">Le titre à mapper.</param>
/// <returns>L'item de la liste de titres.</returns>
private static TitreStyleItem MapTitreItem(Titre titre) private static TitreStyleItem MapTitreItem(Titre titre)
{ {
return new TitreStyleItem return new TitreStyleItem
@@ -166,10 +159,10 @@ namespace Webzine.WebApplication.Controllers
} }
/// <summary> /// <summary>
/// /// Construit une URL d'intégration Spotify à partir de l'URL d'écoute d'un titre.
/// </summary> /// </summary>
/// <param name="urlEcoute"></param> /// <param name="urlEcoute">L'URL d'écoute du titre.</param>
/// <returns></returns> /// <returns>L'URL d'intégration Spotify ou null si l'URL n'est pas valide.</returns>
private static string? BuildSpotifyEmbedUrl(string? urlEcoute) private static string? BuildSpotifyEmbedUrl(string? urlEcoute)
{ {
if (string.IsNullOrWhiteSpace(urlEcoute)) if (string.IsNullOrWhiteSpace(urlEcoute))

View File

@@ -23,6 +23,16 @@ public static class RouteConfiguration
pattern: "artiste/{nom}", pattern: "artiste/{nom}",
defaults: new { controller = "Artiste", action = "Index" }); 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 ----------- // ----------- ADMIN -----------
var adminRoutes = new Dictionary<string, string> var adminRoutes = new Dictionary<string, string>
{ {
@@ -37,7 +47,7 @@ public static class RouteConfiguration
defaults: new { area = "Administration", controller = route.Value, action = "Index" }); defaults: new { area = "Administration", controller = route.Value, action = "Index" });
} }
// --- AUTRE PROUTES --- // --- AUTRES ROUTES ---
endpoints.MapControllerRoute( endpoints.MapControllerRoute(
name: "areas", name: "areas",
pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}"); pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");

View File

@@ -150,11 +150,13 @@ try
var commentaires = new List<Commentaire>(); var commentaires = new List<Commentaire>();
var titres = SeedDataLocal.GenererListeTitre(500, artistes, styles, albums); var titres = SeedDataLocal.GenererListeTitre(500, artistes, styles, albums);
int commentaireIdStart = 1;
foreach (var titre in titres) foreach (var titre in titres)
{ {
var commentairesForTitre = SeedDataLocal.GenererListeCommentaire(titre, 0, 5); var commentairesForTitre = SeedDataLocal.GenererListeCommentaire(titre, 0, 5, commentaireIdStart);
titre.Commentaires.AddRange(commentairesForTitre); titre.Commentaires.AddRange(commentairesForTitre);
commentaires.AddRange(commentairesForTitre); commentaires.AddRange(commentairesForTitre);
commentaireIdStart += commentairesForTitre.Count;
} }
store.Artistes.AddRange(artistes); store.Artistes.AddRange(artistes);

View File

@@ -59,12 +59,11 @@
<!-- ACTION BUTTONS --> <!-- ACTION BUTTONS -->
<div class="d-flex gap-2"> <div class="d-flex gap-2">
<form asp-action="Like" method="post"> <form asp-action="Like" asp-controller="Titre" asp-route-id="@Model.Details.IdTitre" method="post">
<input type="hidden" name="IdTitre" value="@Model.Details.IdTitre"/> <button type="submit" class="btn btn-outline-primary btn-sm">
<button type="submit" class="btn btn-outline-primary btn-sm"> <i class="fa fa-thumbs-up me-1"></i> Like
<i class="fa fa-thumbs-up me-1"></i> Like </button>
</button> </form>
</form>
<a asp-area="Administration" asp-controller="Titre" asp-action="Edit" <a asp-area="Administration" asp-controller="Titre" asp-action="Edit"
asp-route-id="@Model.Details.IdTitre" class="btn text-primary btn-sm"> asp-route-id="@Model.Details.IdTitre" class="btn text-primary btn-sm">
@@ -125,7 +124,7 @@
<h4 class="mb-4">Donne ton avis sur le titre</h4> <h4 class="mb-4">Donne ton avis sur le titre</h4>
<form asp-action="Comment" method="post"> <form asp-action="Comment" asp-controller="Titre" asp-route-id="@Model.Details.IdTitre" method="post">
<input type="hidden" name="IdTitre" value="@Model.Details.IdTitre"/> <input type="hidden" name="IdTitre" value="@Model.Details.IdTitre"/>
<div class="row mb-3 align-items-center"> <div class="row mb-3 align-items-center">