Merge pull request 'Optimisation de la recherche de puis le header.' (#159) from j3/recherche_linq into dev

Reviewed-on: https://10.4.0.131/gitea/DI1-P4-E1/Webzine/pulls/159
This commit is contained in:
j.vetu
2026-03-31 14:48:59 +02:00
10 changed files with 118 additions and 119 deletions

View File

@@ -44,5 +44,12 @@ namespace Webzine.Repository.Contracts
/// </summary>
/// <param name="artiste">L'artiste à mettre à jour.</param>
void Update(Artiste artiste);
/// <summary>
/// Récupérer une liste d'artiste à partir d'une recherche.
/// </summary>
/// <param name="nom">Nom de l'artiste.</param>
/// <returns>IEnumarble.<Artiste> qui contient la chaine de caractere.</returns>
IEnumerable<Artiste> Search(string nom);
}
}

View File

@@ -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;
}
}
/// <inheritdoc/>
public IEnumerable<Artiste> 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);
}
}
}
}

View File

@@ -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);

View File

@@ -85,5 +85,13 @@ namespace Webzine.Repository
{
throw new NotSupportedException("Mode Local");
}
/// <inheritdoc/>
public IEnumerable<Artiste> Search(string mot)
{
return this.dataStore.Artistes
.Where(a => a.Nom.ToLower().Contains(mot.ToLower()))
.ToList();
}
}
}

View File

@@ -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<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("")]
/// <summary>
/// Affichage de la page Recherche depuis le header de l'app.
/// </summary>
/// <param name="mot">Nom d'artiste ou de titre.</param>
/// <returns>Page de recherche avec les résultats.</returns>
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,

View File

@@ -1,17 +0,0 @@
namespace Webzine.WebApplication.ViewModels.Recherche;
/// <summary>
/// ViewModel pour afficher un artiste dans les resultats de recherche.
/// </summary>
public class RechercheArtisteItem
{
/// <summary>
/// Nom de l'artiste.
/// </summary>
public string? Nom { get; set; }
/// <summary>
/// Nombre de titres associes a l'artiste.
/// </summary>
public int NombreDeTitres { get; set; }
}

View File

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

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

@@ -61,7 +61,7 @@
</ul>
<!-- Barre de recherche -->
<form class="d-flex" asp-controller="Recherche" asp-action="Index" method="post">
<form class="d-flex" asp-controller="Recherche" asp-action="Index" method="get">
<div class="input-group">
<div class="form-outline">
<input class="form-control me-2"

View File