#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
{
// .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;
}

View File

@@ -242,6 +242,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);

View File

@@ -8,52 +8,32 @@ 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<RechercheController> logger;
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.titreRepository = titreRepository;
this.artisteRepository = artisteRepository;
}
[HttpPost("")]
public IActionResult Index(string mot)
{
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();
var titres = this.titreRepository.Search(mot).ToList();
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,
})
var artistes = this.artisteRepository
.FindAll()
.Where(a => a.Nom.ToLower().Contains(mot.ToLower()))
.ToList();
var vm = new RechercheIndexViewModel

View File

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

View File

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

View File