feat: Refactor Titre views and view models to use Administration area

This commit is contained in:
mirage
2026-03-05 15:55:07 +01:00
parent c42a4bb72c
commit de6ca18eac
13 changed files with 28 additions and 12 deletions

View File

@@ -0,0 +1,164 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Webzine.Entity;
using Webzine.Entity.Fixtures;
using Webzine.WebApplication.Areas.Administration.ViewModels.Titre;
namespace Webzine.WebApplication.Areas.Administration.Controllers;
[Area("Administration")]
public class TitreController : Controller
{
private readonly ILogger<TitreController> _logger;
private readonly List<Titre> _titres;
private readonly List<Style> _styles;
private readonly List<Artiste> _artistes;
/// <summary>
/// Initialise une nouvelle instance du <see cref="TitreController"/>.
/// Les données sont générées dynamiquement via <see cref="DataFactory"/>.
/// </summary>
/// <param name="logger">Service de journalisation injecté.</param>
public TitreController(ILogger<TitreController> logger)
{
_logger = logger;
_logger.LogInformation("Initialisation du contrôleur TitreController.");
var factory = new DataFactory();
_artistes = factory.GenerateArtists(10);
_styles = factory.GenerateStyles(10);
_titres = factory.GenerateTitres(30, _artistes, _styles);
factory.GenerateCommentaires(50, _titres);
_logger.LogInformation("Données fictives générées avec succès.");
}
public ActionResult Index()
{
var model = _titres.Select(t => new AdminTitreList
{
Id = t.IdTitre,
Artiste = t.Artiste?.Nom,
Titre = t.Libelle,
Duree = TimeSpan.FromSeconds(t.Duree).ToString(@"mm\:ss"),
DateSortie = t.DateSortie,
NbLectures = t.NbLectures,
NbLikes = t.NbLikes,
NbCommentaires = t.Commentaires?.Count ?? 0
}).ToList();
return View(model);
}
public ActionResult Create()
{
var model = new AdminTitreForm
{
Artistes = _artistes.Select(a => new SelectListItem
{
Value = a.IdArtiste.ToString(),
Text = a.Nom
}).ToList(),
AllStyles = _styles.Select(s => new SelectListItem
{
Value = s.IdStyle.ToString(),
Text = s.Libelle
}).ToList()
};
return View(model);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(IFormCollection collection)
{
try
{
return RedirectToAction(nameof(Index));
}
catch
{
return View();
}
}
public ActionResult Edit(int id)
{
var titre = _titres.First(t => t.IdTitre == id);
var model = new AdminTitreForm
{
Id = titre.IdTitre,
IdArtiste = titre.IdArtiste,
Libelle = titre.Libelle,
Album = titre.Album,
Chronique = titre.Chronique,
DateSortie = titre.DateSortie,
Duree = titre.Duree,
UrlJaquette = titre.UrlJaquette,
UrlEcoute = titre.UrlEcoute,
NbLectures = titre.NbLectures,
NbLikes = titre.NbLikes,
Styles = titre.Styles.Select(s => s.IdStyle).ToList(),
Artistes = _artistes.Select(a => new SelectListItem
{
Value = a.IdArtiste.ToString(),
Text = a.Nom
}).ToList(),
AllStyles = _styles.Select(s => new SelectListItem
{
Value = s.IdStyle.ToString(),
Text = s.Libelle
}).ToList()
};
return View(model);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(int id, IFormCollection collection)
{
try
{
return RedirectToAction(nameof(Index));
}
catch
{
return View();
}
}
public ActionResult Delete(int id)
{
var titre = _titres.First(t => t.IdTitre == id);
var model = new AdminTitreDelete
{
Id = titre.IdTitre,
Titre = titre.Libelle,
Artiste = titre.Artiste?.Nom
};
return View(model);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Delete(AdminTitreDelete model)
{
var titre = _titres.First(t => t.IdTitre == model.Id);
_titres.Remove(titre);
return RedirectToAction(nameof(Index));
}
}

View File

@@ -0,0 +1,19 @@
// <copyright file="CommentaireViewModel.cs" company="Webzine">
// Copyright (c) Webzine. All rights reserved.
// </copyright>
using Webzine.Entity;
namespace Webzine.WebApplication.ViewModels
{
/// <summary>
/// ViewModel pour afficher la liste des commentaires en administration.
/// </summary>
public class CommentaireViewModel
{
/// <summary>
/// Obtient ou définit la liste des commentaires.
/// </summary>
public IEnumerable<Commentaire> Commentaires { get; set; } = new List<Commentaire>();
}
}

View File

@@ -0,0 +1,10 @@
namespace Webzine.WebApplication.Areas.Administration.ViewModels.Titre;
public class AdminTitreDelete
{
public int Id { get; set; }
public string Titre { get; set; }
public string Artiste { get; set; }
}

View File

@@ -0,0 +1,34 @@
using Microsoft.AspNetCore.Mvc.Rendering;
namespace Webzine.WebApplication.Areas.Administration.ViewModels.Titre;
public class AdminTitreForm
{
public int Id { get; set; }
public int IdArtiste { get; set; }
public string Libelle { get; set; }
public string Album { get; set; }
public string Chronique { get; set; }
public DateTime DateSortie { get; set; }
public int Duree { get; set; }
public string UrlJaquette { get; set; }
public string UrlEcoute { get; set; }
public int NbLectures { get; set; }
public int NbLikes { get; set; }
public List<int> Styles { get; set; } = new();
public List<SelectListItem> Artistes { get; set; }
public List<SelectListItem> AllStyles { get; set; }
}

View File

@@ -0,0 +1,21 @@
namespace Webzine.WebApplication.Areas.Administration.ViewModels.Titre
{
public class AdminTitreList
{
public int Id { get; set; }
public string Artiste { get; set; }
public string Titre { get; set; }
public string Duree { get; set; }
public DateTime DateSortie { get; set; }
public int NbLectures { get; set; }
public int NbLikes { get; set; }
public int NbCommentaires { get; set; }
}
}

View File

@@ -0,0 +1,89 @@
@using Webzine.WebApplication.ViewModels
@using Webzine.Entity
@model CommentaireViewModel
@{
ViewData["Title"] = "Commentaires";
}
<style>
.commentaires-container {
overflow-x: auto;
}
.commentaires-table {
width: 100%;
border-collapse: collapse;
margin-top: 1.5rem;
background: white;
}
.commentaires-table thead {
background: #ddd;
font-weight: bold;
}
.commentaires-table th, .commentaires-table td {
padding: 1rem;
border: 1px solid #ccc;
text-align: left;
}
.btn-delete {
color: #0066cc;
border: none;
background: none;
cursor: pointer;
font-size: 1.2rem;
}
.btn-delete:hover {
color: #ff4444;
}
</style>
<h1>Commentaires</h1>
<div class="commentaires-container">
<table class="commentaires-table">
<thead>
<tr>
<th>Titre</th>
<th>Auteur</th>
<th>Commentaire</th>
<th>Date de création</th>
<th style="text-align: center;">Actions</th>
</tr>
</thead>
<tbody>
@foreach (Webzine.Entity.Commentaire commentaire in Model.Commentaires)
{
<tr>
<td>
<!-- Titre est un objet, on affiche sa propriété Libelle -->
@(commentaire.Titre != null ? commentaire.Titre.Libelle : "Titre inconnu")
</td>
<td>
<!-- On utilise Auteur (et pas Nom) -->
@commentaire.Auteur
</td>
<td>
@commentaire.Contenu
</td>
<td>
@commentaire.DateCreation.ToString("dd/MM/yyyy HH:mm:ss")
</td>
<td style="text-align: center;">
<!-- On utilise IdCommentaire (et pas Id) -->
<form method="post" action="@Url.Action("Delete", new { id = commentaire.IdCommentaire })" style="display: inline;">
@Html.AntiForgeryToken()
<button type="submit" class="btn-delete" title="Supprimer">
<i class="fas fa-trash"></i>
</button>
</form>
</td>
</tr>
}
</tbody>
</table>
</div>

View File

@@ -0,0 +1,27 @@
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>@ViewData["Title"] - Webzine</title>
@* Ajout de bootstrap *@
<script src="/js/bootstrap.min.js" defer></script>
<link rel="stylesheet" href="/css/app.css">
<link rel="stylesheet" href="/css/bootstrap.min.css">
@* Ajout de font-awesome *@
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">
</head>
<body>
<div class="container">
@await Html.PartialAsync("_Header")
<div class="row mt-5">
<main class="col-9">
@RenderBody()
</main>
@await Html.PartialAsync("_Sidebar")
</div>
</div>
</body>
</html>

View File

@@ -0,0 +1,11 @@
@model Webzine.WebApplication.Areas.Administration.ViewModels.Titre.AdminTitreForm
<h1>Créer un titre</h1>
<hr />
<form asp-action="Create" method="post">
<partial name="_Form"/>
</form>

View File

@@ -0,0 +1,34 @@
@model Webzine.WebApplication.Areas.Administration.ViewModels.Titre.AdminTitreDelete
<div class="container mt-4">
<h1 class="mb-3">Supprimer un titre</h1>
<hr />
<p>
Etes-vous sûr de vouloir supprimer le titre
"@Model.Titre"
de
@Model.Artiste ?
</p>
<form asp-action="Delete" method="post">
<input type="hidden" asp-for="Id"/>
<button type="submit" class="btn btn-danger">
Supprimer
</button>
</form>
<br/>
<br/>
<a asp-action="Index">
Retour à l'administration des titres
</a>
</div>

View File

@@ -0,0 +1,13 @@
@model Webzine.WebApplication.Areas.Administration.ViewModels.Titre.AdminTitreForm
<h1>Editer un titre</h1>
<hr />
<form asp-action="Edit" method="post">
<input type="hidden" asp-for="Id"/>
<partial name="_Form"/>
</form>

View File

@@ -0,0 +1,74 @@
@model IEnumerable<Webzine.WebApplication.Areas.Administration.ViewModels.Titre.AdminTitreList>
@{
ViewData["Title"] = "Titres";
}
<div class="container mt-4">
<h1 class="mb-3">Titres</h1>
<hr />
<a asp-action="" class="btn btn-primary mb-3">
<i class="fa fa-plus"></i> Ajouter un nouvel titre
</a>
<table class="table table-striped table-bordered align-middle">
<thead class="table-light">
<tr>
<th>Artiste</th>
<th>Titre</th>
<th>Durée</th>
<th>Date de sortie</th>
<th class="text-center"><i class="fa fa-eye"></i></th>
<th class="text-center"><i class="fa fa-thumbs-up"></i></th>
<th class="text-center"><i class="fa fa-comments"></i></th>
<th class="text-center action-column">Actions</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>@item.Artiste</td>
<td>@item.Titre</td>
<td>@item.Duree</td>
<td>@item.DateSortie.ToString("dd/MM/yyyy")</td>
<td class="text-center">@item.NbLectures</td>
<td class="text-center">@item.NbLikes</td>
<td class="text-center">@item.NbCommentaires</td>
<td class="text-center action-column">
<a asp-action="Edit" asp-route-id="@item.Id"
class="btn btn-sm btn-outline-primary">
<i class="fa fa-pen"></i>
</a>
<a asp-action="Delete" asp-route-id="@item.Id"
class="btn btn-sm btn-outline-danger">
<i class="fa fa-trash"></i>
</a>
</td>
</tr>
}
</tbody>
</table>
</div>
<style>
.action-column{
width:120px;
white-space:nowrap;
}
.action-column .btn{
margin-right:4px;
}
</style>

View File

@@ -0,0 +1,135 @@
@model Webzine.WebApplication.Areas.Administration.ViewModels.Titre.AdminTitreForm
<div class="container">
<!-- ARTISTE -->
<div class="row mb-3 align-items-center">
<label class="col-md-3 col-form-label">Nom de l'artiste<span class="text-danger">*</span></label>
<div class="col-md-9">
<select asp-for="IdArtiste"
asp-items="Model.Artistes"
class="form-select"></select>
</div>
</div>
<!-- TITRE -->
<div class="row mb-3 align-items-center">
<label class="col-md-3 col-form-label">Titre<span class="text-danger">*</span></label>
<div class="col-md-9">
<input asp-for="Libelle" class="form-control"/>
</div>
</div>
<!-- ALBUM -->
<div class="row mb-3 align-items-center">
<label class="col-md-3 col-form-label">Album<span class="text-danger">*</span></label>
<div class="col-md-9">
<input asp-for="Album" class="form-control"/>
</div>
</div>
<!-- CHRONIQUE -->
<div class="row mb-3">
<label class="col-md-3 col-form-label">Chronique<span class="text-danger">*</span></label>
<div class="col-md-9">
<textarea asp-for="Chronique"
class="form-control"
rows="5"></textarea>
</div>
</div>
<!-- DATE + DUREE -->
<div class="row mb-3 align-items-center">
<label class="col-md-3 col-form-label">Date de sortie<span class="text-danger">*</span></label>
<div class="col-md-3">
<input type="text"
class="form-control"
name="DateSortie"
pattern="\d{2}/\d{2}/\d{4}"
value="@Model.DateSortie.ToString("d")"/>
</div>
<label class="col-md-3 col-form-label">Durée en secondes<span class="text-danger">*</span></label>
<div class="col-md-3">
<div class="input-group">
<input asp-for="Duree"
class="form-control"
type="number"
min="0" />
<span class="input-group-text text-muted">seconds</span>
</div>
</div>
</div>
<!-- JAQUETTE -->
<div class="row mb-3 align-items-center">
<label class="col-md-3 col-form-label">Jaquette<span class="text-danger">*</span></label>
<div class="col-md-9">
<input asp-for="UrlJaquette"
class="form-control"/>
</div>
</div>
<!-- URL ECOUTE -->
<div class="row mb-3 align-items-center">
<label class="col-md-3 col-form-label">URL d'écoute</label>
<div class="col-md-9">
<input asp-for="UrlEcoute"
class="form-control"/>
</div>
</div>
<!-- STYLES -->
<div class="row mb-4">
<label class="col-md-3 col-form-label">Styles</label>
<div class="col-md-9">
<div class="row">
@foreach (var style in Model.AllStyles)
{
<div class="col-md-4 form-check">
<input class="form-check-input"
type="checkbox"
name="Styles"
value="@style.Value"
@(Model.Styles.Contains(int.Parse(style.Value)) ? "checked" : "") />
<label class="form-check-label">
@style.Text
</label>
</div>
}
</div>
</div>
</div>
<!-- LECTURES / LIKES (AFFICHAGE UNIQUEMENT) -->
<div class="row mb-4 align-items-center">
<label class="col-md-3 col-form-label">Nb de lectures<span class="text-danger">*</span></label>
<div class="col-md-3">
@Model.NbLectures
</div>
</div>
<div class="row mb-4 align-items-center">
<label class="col-md-3 col-form-label">Nb de likes<span class="text-danger">*</span></label>
<div class="col-md-3">
@Model.NbLikes
</div>
</div>
<!-- BOUTONS -->
<div class="row mt-4">
<div class="col-md-9 offset-md-3">
<button type="submit" class="btn btn-primary me-2">
Sauvegarder
</button>
</div>
</div>
<br />
<br />
<a asp-action="Index"
class="btn text-primary">
Retour à l'administration des titres
</a>
</div>