Files
webzine/Webzine.WebApplication/Controllers/RechercheController.cs
josephine.vetu f193aa47e1 add TODO
2026-03-31 13:54:39 +02:00

69 lines
2.4 KiB
C#

// <copyright file="RechercheController.cs" company="Equipe 1 - BOBIN, MASI, NODON, VETU">
// Copyright (c) Equipe 1 - BOBIN, MASI, NODON, VETU. 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 // TODO à modifier intégralement et à commenter
{
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)) // TODO OrdinalIgnoreCase pas bien
.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);
}
}
}