#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>
/// Ajoute un like a un titre.
/// </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>
[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 });
}
/// <summary>
@@ -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 });
}
/// <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)
{
return new TitreStyleItem
@@ -166,10 +159,10 @@ namespace Webzine.WebApplication.Controllers
}
/// <summary>
///
/// Construit une URL d'intégration Spotify à partir de l'URL d'écoute d'un titre.
/// </summary>
/// <param name="urlEcoute"></param>
/// <returns></returns>
/// <param name="urlEcoute">L'URL d'écoute du titre.</param>
/// <returns>L'URL d'intégration Spotify ou null si l'URL n'est pas valide.</returns>
private static string? BuildSpotifyEmbedUrl(string? urlEcoute)
{
if (string.IsNullOrWhiteSpace(urlEcoute))

View File

@@ -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<string, string>
{
@@ -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?}");

View File

@@ -150,11 +150,13 @@ try
var commentaires = new List<Commentaire>();
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);

View File

@@ -59,12 +59,11 @@
<!-- ACTION BUTTONS -->
<div class="d-flex gap-2">
<form asp-action="Like" method="post">
<input type="hidden" name="IdTitre" value="@Model.Details.IdTitre"/>
<button type="submit" class="btn btn-outline-primary btn-sm">
<i class="fa fa-thumbs-up me-1"></i> Like
</button>
</form>
<form asp-action="Like" asp-controller="Titre" asp-route-id="@Model.Details.IdTitre" method="post">
<button type="submit" class="btn btn-outline-primary btn-sm">
<i class="fa fa-thumbs-up me-1"></i> Like
</button>
</form>
<a asp-area="Administration" asp-controller="Titre" asp-action="Edit"
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>
<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"/>
<div class="row mb-3 align-items-center">