Files
webzine/Webzine.WebApplication/Controllers/RechercheController.cs
2026-03-10 21:38:40 +01:00

64 lines
1.9 KiB
C#

using Microsoft.AspNetCore.Mvc;
using Webzine.Repository.Contracts;
using Webzine.WebApplication.ViewModels.Recherche;
using Webzine.WebApplication.ViewModels.Titre;
namespace Webzine.WebApplication.Controllers;
[Route("recherche")]
public class RechercheController : Controller
{
private readonly ILogger<RechercheController> _logger;
private readonly ITitreRepository _titreRepository;
public RechercheController(ILogger<RechercheController> logger, ITitreRepository titreRepository)
{
_logger = logger;
_titreRepository = titreRepository;
}
[HttpPost("")]
public IActionResult Index(string mot)
{
_logger.LogInformation("Recherche artistes/titres pour le mot : {Mot}.", mot);
var titres = _titreRepository.Search(mot)
.Concat(_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 = _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();
var vm = new RechercheIndexViewModel
{
Mot = mot,
Artistes = artistes,
Titres = titres
};
return View(vm);
}
}