69 lines
2.3 KiB
C#
69 lines
2.3 KiB
C#
// <copyright file="RechercheController.cs" company="PlaceholderCompany">
|
|
// Copyright (c) PlaceholderCompany. All rights reserved.
|
|
// </copyright>
|
|
|
|
namespace Webzine.WebApplication.Controllers
|
|
{
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
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;
|
|
|
|
public RechercheController(ILogger<RechercheController> logger, ITitreRepository titreRepository)
|
|
{
|
|
this.logger = logger;
|
|
this.titreRepository = titreRepository;
|
|
}
|
|
|
|
[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 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();
|
|
|
|
var vm = new RechercheIndexViewModel
|
|
{
|
|
Mot = mot,
|
|
Artistes = artistes,
|
|
Titres = titres,
|
|
};
|
|
|
|
return this.View(vm);
|
|
}
|
|
}
|
|
} |