From b407317e95e59f63b38668105d019e336cb65451 Mon Sep 17 00:00:00 2001 From: "josephine.vetu" Date: Thu, 5 Mar 2026 14:10:56 +0100 Subject: [PATCH] =?UTF-8?q?#21=20Cr=C3=A9ation=20d'un=20ArtisteFactory=20q?= =?UTF-8?q?ui=20g=C3=A9n=C3=A8re=20des=20titres=20et=20deux=20albums=20par?= =?UTF-8?q?=20artiste.=20Le=20viewmod=C3=A8le=20est=20pass=C3=A9=20dans=20?= =?UTF-8?q?la=20vue=20par=20le=20controleur.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Webzine.Entity/Fixtures/ArtisteFactory.cs | 54 ++++++++++++++ Webzine.Entity/Webzine.Entity.csproj | 1 + .../Controllers/AccueilController.cs | 9 +++ .../Controllers/ArtisteController.cs | 54 ++++++++++++++ .../ViewModels/ArtisteModel.cs | 17 +++++ .../Views/Artiste/Index.cshtml | 70 +++++++++++++++++++ .../Views/Shared/_Layout.cshtml | 2 +- .../Webzine.WebApplication.csproj.user | 5 +- 8 files changed, 210 insertions(+), 2 deletions(-) create mode 100644 Webzine.Entity/Fixtures/ArtisteFactory.cs create mode 100644 Webzine.WebApplication/Controllers/ArtisteController.cs create mode 100644 Webzine.WebApplication/ViewModels/ArtisteModel.cs create mode 100644 Webzine.WebApplication/Views/Artiste/Index.cshtml diff --git a/Webzine.Entity/Fixtures/ArtisteFactory.cs b/Webzine.Entity/Fixtures/ArtisteFactory.cs new file mode 100644 index 0000000..4161e35 --- /dev/null +++ b/Webzine.Entity/Fixtures/ArtisteFactory.cs @@ -0,0 +1,54 @@ +using Bogus; + +namespace Webzine.Entity.Fixtures +{ + /// + /// Factory pour générer des artistes avec des titres associés, à l'aide de la bibliothèque Bogus. + /// + /// + public class ArtisteFactory + { + /// + /// Récupère un artiste par son nom, en générant des données fictives pour ses titres associés. + /// + /// + /// + public static Artiste FindByName(string nom) + { + // On définit nos albums "bouchonnés" + var albumsData = new[] + { + new { Nom = "Bohemian Rhapsody", Image = "https://upload.wikimedia.org/wikipedia/en/9/9f/Bohemian_Rhapsody.png" }, + new { Nom = "Born This Way", Image = "https://static.wikia.nocookie.net/ladygaga/images/2/2d/BornThisWay-DeluxeEdition.jpg/revision/latest/scale-to-width-down/3500?cb=20111120030308" } + }; + + var faker = new Bogus.Faker("fr"); + var tousLesTitres = new List(); + + // Pour chaque album, on génère un paquet de titres + foreach (var album in albumsData) + { + var nombreDeTitres = faker.Random.Number(3, 6); + + var titresDeLalbum = new Faker("fr") + .RuleFor(t => t.IdTitre, f => f.IndexFaker + 1 + tousLesTitres.Count) + .RuleFor(t => t.UrlJaquette, _ => album.Image) + .RuleFor(t => t.Album, _ => album.Nom) + .RuleFor(t => t.Duree, f => f.Random.Number(90, 180)) + .RuleFor(t => t.Libelle, f => f.Music.Genre() + " - " + f.Commerce.ProductName()) + .Generate(nombreDeTitres); + + tousLesTitres.AddRange(titresDeLalbum); + } + + // On crée l'artiste final + var artisteFaker = new Faker("fr") + .RuleFor(a => a.IdArtiste, f => f.IndexFaker + 1) + .RuleFor(a => a.Nom, _ => nom) + .RuleFor(a => a.Biographie, f => f.Lorem.Paragraphs(2)) + .RuleFor(a => a.Titres, _ => tousLesTitres); + + return artisteFaker.Generate(); + } + } +} \ No newline at end of file diff --git a/Webzine.Entity/Webzine.Entity.csproj b/Webzine.Entity/Webzine.Entity.csproj index 33c0658..06e46f1 100644 --- a/Webzine.Entity/Webzine.Entity.csproj +++ b/Webzine.Entity/Webzine.Entity.csproj @@ -7,6 +7,7 @@ + diff --git a/Webzine.WebApplication/Controllers/AccueilController.cs b/Webzine.WebApplication/Controllers/AccueilController.cs index 32e2e49..1602c40 100644 --- a/Webzine.WebApplication/Controllers/AccueilController.cs +++ b/Webzine.WebApplication/Controllers/AccueilController.cs @@ -5,9 +5,18 @@ namespace Webzine.WebApplication.Controllers { public class AccueilController : Controller { + // Injection du logger via le constructeur + private readonly ILogger _logger; + + public AccueilController(ILogger logger) + { + _logger = logger; + } + // GET: AccueilController public ActionResult Index() { + _logger.LogInformation("Arrivée sur la page d'accueil"); return View(); } diff --git a/Webzine.WebApplication/Controllers/ArtisteController.cs b/Webzine.WebApplication/Controllers/ArtisteController.cs new file mode 100644 index 0000000..0f26971 --- /dev/null +++ b/Webzine.WebApplication/Controllers/ArtisteController.cs @@ -0,0 +1,54 @@ +using Microsoft.AspNetCore.Mvc; +using Webzine.Entity.Fixtures; +using Webzine.WebApplication.ViewModels; + +namespace Webzine.WebApplication.Controllers +{ + public class ArtisteController : Controller + { + // Injection du logger via le constructeur + private readonly ILogger _logger; + + public ArtisteController(ILogger logger) + { + _logger = logger; + } + + /// + /// Prend en paramètre le nom de l'artiste (ex: "fatal-bazooka"), utilise la factory pour trouver l'artiste correspondant, et affiche sa page dédiée. + /// + /// + /// + [HttpGet("/artiste/{nom}")] + public IActionResult Index(string nom) + { + _logger.LogInformation("Tentative d'accès à l'artiste avec le nom : {NomArtiste}", nom); + + if (string.IsNullOrEmpty(nom)) return RedirectToAction("Index", "Accueil"); + + // On transforme "fatal-bazooka" en "Fatal Bazooka" pour la factory + string nomPropre = System.Globalization.CultureInfo.CurrentCulture.TextInfo + .ToTitleCase(nom.Replace("-", " ")); + + // On appelle la factory pour obtenir l'artiste unique + var artiste = ArtisteFactory.FindByName(nomPropre); + + if (artiste == null) + { + _logger.LogWarning("Artiste non trouvé pour le nom : {NomArtiste}", nomPropre); + return NotFound(); + } + + _logger.LogInformation("Artiste trouvé : {NomArtiste}", nom); + + // On remplit le ViewModel + var viewModel = new ArtisteModel + { + Artiste = artiste, + Titres = artiste.Titres + }; + + return View(viewModel); + } + } +} diff --git a/Webzine.WebApplication/ViewModels/ArtisteModel.cs b/Webzine.WebApplication/ViewModels/ArtisteModel.cs new file mode 100644 index 0000000..176623c --- /dev/null +++ b/Webzine.WebApplication/ViewModels/ArtisteModel.cs @@ -0,0 +1,17 @@ +using Webzine.Entity; + +namespace Webzine.WebApplication.ViewModels +{ + public class ArtisteModel + { + /// + /// Artiste dont on affiche les détails. + /// + public Artiste Artiste { get; set; } + + /// + /// Liste des titres de l'artiste. + /// + public List Titres { get; set; } + } +} diff --git a/Webzine.WebApplication/Views/Artiste/Index.cshtml b/Webzine.WebApplication/Views/Artiste/Index.cshtml new file mode 100644 index 0000000..2063243 --- /dev/null +++ b/Webzine.WebApplication/Views/Artiste/Index.cshtml @@ -0,0 +1,70 @@ +@model Webzine.WebApplication.ViewModels.ArtisteModel + +@{ + ViewData["Title"] = "Artiste"; +} + + +
+

@Model.Artiste.Nom

+ +
+ +

@Model.Artiste.Biographie

+ +

Albums

+
+ + @* On groupe les titres par nom d'album *@ + @{ + var albumsGroupes = Model.Titres + .OrderBy(t => t.Libelle) // Trie les titres par ordre alphabétique au sein de chaque groupe futur + .GroupBy(t => t.Album) // Groupe par nom d'album + .OrderBy(g => g.Key); // Trie les albums par ordre alphabétique (la clé du groupe) + } + + @foreach (var groupe in albumsGroupes) + { + // On récupère le premier titre du groupe pour afficher l'image de l'album + var premierTitre = groupe.First(); + +
+
+ Pochette de @groupe.Key +
+ +
+

@groupe.Key

+ + + + + + + + + + @foreach (var titre in groupe) + { + // Conversion des secondes en format MM:SS + var minutes = titre.Duree / 60; + var secondes = titre.Duree % 60; + var dureeFormatee = $"{minutes}:{secondes:D2}"; + + + + + + } + +
DuréeTitre
@dureeFormatee + + @titre.Libelle + +
+
+
+ } +
\ No newline at end of file diff --git a/Webzine.WebApplication/Views/Shared/_Layout.cshtml b/Webzine.WebApplication/Views/Shared/_Layout.cshtml index 2f42ca0..b807a6d 100644 --- a/Webzine.WebApplication/Views/Shared/_Layout.cshtml +++ b/Webzine.WebApplication/Views/Shared/_Layout.cshtml @@ -7,7 +7,7 @@ @* Ajout de bootstrap *@ - + @* Ajout de font-awesome *@ diff --git a/Webzine.WebApplication/Webzine.WebApplication.csproj.user b/Webzine.WebApplication/Webzine.WebApplication.csproj.user index ea9a183..2702025 100644 --- a/Webzine.WebApplication/Webzine.WebApplication.csproj.user +++ b/Webzine.WebApplication/Webzine.WebApplication.csproj.user @@ -2,9 +2,12 @@ https - MvcControllerWithActionsScaffolder + MvcControllerEmptyScaffolder root/Common/MVC/Controller RazorViewEmptyScaffolder root/Common/MVC/View + + ProjectDebugger + \ No newline at end of file