feat: Ajouter la fonctionnalité de suppression de commentaires avec un nouveau ViewModel
This commit is contained in:
@@ -3,13 +3,38 @@ using System.Collections.Generic;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Webzine.Entity;
|
||||
using Webzine.WebApplication.Areas.Administration.ViewModels;
|
||||
using Webzine.Entity.Fixtures;
|
||||
using Webzine.WebApplication.Areas.Administration.ViewModels.Commentaire;
|
||||
|
||||
namespace Webzine.WebApplication.Areas.Administration.Controllers
|
||||
{
|
||||
[Area("Administration")]
|
||||
public class CommentairesController : Controller
|
||||
public class CommentaireController : Controller
|
||||
{
|
||||
private readonly ILogger<CommentaireController> _logger;
|
||||
private readonly List<Commentaire> _commentaires;
|
||||
/// <summary>
|
||||
/// Initialise une nouvelle instance du <see cref="CommentaireController"/>.
|
||||
/// Les données sont générées dynamiquement via <see cref="DataFactory"/>.
|
||||
/// </summary>
|
||||
/// <param name="logger">Service de journalisation injecté.</param>
|
||||
public CommentaireController(ILogger<CommentaireController> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
|
||||
_logger.LogInformation("Initialisation du contrôleur CommentaireController.");
|
||||
|
||||
var factory = new DataFactory();
|
||||
|
||||
var _artistes = factory.GenerateArtists(10);
|
||||
var _styles = factory.GenerateStyles(10);
|
||||
var _titres = factory.GenerateTitres(30, _artistes, _styles);
|
||||
|
||||
_commentaires = factory.GenerateCommentaires(50, _titres);
|
||||
|
||||
_logger.LogInformation("Données fictives générées avec succès.");
|
||||
}
|
||||
|
||||
// GET: Administration/Commentaires
|
||||
public ActionResult Index()
|
||||
{
|
||||
@@ -53,61 +78,42 @@ namespace Webzine.WebApplication.Areas.Administration.Controllers
|
||||
}
|
||||
|
||||
|
||||
|
||||
// GET: CommentairesController/Details/5
|
||||
public ActionResult Details(int id)
|
||||
{
|
||||
return View();
|
||||
}
|
||||
|
||||
// GET: CommentairesController/Create
|
||||
public ActionResult Create()
|
||||
{
|
||||
return View();
|
||||
}
|
||||
|
||||
// POST: CommentairesController/Create
|
||||
[HttpPost]
|
||||
[ValidateAntiForgeryToken]
|
||||
public ActionResult Create(IFormCollection collection)
|
||||
{
|
||||
try
|
||||
{
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
catch
|
||||
{
|
||||
return View();
|
||||
}
|
||||
}
|
||||
|
||||
// GET: CommentairesController/Edit/5
|
||||
public ActionResult Edit(int id)
|
||||
{
|
||||
return View();
|
||||
}
|
||||
|
||||
// POST: CommentairesController/Edit/5
|
||||
[HttpPost]
|
||||
[ValidateAntiForgeryToken]
|
||||
public ActionResult Edit(int id, IFormCollection collection)
|
||||
{
|
||||
try
|
||||
{
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
catch
|
||||
{
|
||||
return View();
|
||||
}
|
||||
}
|
||||
|
||||
// GET: CommentairesController/Delete/5
|
||||
// GET: Administration/Commentaires/Delete/5
|
||||
public ActionResult Delete(int id)
|
||||
{
|
||||
return View();
|
||||
var commentaire = _commentaires
|
||||
.FirstOrDefault(c => c.IdCommentaire == id);
|
||||
|
||||
if (commentaire == null)
|
||||
return NotFound();
|
||||
|
||||
var vm = new CommentaireDeleteViewModel
|
||||
{
|
||||
IdCommentaire = commentaire.IdCommentaire,
|
||||
Auteur = commentaire.Auteur,
|
||||
Contenu = commentaire.Contenu,
|
||||
DateCreation = commentaire.DateCreation,
|
||||
TitreLibelle = commentaire.Titre?.Libelle
|
||||
};
|
||||
|
||||
return View(vm);
|
||||
}
|
||||
|
||||
// POST: CommentairesController/Delete/5
|
||||
// POST: Administration/Commentaires/Delete/5
|
||||
[HttpPost]
|
||||
[ValidateAntiForgeryToken]
|
||||
public ActionResult Delete(int id, CommentaireDeleteViewModel model)
|
||||
{
|
||||
try
|
||||
{
|
||||
return RedirectToAction();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
// Log de l'erreur
|
||||
Console.WriteLine(e);
|
||||
return View(model);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
namespace Webzine.WebApplication.Areas.Administration.ViewModels.Commentaire;
|
||||
|
||||
public class CommentaireDeleteViewModel
|
||||
{
|
||||
public int IdCommentaire { get; set; }
|
||||
|
||||
public string? Auteur { get; set; }
|
||||
|
||||
public string? Contenu { get; set; }
|
||||
|
||||
public DateTime DateCreation { get; set; }
|
||||
|
||||
public string? TitreLibelle { get; set; }
|
||||
}
|
||||
@@ -6,10 +6,7 @@
|
||||
// <copyright file="CommentaireViewModel.cs" company="Webzine">
|
||||
// Copyright (c) Webzine. All rights reserved.
|
||||
// </copyright>
|
||||
|
||||
using Webzine.Entity;
|
||||
|
||||
namespace Webzine.WebApplication.Areas.Administration.ViewModels
|
||||
namespace Webzine.WebApplication.Areas.Administration.ViewModels.Commentaire
|
||||
{
|
||||
/// <summary>
|
||||
/// ViewModel pour afficher la liste des commentaires en administration.
|
||||
@@ -19,6 +16,6 @@ namespace Webzine.WebApplication.Areas.Administration.ViewModels
|
||||
/// <summary>
|
||||
/// Obtient ou définit la liste des commentaires.
|
||||
/// </summary>
|
||||
public IEnumerable<Commentaire> Commentaires { get; set; } = new List<Commentaire>();
|
||||
public IEnumerable<Entity.Commentaire> Commentaires { get; set; } = new List<Entity.Commentaire>();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
@model Webzine.WebApplication.Areas.Administration.ViewModels.Commentaire.CommentaireDeleteViewModel
|
||||
|
||||
@{
|
||||
ViewData["Title"] = "Supprimer un commentaire";
|
||||
}
|
||||
|
||||
<div class="container mt-4">
|
||||
|
||||
<h1 class="mb-3">Supprimer un commentaire</h1>
|
||||
<hr />
|
||||
|
||||
<p class="mb-4">
|
||||
Êtes-vous sûr de vouloir supprimer le commentaire suivant ?
|
||||
</p>
|
||||
|
||||
<div class="mb-4">
|
||||
<h4>@Model.Contenu</h4>
|
||||
|
||||
<div class="text-muted">
|
||||
— <strong>@Model.Auteur</strong>
|
||||
le @Model.DateCreation.ToString("dd/MM/yyyy HH:mm:ss")
|
||||
sur <em>@Model.TitreLibelle</em>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<form asp-action="Delete" method="post">
|
||||
<input type="hidden" asp-for="IdCommentaire" />
|
||||
|
||||
<button type="submit" class="btn btn-danger">
|
||||
Supprimer
|
||||
</button>
|
||||
</form>
|
||||
|
||||
<br/>
|
||||
<br/>
|
||||
|
||||
<a asp-action="Index"
|
||||
class="btn-link">
|
||||
Retour à l'administration des commentaires
|
||||
</a>
|
||||
|
||||
</div>
|
||||
@@ -1,4 +1,4 @@
|
||||
@model Webzine.WebApplication.Areas.Administration.ViewModels.CommentaireViewModel
|
||||
@model Webzine.WebApplication.Areas.Administration.ViewModels.Commentaire.CommentaireViewModel
|
||||
|
||||
@{
|
||||
ViewData["Title"] = "Commentaires";
|
||||
@@ -24,7 +24,7 @@
|
||||
{
|
||||
<tr class="align-middle">
|
||||
<td>
|
||||
@(commentaire.Titre != null ? commentaire.Titre.Libelle : "Titre inconnu")
|
||||
@commentaire.Titre.Libelle
|
||||
</td>
|
||||
<td>
|
||||
@commentaire.Auteur
|
||||
@@ -36,13 +36,9 @@
|
||||
@commentaire.DateCreation.ToString("dd/MM/yyyy HH:mm:ss")
|
||||
</td>
|
||||
<td class="text-center">
|
||||
<form method="post" action="@Url.Action("Delete", new { id = commentaire.IdCommentaire })" class="d-inline">
|
||||
@Html.AntiForgeryToken()
|
||||
<!-- Bouton rouge (text-danger) sans fond (btn-link) -->
|
||||
<button type="submit" class="btn btn-link text-primary p-0" title="Supprimer" onclick="return confirm('Êtes-vous sûr de vouloir supprimer ce commentaire ?');">
|
||||
<i class="fas fa-trash"></i>
|
||||
</button>
|
||||
</form>
|
||||
<a asp-action="Delete" asp-route-id="@commentaire.IdCommentaire" class="d-inline btn btn-link text-primary">
|
||||
<i class="fas fa-trash"></i>
|
||||
</a>
|
||||
</td>
|
||||
</tr>
|
||||
}
|
||||
@@ -1,113 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Webzine.Entity;
|
||||
using Webzine.WebApplication.ViewModels;
|
||||
|
||||
namespace Webzine.WebApplication.Controllers
|
||||
{
|
||||
[Area("Admin")]
|
||||
public class CommentairesController : Controller
|
||||
{
|
||||
// GET: Admin/Commentaires
|
||||
public ActionResult Index()
|
||||
{
|
||||
// Création de données "bouchon" (mock) pour tester l'affichage
|
||||
var listeCommentaires = new List<Commentaire>
|
||||
{
|
||||
new Commentaire
|
||||
{
|
||||
IdCommentaire = 1, // Correction: Id -> IdCommentaire
|
||||
Auteur = "Michel", // Correction: Nom -> Auteur
|
||||
Contenu = "Nulla sed velit nec tellus gravida molestie",
|
||||
DateCreation = new DateTime(2023, 1, 22, 15, 59, 28),
|
||||
// Important : On initialise l'objet Titre pour accéder à Titre.Libelle
|
||||
Titre = new Titre { Libelle = "St Germain - So Flute" },
|
||||
},
|
||||
new Commentaire
|
||||
{
|
||||
IdCommentaire = 2,
|
||||
Auteur = "Jeff",
|
||||
Contenu = "Lorem ipsum dolor sit.",
|
||||
DateCreation = new DateTime(2023, 1, 22, 14, 27, 8),
|
||||
Titre = new Titre { Libelle = "Queen - Bohemian Rapsody" },
|
||||
},
|
||||
new Commentaire
|
||||
{
|
||||
IdCommentaire = 3,
|
||||
Auteur = "Eva",
|
||||
Contenu = "Aenean vulputate eleifend tellus.",
|
||||
DateCreation = new DateTime(2023, 1, 22, 13, 2, 17),
|
||||
Titre = new Titre { Libelle = "Rammstein - Du hast" },
|
||||
},
|
||||
};
|
||||
|
||||
// Initialisation du ViewModel
|
||||
var viewModel = new CommentaireViewModel
|
||||
{
|
||||
Commentaires = listeCommentaires
|
||||
};
|
||||
|
||||
return View(viewModel);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// GET: CommentairesController/Details/5
|
||||
public ActionResult Details(int id)
|
||||
{
|
||||
return View();
|
||||
}
|
||||
|
||||
// GET: CommentairesController/Create
|
||||
public ActionResult Create()
|
||||
{
|
||||
return View();
|
||||
}
|
||||
|
||||
// POST: CommentairesController/Create
|
||||
[HttpPost]
|
||||
[ValidateAntiForgeryToken]
|
||||
public ActionResult Create(IFormCollection collection)
|
||||
{
|
||||
try
|
||||
{
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
catch
|
||||
{
|
||||
return View();
|
||||
}
|
||||
}
|
||||
|
||||
// GET: CommentairesController/Edit/5
|
||||
public ActionResult Edit(int id)
|
||||
{
|
||||
return View();
|
||||
}
|
||||
|
||||
// POST: CommentairesController/Edit/5
|
||||
[HttpPost]
|
||||
[ValidateAntiForgeryToken]
|
||||
public ActionResult Edit(int id, IFormCollection collection)
|
||||
{
|
||||
try
|
||||
{
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
catch
|
||||
{
|
||||
return View();
|
||||
}
|
||||
}
|
||||
|
||||
// GET: CommentairesController/Delete/5
|
||||
public ActionResult Delete(int id)
|
||||
{
|
||||
return View();
|
||||
}
|
||||
|
||||
// POST: CommentairesController/Delete/5
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user