Files
webzine/Webzine.WebApplication/Areas/Administration/Controllers/CommentairesController.cs

119 lines
4.2 KiB
C#

using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Webzine.Entity;
using Webzine.Entity.Fixtures;
using Webzine.WebApplication.Areas.Administration.ViewModels.Commentaire;
namespace Webzine.WebApplication.Areas.Administration.Controllers
{
[Area("Administration")]
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()
{
// 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: Administration/Commentaires/Delete/5
public ActionResult Delete(int id)
{
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: 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);
}
}
}
}