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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user