#145 : merge de la branch dev.
This commit is contained in:
70
Webzine.Business/DashboardService.cs
Normal file
70
Webzine.Business/DashboardService.cs
Normal file
@@ -0,0 +1,70 @@
|
||||
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),
|
||||
};
|
||||
}
|
||||
}
|
||||
124
Webzine.Business/TitreAdminService.cs
Normal file
124
Webzine.Business/TitreAdminService.cs
Normal file
@@ -0,0 +1,124 @@
|
||||
namespace Webzine.Business;
|
||||
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
using Webzine.Business.Contracts;
|
||||
using Webzine.Business.Contracts.Dto;
|
||||
using Webzine.Entity;
|
||||
using Webzine.Repository.Contracts;
|
||||
|
||||
/// <summary>
|
||||
/// Implémentation de <see cref="ITitreAdminService"/>.
|
||||
/// Orchestre la résolution des styles, la construction de l'entité
|
||||
/// et la délégation au repository.
|
||||
/// </summary>
|
||||
public class TitreAdminService : ITitreAdminService
|
||||
{
|
||||
private readonly ITitreRepository titreRepository;
|
||||
private readonly IArtisteRepository artisteRepository;
|
||||
private readonly IStyleRepository styleRepository;
|
||||
private readonly ILogger<TitreAdminService> logger;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="TitreAdminService"/> class.
|
||||
/// </summary>
|
||||
/// <param name="titreRepository">Repository des titres.</param>
|
||||
/// <param name="artisteRepository">Repository des artistes.</param>
|
||||
/// <param name="styleRepository">Repository des styles.</param>
|
||||
/// <param name="logger">Service de journalisation.</param>
|
||||
public TitreAdminService(
|
||||
ITitreRepository titreRepository,
|
||||
IArtisteRepository artisteRepository,
|
||||
IStyleRepository styleRepository,
|
||||
ILogger<TitreAdminService> logger)
|
||||
{
|
||||
this.titreRepository = titreRepository;
|
||||
this.artisteRepository = artisteRepository;
|
||||
this.styleRepository = styleRepository;
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void CreerTitre(TitreAdminDTO commande)
|
||||
{
|
||||
this.logger.LogInformation(
|
||||
"Création d'un nouveau titre '{Libelle}' pour l'artiste ID {IdArtiste}.",
|
||||
commande.Libelle,
|
||||
commande.IdArtiste);
|
||||
|
||||
Artiste artiste = this.artisteRepository.Find(commande.IdArtiste);
|
||||
List<Style> styles = this.ResoudreStyles(commande.Styles);
|
||||
|
||||
var titre = new Titre
|
||||
{
|
||||
IdArtiste = artiste.IdArtiste,
|
||||
Artiste = artiste,
|
||||
Libelle = commande.Libelle,
|
||||
Album = commande.Album,
|
||||
Chronique = commande.Chronique,
|
||||
DateCreation = DateTime.UtcNow,
|
||||
DateSortie = commande.DateSortie,
|
||||
Duree = commande.Duree,
|
||||
UrlJaquette = commande.UrlJaquette,
|
||||
UrlEcoute = commande.UrlEcoute ?? string.Empty,
|
||||
Styles = styles,
|
||||
Commentaires = new List<Commentaire>(),
|
||||
};
|
||||
|
||||
this.titreRepository.Add(titre);
|
||||
|
||||
this.logger.LogInformation("Titre '{Libelle}' créé avec succès (ID {IdTitre}).", titre.Libelle, titre.IdTitre);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void ModifierTitre(TitreAdminDTO commande)
|
||||
{
|
||||
this.logger.LogInformation("Modification du titre ID {Id} ('{Libelle}').", commande.Id, commande.Libelle);
|
||||
|
||||
List<Style> styles = this.ResoudreStyles(commande.Styles);
|
||||
|
||||
// On charge le titre existant pour ne pas écraser NbLectures / NbLikes
|
||||
Titre existant = this.titreRepository.Find(commande.Id);
|
||||
|
||||
existant.IdArtiste = commande.IdArtiste;
|
||||
existant.Libelle = commande.Libelle;
|
||||
existant.Album = commande.Album;
|
||||
existant.Chronique = commande.Chronique;
|
||||
existant.DateSortie = commande.DateSortie;
|
||||
existant.Duree = commande.Duree;
|
||||
existant.UrlJaquette = commande.UrlJaquette;
|
||||
existant.UrlEcoute = commande.UrlEcoute ?? string.Empty;
|
||||
|
||||
existant.Styles.Clear();
|
||||
foreach (var style in styles)
|
||||
{
|
||||
existant.Styles.Add(style);
|
||||
}
|
||||
|
||||
this.titreRepository.Update(existant);
|
||||
|
||||
this.logger.LogInformation("Titre ID {Id} modifié avec succès.", commande.Id);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Résout les entités <see cref="Style"/> à partir d'une liste d'identifiants.
|
||||
/// Les identifiants introuvables sont ignorés avec un avertissement.
|
||||
/// </summary>
|
||||
/// <param name="styleIds">Identifiants des styles sélectionnés.</param>
|
||||
/// <returns>Liste des styles résolus.</returns>
|
||||
private List<Style> ResoudreStyles(List<int> styleIds)
|
||||
{
|
||||
var styles = new List<Style>();
|
||||
|
||||
foreach (int id in styleIds)
|
||||
{
|
||||
Style style = this.styleRepository.Find(id);
|
||||
|
||||
styles.Add(style);
|
||||
}
|
||||
|
||||
this.logger.LogDebug("{NbResolus}/{NbDemandes} styles résolus avec succès.", styles.Count, styleIds.Count);
|
||||
|
||||
return styles;
|
||||
}
|
||||
}
|
||||
@@ -23,7 +23,10 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Webzine.Business.Contracts\Webzine.Business.Contracts.csproj" />
|
||||
<ProjectReference Include="..\Webzine.Entity\Webzine.Entity.csproj" />
|
||||
<ProjectReference Include="..\Webzine.Repository.Contracts\Webzine.Repository.Contracts.csproj" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="10.0.0-preview.1.25080.5" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
Reference in New Issue
Block a user