70 lines
2.5 KiB
C#
70 lines
2.5 KiB
C#
namespace Webzine.Business;
|
|
|
|
using Webzine.Business.Contracts;
|
|
using Webzine.Business.Contracts.Dto;
|
|
using Webzine.Entity;
|
|
using Webzine.Repository.Contracts;
|
|
|
|
/// <summary>
|
|
/// Implémentation de <see cref="IDashboardService"/>.
|
|
/// Orchestre plusieurs appels aux repositories pour produire les statistiques du tableau de bord.
|
|
/// </summary>
|
|
public class DashboardService : IDashboardService
|
|
{
|
|
private readonly IArtisteRepository artisteRepository;
|
|
private readonly ITitreRepository titreRepository;
|
|
private readonly IStyleRepository styleRepository;
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="DashboardService"/> class.
|
|
/// </summary>
|
|
/// <param name="artisteRepository">Repository des artistes.</param>
|
|
/// <param name="titreRepository">Repository des titres.</param>
|
|
/// <param name="styleRepository">Repository des styles.</param>
|
|
public DashboardService(
|
|
IArtisteRepository artisteRepository,
|
|
ITitreRepository titreRepository,
|
|
IStyleRepository styleRepository)
|
|
{
|
|
this.artisteRepository = artisteRepository;
|
|
this.titreRepository = titreRepository;
|
|
this.styleRepository = styleRepository;
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public DashboardDTO GetDashboardData()
|
|
{
|
|
IEnumerable<Titre> titres = this.titreRepository.FindAll();
|
|
|
|
Artiste? artisteLePlusChronique = titres
|
|
.GroupBy(t => t.Artiste)
|
|
.OrderByDescending(g => g.Count())
|
|
.FirstOrDefault()
|
|
?.Key;
|
|
|
|
Artiste? albumLePlusChronique = titres
|
|
.GroupBy(t => (t.Artiste, t.Album))
|
|
.GroupBy(g => g.Key.Artiste)
|
|
.OrderByDescending(g => g.Count())
|
|
.FirstOrDefault()
|
|
?.Key;
|
|
|
|
Titre? musiqueLaPlusJouee = titres
|
|
.OrderByDescending(t => t.NbLectures)
|
|
.FirstOrDefault();
|
|
|
|
return new DashboardDTO
|
|
{
|
|
NombreArtistes = this.artisteRepository.Count(),
|
|
ArtisteLePlusChronique = artisteLePlusChronique.Nom,
|
|
AlbumLePlusChronique = albumLePlusChronique.Nom,
|
|
NombreBiographies = this.artisteRepository.Count(a => !string.IsNullOrEmpty(a.Biographie)),
|
|
IdMusiqueLaPlusJouee = musiqueLaPlusJouee.IdTitre,
|
|
MusiqueLaPlusJouee = musiqueLaPlusJouee.Libelle,
|
|
NombreTitres = this.titreRepository.Count(),
|
|
NombreGenres = this.styleRepository.Count(),
|
|
NombreLectures = titres.Sum(t => t.NbLectures),
|
|
NombreLikes = titres.Sum(t => t.NbLikes),
|
|
};
|
|
}
|
|
} |