#154 : Première optimisation de la recherche

This commit is contained in:
Loic Masi
2026-03-31 13:06:30 +02:00
parent c65c0113af
commit 6f009fcc3d
6 changed files with 72 additions and 100 deletions

View File

@@ -120,7 +120,7 @@ namespace Webzine.Repository
try try
{ {
// .AsNoTracking() rend la requête beaucoup plus rapide pour de la lecture // .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); this.logger.LogDebug("{Count} artistes récupérés de la base.", artistes.Count);
return artistes; return artistes;
} }

View File

@@ -242,6 +242,7 @@ public class DbTitreRepository : ITitreRepository
.Include(t => t.Styles) .Include(t => t.Styles)
.Where(t => t.Libelle.ToLower().Contains(mot.ToLower())) .Where(t => t.Libelle.ToLower().Contains(mot.ToLower()))
.OrderBy(t => t.Libelle) .OrderBy(t => t.Libelle)
.AsNoTracking()
.ToList(); .ToList();
this.logger.LogDebug("{Count} titres trouvés correspondant à '{Mot}'", titres.Count, mot); this.logger.LogDebug("{Count} titres trouvés correspondant à '{Mot}'", titres.Count, mot);

View File

@@ -8,52 +8,32 @@ namespace Webzine.WebApplication.Controllers
using Webzine.Repository.Contracts; using Webzine.Repository.Contracts;
using Webzine.WebApplication.ViewModels.Recherche; using Webzine.WebApplication.ViewModels.Recherche;
using Webzine.WebApplication.ViewModels.Titre;
[Route("recherche")]
public class RechercheController : Controller public class RechercheController : Controller
{ {
private readonly ILogger<RechercheController> logger; private readonly ILogger<RechercheController> logger;
private readonly ITitreRepository titreRepository; private readonly ITitreRepository titreRepository;
private readonly IArtisteRepository artisteRepository;
public RechercheController(ILogger<RechercheController> logger, ITitreRepository titreRepository) public RechercheController(
ILogger<RechercheController> logger,
ITitreRepository titreRepository,
IArtisteRepository artisteRepository)
{ {
this.logger = logger; this.logger = logger;
this.titreRepository = titreRepository; this.titreRepository = titreRepository;
this.artisteRepository = artisteRepository;
} }
[HttpPost("")]
public IActionResult Index(string mot) public IActionResult Index(string mot)
{ {
this.logger.LogInformation("Recherche artistes/titres pour le mot : {Mot}.", mot); this.logger.LogInformation("Recherche artistes/titres pour le mot : {Mot}.", mot);
var titres = this.titreRepository.Search(mot) var titres = this.titreRepository.Search(mot).ToList();
.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();
var artistes = this.titreRepository.FindAll() var artistes = this.artisteRepository
.Select(t => t.Artiste) .FindAll()
.Where(a => a != null .Where(a => a.Nom.ToLower().Contains(mot.ToLower()))
&& !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(); .ToList();
var vm = new RechercheIndexViewModel var vm = new RechercheIndexViewModel

View File

@@ -1,12 +1,11 @@
using Webzine.WebApplication.ViewModels.Titre; namespace Webzine.WebApplication.ViewModels.Recherche
namespace Webzine.WebApplication.ViewModels.Recherche;
/// <summary>
/// ViewModel pour afficher les resultats de recherche d'artistes et de titres.
/// </summary>
public class RechercheIndexViewModel
{ {
using Webzine.Entity;
/// <summary>
/// ViewModel pour afficher les resultats de recherche d'artistes et de titres.
/// </summary>
public class RechercheIndexViewModel
{
/// <summary> /// <summary>
/// Mot saisi dans le formulaire. /// Mot saisi dans le formulaire.
/// </summary> /// </summary>
@@ -15,10 +14,11 @@ public class RechercheIndexViewModel
/// <summary> /// <summary>
/// Artistes trouves. /// Artistes trouves.
/// </summary> /// </summary>
public List<RechercheArtisteItem> Artistes { get; set; } = new (); public List<Artiste> Artistes { get; set; } = new ();
/// <summary> /// <summary>
/// Titres trouves. /// Titres trouves.
/// </summary> /// </summary>
public List<TitreStyleItem> Titres { get; set; } = new (); public List<Titre> Titres { get; set; } = new ();
}
} }

View File

@@ -10,25 +10,15 @@
<h1 class="mb-4">Resultats pour "@Model.Mot"</h1> <h1 class="mb-4">Resultats pour "@Model.Mot"</h1>
<hr /> <hr />
@if (string.IsNullOrWhiteSpace(Model.Mot))
{
<div class="alert alert-info">
Saisissez un mot-cle pour lancer une recherche.
</div>
}
else if (!Model.Artistes.Any() && !Model.Titres.Any())
{
<div class="alert alert-info">
Aucun artiste ni titre ne correspond a votre recherche.
</div>
}
else
{
@if (Model.Artistes.Any())
{
<h2 class="h4 mt-4">Artistes</h2> <h2 class="h4 mt-4">Artistes</h2>
@if (!Model.Artistes.Any())
{
<div class="alert alert-info">
<p>Aucun artiste n'a été trouvé.</p>
</div>
}
@foreach (var artiste in Model.Artistes) @foreach (var artiste in Model.Artistes)
{ {
<div class="my-3"> <div class="my-3">
@@ -37,15 +27,18 @@
asp-route-nom="@artiste.Nom"> asp-route-nom="@artiste.Nom">
@artiste.Nom @artiste.Nom
</a> </a>
<span class="text-muted">(@artiste.NombreDeTitres titre(s))</span> <span class="text-muted">(@artiste.Titres.Count titre(s))</span>
</div> </div>
} }
}
@if (Model.Titres.Any())
{
<h2 class="h4 mt-4">Titres</h2> <h2 class="h4 mt-4">Titres</h2>
@if (!Model.Titres.Any())
{
<div class="alert alert-info">
<p>Aucun titre n'a été trouvé.</p>
</div>
}
@foreach (var titre in Model.Titres) @foreach (var titre in Model.Titres)
{ {
<div class="d-flex align-items-start my-3"> <div class="d-flex align-items-start my-3">
@@ -53,15 +46,15 @@
asp-action="Details" asp-action="Details"
asp-route-id="@titre.IdTitre" asp-route-id="@titre.IdTitre"
class="me-3 text-black"> class="me-3 text-black">
<img src="@titre.UrlJaquette" alt="@titre.Libelle" width="70" height="70" class="object-fit-cover" loading="lazy"/> <img src="@titre.UrlJaquette" alt="@titre.Libelle" width="70" height="70" class="object-fit-cover" loading="lazy" />
</a> </a>
<div class="justify-content-center d-flex flex-column"> <div class="justify-content-center d-flex flex-column">
<div> <div>
<a asp-controller="Artiste" <a asp-controller="Artiste"
asp-action="Index" asp-action="Index"
asp-route-nom="@titre.ArtisteNom"> asp-route-nom="@titre.Artiste.Nom">
@titre.ArtisteNom @titre.Artiste.Nom
</a> </a>
- -
<a asp-controller="Titre" <a asp-controller="Titre"
@@ -77,8 +70,6 @@
</div> </div>
</div> </div>
} }
}
}
</div> </div>
</div> </div>
</div> </div>

View File