Merge branch 'J1/admin/commentaire' into J1/feat/AdminTitreIHM
# Conflicts: # Webzine.WebApplication/Webzine.WebApplication.csproj
This commit is contained in:
@@ -0,0 +1,163 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Rendering;
|
||||
using Webzine.Entity;
|
||||
using Webzine.Entity.Fixtures;
|
||||
using Webzine.WebApplication.ViewModels.Admin.Titre;
|
||||
|
||||
namespace Webzine.WebApplication.Areas.Admin.Controllers;
|
||||
|
||||
public class TitreController : Microsoft.AspNetCore.Mvc.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));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user