From 47b3c0bdd7e2f21f2018472f094b213b1f43e27f Mon Sep 17 00:00:00 2001 From: Loic Masi Date: Tue, 10 Mar 2026 21:38:40 +0100 Subject: [PATCH] Ajout du moteur de recherche dans le header --- .../ITtitreRepository.cs | 10 +- Webzine.Repository/LocalEntityRepository.cs | 67 ++++++++++-- Webzine.Repository/Webzine.Repository.csproj | 1 + .../Controllers/RechercheController.cs | 63 +++++++++++ .../Controllers/TitreController.cs | 102 ++++++++---------- Webzine.WebApplication/Program.cs | 21 ++-- .../Recherche/RechercheArtisteItem.cs | 17 +++ .../Recherche/RechercheIndexViewModel.cs | 24 +++++ .../Views/Recherche/Index.cshtml | 85 +++++++++++++++ .../Views/Shared/_Footer.cshtml | 6 +- .../Views/Shared/_Header.cshtml | 9 +- .../Views/Shared/_Layout.cshtml | 22 ++-- 12 files changed, 334 insertions(+), 93 deletions(-) create mode 100644 Webzine.WebApplication/Controllers/RechercheController.cs create mode 100644 Webzine.WebApplication/ViewModels/Recherche/RechercheArtisteItem.cs create mode 100644 Webzine.WebApplication/ViewModels/Recherche/RechercheIndexViewModel.cs create mode 100644 Webzine.WebApplication/Views/Recherche/Index.cshtml diff --git a/Webzine.Repository.Contracts/ITtitreRepository.cs b/Webzine.Repository.Contracts/ITtitreRepository.cs index b51c847..c7e9fc1 100644 --- a/Webzine.Repository.Contracts/ITtitreRepository.cs +++ b/Webzine.Repository.Contracts/ITtitreRepository.cs @@ -10,20 +10,20 @@ namespace Webzine.Repository.Contracts // void Delete(Titre titre); - // Titre Find(int idTitre); + Titre? Find(int idTitre); // IEnumerable FindTitres(int offset, int limit); - // IEnumerable FindAll(); + IEnumerable FindAll(); // void IncrementNbLectures(Titre titre); // void IncrementNbLikes(Titre titre); - // IEnumerable Search(string mot); + IEnumerable Search(string mot); - // IEnumerable SearchByStyle(string libelle); + IEnumerable SearchByStyle(string libelle); // void Update(Titre titre); } -} \ No newline at end of file +} diff --git a/Webzine.Repository/LocalEntityRepository.cs b/Webzine.Repository/LocalEntityRepository.cs index 74aa35b..8095e32 100644 --- a/Webzine.Repository/LocalEntityRepository.cs +++ b/Webzine.Repository/LocalEntityRepository.cs @@ -1,18 +1,71 @@ -using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging; +using Webzine.Entity; +using Webzine.Entity.Fixtures; +using Webzine.Repository.Contracts; namespace Webzine.Repository; -public class LocalEntityRepository +/// +/// Classe qui permet d'initialiser un jeu de données +/// pour tester l'application +/// +public class LocalEntityRepository : ITitreRepository { private readonly ILogger _logger; + private readonly List _titres; /// - /// Initialise une nouvelle instance du avec un service de journalisation injecté. + /// Initialise une nouvelle instance du avec un service de journalisation injecte. /// - /// Service de journalisation injecté pour suivre les opérations du repository. + /// Service de journalisation injecte pour suivre les operations du repository. public LocalEntityRepository(ILogger logger) { - this._logger = logger; - this._logger.LogDebug(1, "NLog injected into LocalEntityRepository"); + _logger = logger; + _logger.LogDebug(1, "NLog injected into LocalEntityRepository"); + + var factory = new DataFactory(); + var artistes = factory.GenerateArtists(10); + var styles = factory.GenerateStyles(10); + + _titres = factory.GenerateTitres(30, artistes, styles); + factory.GenerateCommentaires(50, _titres); } -} \ No newline at end of file + + public IEnumerable Search(string mot) + { + if (string.IsNullOrWhiteSpace(mot)) + { + return Enumerable.Empty(); + } + + return _titres + .Where(t => !string.IsNullOrWhiteSpace(t.Libelle) + && t.Libelle.Contains(mot, StringComparison.OrdinalIgnoreCase)) + .OrderBy(t => t.Libelle) + .ToList(); + } + + public Titre? Find(int idTitre) + { + return _titres.FirstOrDefault(t => t.IdTitre == idTitre); + } + + public IEnumerable FindAll() + { + return _titres; + } + + public IEnumerable SearchByStyle(string libelle) + { + if (string.IsNullOrWhiteSpace(libelle)) + { + return Enumerable.Empty(); + } + + return _titres + .Where(t => t.Styles.Any(s => !string.IsNullOrWhiteSpace(s.Libelle) + && s.Libelle.Contains(libelle, StringComparison.OrdinalIgnoreCase))) + .OrderBy(t => t.Libelle) + .ToList(); + } +} diff --git a/Webzine.Repository/Webzine.Repository.csproj b/Webzine.Repository/Webzine.Repository.csproj index cc38234..eaf2692 100644 --- a/Webzine.Repository/Webzine.Repository.csproj +++ b/Webzine.Repository/Webzine.Repository.csproj @@ -24,6 +24,7 @@ + diff --git a/Webzine.WebApplication/Controllers/RechercheController.cs b/Webzine.WebApplication/Controllers/RechercheController.cs new file mode 100644 index 0000000..ab4185a --- /dev/null +++ b/Webzine.WebApplication/Controllers/RechercheController.cs @@ -0,0 +1,63 @@ +using Microsoft.AspNetCore.Mvc; +using Webzine.Repository.Contracts; +using Webzine.WebApplication.ViewModels.Recherche; +using Webzine.WebApplication.ViewModels.Titre; + +namespace Webzine.WebApplication.Controllers; + +[Route("recherche")] +public class RechercheController : Controller +{ + private readonly ILogger _logger; + private readonly ITitreRepository _titreRepository; + + public RechercheController(ILogger logger, ITitreRepository titreRepository) + { + _logger = logger; + _titreRepository = titreRepository; + } + + [HttpPost("")] + public IActionResult Index(string mot) + { + _logger.LogInformation("Recherche artistes/titres pour le mot : {Mot}.", mot); + + var titres = _titreRepository.Search(mot) + .Concat(_titreRepository.SearchByStyle(mot)) + .DistinctBy(t => t.IdTitre) + .OrderBy(t => t.Libelle) + .Select(t => new TitreStyleItem + { + IdTitre = t.IdTitre, + Libelle = t.Libelle, + ArtisteNom = t.Artiste?.Nom, + UrlJaquette = t.UrlJaquette, + Duree = t.Duree + }) + .ToList(); + + var artistes = _titreRepository.FindAll() + .Select(t => t.Artiste) + .Where(a => a != null + && !string.IsNullOrWhiteSpace(a.Nom) + && !string.IsNullOrWhiteSpace(mot) + && a.Nom.Contains(mot, StringComparison.OrdinalIgnoreCase)) + .DistinctBy(a => a!.IdArtiste) + .OrderBy(a => a!.Nom) + .Select(a => new RechercheArtisteItem + { + Nom = a!.Nom, + NombreDeTitres = a.Titres?.Count ?? 0 + }) + .ToList(); + + var vm = new RechercheIndexViewModel + { + Mot = mot, + Artistes = artistes, + Titres = titres + }; + + return View(vm); + } +} diff --git a/Webzine.WebApplication/Controllers/TitreController.cs b/Webzine.WebApplication/Controllers/TitreController.cs index b89c757..cec375a 100644 --- a/Webzine.WebApplication/Controllers/TitreController.cs +++ b/Webzine.WebApplication/Controllers/TitreController.cs @@ -1,56 +1,45 @@ using Microsoft.AspNetCore.Mvc; using Webzine.Entity; -using Webzine.Entity.Fixtures; +using Webzine.Repository.Contracts; using Webzine.WebApplication.ViewModels.Titre; namespace Webzine.WebApplication.Controllers; /// -/// Contrôleur responsable de la gestion des titres musicaux : -/// affichage des détails, filtrage par style, -/// ajout de likes et commentaires. +/// Controleur responsable de la gestion des titres musicaux : +/// affichage des details, filtrage par style, +/// ajout de likes, commentaires et recherche. /// [Route("titre")] public class TitreController : Controller { private readonly ILogger _logger; - private readonly List _titres; - private readonly List