diff --git a/Webzine.Repository.Contracts/IArtisteRepository.cs b/Webzine.Repository.Contracts/IArtisteRepository.cs index 1ab6e83..bc13f76 100644 --- a/Webzine.Repository.Contracts/IArtisteRepository.cs +++ b/Webzine.Repository.Contracts/IArtisteRepository.cs @@ -44,5 +44,12 @@ namespace Webzine.Repository.Contracts /// /// L'artiste à mettre à jour. void Update(Artiste artiste); + + /// + /// Récupérer une liste d'artiste à partir d'une recherche. + /// + /// Nom de l'artiste. + /// IEnumarble. qui contient la chaine de caractere. + IEnumerable Search(string nom); } } \ No newline at end of file diff --git a/Webzine.Repository/DbArtisteRepository.cs b/Webzine.Repository/DbArtisteRepository.cs index 800f5af..fa6ff18 100644 --- a/Webzine.Repository/DbArtisteRepository.cs +++ b/Webzine.Repository/DbArtisteRepository.cs @@ -118,7 +118,7 @@ namespace Webzine.Repository try { // .AsNoTracking() rend la requête beaucoup plus rapide pour de la lecture - var artistes = this.context.Artistes.AsNoTracking().ToList(); + var artistes = this.context.Artistes.AsNoTracking().Include(t => t.Titres).ToList(); this.logger.LogDebug("{Count} artistes récupérés de la base.", artistes.Count); return artistes; } @@ -154,5 +154,27 @@ namespace Webzine.Repository throw; } } + + /// + public IEnumerable Search(string mot) + { + try + { + // Récupération des artistes et des titres + // qui leurs sont associés. + // ajout de 'ToLower' pour ne pas être sensible à la casse. + // NoTracking car action de lecture seulement. + var artiste = this.context.Artistes + .Where(a => a.Nom.ToLower().Contains(mot.ToLower())) + .Include(t => t.Titres) + .AsNoTracking() + .ToList(); + return artiste; + } + catch (Exception ex) + { + throw new Exception("Erreur lors de la recherche d'artiste {error}", ex); + } + } } } \ No newline at end of file diff --git a/Webzine.Repository/DbTitreRepository.cs b/Webzine.Repository/DbTitreRepository.cs index 9c0ac04..7098fa0 100644 --- a/Webzine.Repository/DbTitreRepository.cs +++ b/Webzine.Repository/DbTitreRepository.cs @@ -243,6 +243,7 @@ public class DbTitreRepository : ITitreRepository .Include(t => t.Styles) .Where(t => t.Libelle.ToLower().Contains(mot.ToLower())) .OrderBy(t => t.Libelle) + .AsNoTracking() .ToList(); this.logger.LogDebug("{Count} titres trouvés correspondant à '{Mot}'", titres.Count, mot); diff --git a/Webzine.Repository/LocalArtisteRepository.cs b/Webzine.Repository/LocalArtisteRepository.cs index 67ebabf..69021c5 100644 --- a/Webzine.Repository/LocalArtisteRepository.cs +++ b/Webzine.Repository/LocalArtisteRepository.cs @@ -85,5 +85,13 @@ namespace Webzine.Repository { throw new NotSupportedException("Mode Local"); } + + /// + public IEnumerable Search(string mot) + { + return this.dataStore.Artistes + .Where(a => a.Nom.ToLower().Contains(mot.ToLower())) + .ToList(); + } } } \ No newline at end of file diff --git a/Webzine.WebApplication/Controllers/RechercheController.cs b/Webzine.WebApplication/Controllers/RechercheController.cs index 4676c9d..2d5c4b9 100644 --- a/Webzine.WebApplication/Controllers/RechercheController.cs +++ b/Webzine.WebApplication/Controllers/RechercheController.cs @@ -8,54 +8,40 @@ namespace Webzine.WebApplication.Controllers using Webzine.Repository.Contracts; using Webzine.WebApplication.ViewModels.Recherche; - using Webzine.WebApplication.ViewModels.Titre; - [Route("recherche")] public class RechercheController : Controller { private readonly ILogger logger; private readonly ITitreRepository titreRepository; + private readonly IArtisteRepository artisteRepository; - public RechercheController(ILogger logger, ITitreRepository titreRepository) + public RechercheController( + ILogger logger, + ITitreRepository titreRepository, + IArtisteRepository artisteRepository) { this.logger = logger; this.titreRepository = titreRepository; + this.artisteRepository = artisteRepository; } - [HttpPost("")] + /// + /// Affichage de la page Recherche depuis le header de l'app. + /// + /// Nom d'artiste ou de titre. + /// Page de recherche avec les résultats. public IActionResult Index(string mot) { + // Logger la recherche. this.logger.LogInformation("Recherche artistes/titres pour le mot : {Mot}.", mot); - var titres = this.titreRepository.Search(mot) - .Concat(this.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(); + // Recherche des titres. + var titres = this.titreRepository.Search(mot); - var artistes = this.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(); + // Recherche des artistes. + var artistes = this.artisteRepository.Search(mot); + // Paramètres a retourner à la vue. var vm = new RechercheIndexViewModel { Mot = mot, diff --git a/Webzine.WebApplication/ViewModels/Recherche/RechercheArtisteItem.cs b/Webzine.WebApplication/ViewModels/Recherche/RechercheArtisteItem.cs deleted file mode 100644 index d179c3f..0000000 --- a/Webzine.WebApplication/ViewModels/Recherche/RechercheArtisteItem.cs +++ /dev/null @@ -1,17 +0,0 @@ -namespace Webzine.WebApplication.ViewModels.Recherche; - -/// -/// ViewModel pour afficher un artiste dans les resultats de recherche. -/// -public class RechercheArtisteItem -{ - /// - /// Nom de l'artiste. - /// - public string? Nom { get; set; } - - /// - /// Nombre de titres associes a l'artiste. - /// - public int NombreDeTitres { get; set; } -} \ No newline at end of file diff --git a/Webzine.WebApplication/ViewModels/Recherche/RechercheIndexViewModel.cs b/Webzine.WebApplication/ViewModels/Recherche/RechercheIndexViewModel.cs index 1d7b246..c1da754 100644 --- a/Webzine.WebApplication/ViewModels/Recherche/RechercheIndexViewModel.cs +++ b/Webzine.WebApplication/ViewModels/Recherche/RechercheIndexViewModel.cs @@ -1,24 +1,25 @@ -namespace Webzine.WebApplication.ViewModels.Recherche; - -using Webzine.WebApplication.ViewModels.Titre; - -/// -/// ViewModel pour afficher les resultats de recherche d'artistes et de titres. -/// -public class RechercheIndexViewModel +namespace Webzine.WebApplication.ViewModels.Recherche { - /// - /// Mot saisi dans le formulaire. - /// - public string? Mot { get; set; } + using Webzine.Entity; /// - /// Artistes trouves. + /// ViewModel pour afficher les resultats de recherche d'artistes et de titres. /// - public List Artistes { get; set; } = new (); + public class RechercheIndexViewModel + { + /// + /// Mot saisi dans le formulaire. + /// + public string? Mot { get; set; } - /// - /// Titres trouves. - /// - public List Titres { get; set; } = new (); + /// + /// Artistes trouves. + /// + public IEnumerable Artistes { get; set; } = new List(); + + /// + /// Titres trouves. + /// + public IEnumerable Titres { get; set; } = new List(); + } } \ No newline at end of file diff --git a/Webzine.WebApplication/Views/Recherche/Index.cshtml b/Webzine.WebApplication/Views/Recherche/Index.cshtml index cb5fca6..53e3853 100644 --- a/Webzine.WebApplication/Views/Recherche/Index.cshtml +++ b/Webzine.WebApplication/Views/Recherche/Index.cshtml @@ -10,74 +10,65 @@

Resultats pour "@Model.Mot"


+

Artistes

- @if (string.IsNullOrWhiteSpace(Model.Mot)) + @if (!Model.Artistes.Any()) {
- Saisissez un mot-cle pour lancer une recherche. +

Aucun artiste n'a été trouvé.

} - else if (!Model.Artistes.Any() && !Model.Titres.Any()) + + @foreach (var artiste in Model.Artistes) + { +
+ + @artiste.Nom + + (@artiste.Titres.Count titre(s)) +
+ } +

Titres

+ + @if (!Model.Titres.Any()) {
- Aucun artiste ni titre ne correspond a votre recherche. +

Aucun titre n'a été trouvé.

} - else - { - @if (Model.Artistes.Any()) - { -

Artistes

- @foreach (var artiste in Model.Artistes) - { -
+ @foreach (var titre in Model.Titres) + { +
+ + @titre.Libelle + + +
+ - } - } - - @if (Model.Titres.Any()) - { -

Titres

- - @foreach (var titre in Model.Titres) - { -
+ - - @titre.Libelle + asp-route-id="@titre.IdTitre"> + @titre.Libelle - -
- - -
- Duree : @TimeSpan.FromSeconds(titre.Duree).ToString(@"mm\:ss") -
-
- } - } + +
+ Duree : @TimeSpan.FromSeconds(titre.Duree).ToString(@"mm\:ss") +
+
+
}
diff --git a/Webzine.WebApplication/Views/Shared/_Header.cshtml b/Webzine.WebApplication/Views/Shared/_Header.cshtml index f538faf..ad2f930 100644 --- a/Webzine.WebApplication/Views/Shared/_Header.cshtml +++ b/Webzine.WebApplication/Views/Shared/_Header.cshtml @@ -61,7 +61,7 @@ -
+