#154 : Première optimisation de la recherche
This commit is contained in:
@@ -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;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
using Webzine.WebApplication.ViewModels.Titre;
|
namespace Webzine.WebApplication.ViewModels.Recherche
|
||||||
|
{
|
||||||
namespace Webzine.WebApplication.ViewModels.Recherche;
|
using Webzine.Entity;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// ViewModel pour afficher les resultats de recherche d'artistes et de titres.
|
/// ViewModel pour afficher les resultats de recherche d'artistes et de titres.
|
||||||
/// </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 ();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -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">
|
||||||
@@ -60,8 +53,8 @@
|
|||||||
<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>
|
||||||
|
|||||||
Reference in New Issue
Block a user