feat: implémenter le service de tableau de bord et DTO pour les statistiques du tableau de bord

This commit is contained in:
mirage
2026-04-01 13:12:00 +02:00
parent 727d29889a
commit e47a50e0da
14 changed files with 198 additions and 50 deletions

View File

@@ -1,9 +1,9 @@
namespace Webzine.WebApplication.Areas.Administration.ViewModels;
namespace Webzine.Business.Contracts.Dto;
/// <summary>
/// ViewModel pour le tableau de bord de l'administration du webzine.
/// DTO pour le tableau de bord de l'administration du webzine.
/// </summary>
public class DashboardViewModel
public class DashboardDTO
{
/// <summary>
/// Définit le nombre total d'artistes chroniqués dans le webzine.

View File

@@ -0,0 +1,16 @@
using Webzine.Business.Contracts.Dto;
namespace Webzine.Business.Contracts;
/// <summary>
/// Service responsable du calcul des statistiques affichées sur le tableau de bord d'administration.
/// Agrège les données provenant de plusieurs repositories pour produire un résumé cohérent.
/// </summary>
public interface IDashboardService
{
/// <summary>
/// Calcule et retourne toutes les statistiques du tableau de bord en une seule passe.
/// </summary>
/// <returns>Un <see cref="DashboardDTO"/> contenant les agrégats calculés.</returns>
DashboardDTO GetDashboardData();
}

View File

@@ -0,0 +1,71 @@
using Webzine.Business.Contracts;
using Webzine.Entity;
using Webzine.Repository.Contracts;
namespace Webzine.Business;
using Contracts.Dto;
/// <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),
};
}
}

View File

@@ -21,4 +21,9 @@
</Content>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Webzine.Business.Contracts\Webzine.Business.Contracts.csproj" />
<ProjectReference Include="..\Webzine.Repository.Contracts\Webzine.Repository.Contracts.csproj" />
</ItemGroup>
</Project>

View File

@@ -51,5 +51,18 @@ namespace Webzine.Repository.Contracts
/// <param name="nom">Nom de l'artiste.</param>
/// <returns>IEnumarble.<Artiste> qui contient la chaine de caractere.</returns>
IEnumerable<Artiste> Search(string nom);
/// <summary>
/// Récupère le nombre total d'artistes dans la collection.
/// </summary>
/// <returns>Le nombre total d'artistes.</returns>
int Count();
/// <summary>
/// Récupère le nombre d'artistes correspondant au prédicat fourni.
/// </summary>
/// <param name="predicate">Le prédicat de filtrage.</param>
/// <returns>Le nombre d'artistes correspondants.</returns>
int Count(Func<Artiste, bool> predicate);
}
}

View File

@@ -37,5 +37,11 @@ namespace Webzine.Repository.Contracts
/// </summary>
/// <param name="style">L'objet style à mettre à jour.</param
void Update(Style style);
/// <summary>
/// Récupère le nombre total de styles dans la liste des styles.
/// </summary>
/// <returns>Le nombre total de styles présents dans la liste.</returns>
int Count();
}
}

View File

@@ -176,5 +176,37 @@ namespace Webzine.Repository
throw new Exception("Erreur lors de la recherche d'artiste {error}", ex);
}
}
/// <inheritdoc/>
public int Count()
{
try
{
int count = this.context.Artistes.Count();
this.logger.LogDebug("Nombre total d'artistes dans la base: {Count}", count);
return count;
}
catch (Exception ex)
{
this.logger.LogError(ex, "Erreur lors du comptage des artistes.");
throw;
}
}
/// <inheritdoc/>
public int Count(Func<Artiste, bool> predicate)
{
try
{
int count = this.context.Artistes.Count(predicate);
this.logger.LogDebug("Nombre d'artistes (avec prédicat): {Count}", count);
return count;
}
catch (Exception ex)
{
this.logger.LogError(ex, "Erreur lors du comptage des artistes avec prédicat.");
throw;
}
}
}
}

View File

@@ -178,4 +178,20 @@ public class DbStyleRepository : IStyleRepository
throw;
}
}
/// <inheritdoc/>
public int Count()
{
try
{
int count = this.context.Styles.Count();
this.logger.LogDebug("Nombre total de styles: {Count}", count);
return count;
}
catch (Exception ex)
{
this.logger.LogError(ex, "Erreur lors du comptage des styles");
throw;
}
}
}

View File

@@ -93,5 +93,17 @@ namespace Webzine.Repository
.Where(a => a.Nom.ToLower().Contains(mot.ToLower()))
.ToList();
}
/// <inheritdoc/>
public int Count()
{
return this.dataStore.Artistes.Count;
}
/// <inheritdoc/>
public int Count(Func<Artiste, bool> predicate)
{
return this.dataStore.Artistes.Count(predicate);
}
}
}

View File

@@ -56,4 +56,10 @@ public class LocalStyleRepository : IStyleRepository
{
throw new NotSupportedException("Mode local");
}
/// <inheritdoc/>
public int Count()
{
return this.dataStore.Styles.Count;
}
}

View File

@@ -1,30 +1,26 @@
namespace Webzine.WebApplication.Areas.Administration.Controllers;
using Webzine.Business.Contracts;
using Webzine.Business.Contracts.Dto;
using Microsoft.AspNetCore.Mvc;
using Webzine.Repository.Contracts;
using Webzine.WebApplication.Areas.Administration.ViewModels;
[Area("Administration")]
public class DashboardController : Controller
{
private readonly ILogger<DashboardController> logger;
private readonly IStyleRepository styleRepository;
private readonly IArtisteRepository artisteRepository;
private readonly ITitreRepository titreRepository;
private readonly IDashboardService dashboardService;
/// <summary>
/// Initializes a new instance of the <see cref="DashboardController"/> class.
/// Initialise une nouvelle instance de la classe <see cref="DashboardController"/>.
/// </summary>
/// <param name="logger">Service de journalisation injecté.</param>
/// <param name="styleRepository">Repository des styles injecté.</param>
public DashboardController(ILogger<DashboardController> logger, IStyleRepository styleRepository, IArtisteRepository artisteRepository, ITitreRepository titreRepository)
/// <param name="dashboardService">Service de calcul des statistiques du tableau de bord.</param>
public DashboardController(ILogger<DashboardController> logger, IDashboardService dashboardService)
{
this.logger = logger;
this.styleRepository = styleRepository;
this.artisteRepository = artisteRepository;
this.titreRepository = titreRepository;
this.dashboardService = dashboardService;
this.logger.LogInformation("Initialisation du contrôleur TitreController.");
}
@@ -35,42 +31,8 @@ public class DashboardController : Controller
/// <returns>La vue Index du tableau de bord.</returns>
public IActionResult Index()
{
var artisteLePlusChronique = this.titreRepository.FindAll()
.GroupBy(t => t.Artiste)
.OrderByDescending(g => g.Count())
.First();
DashboardDTO data = dashboardService.GetDashboardData();
var albumLePlusChronique = this.titreRepository.FindAll()
.GroupBy(t => t.Artiste)
.OrderByDescending(g => g.Select(t => t.Album).Distinct().Count())
.First();
var musiqueLaPlusJouee = this.titreRepository.FindAll()
.OrderByDescending(t => t.NbLectures)
.First();
var model = new DashboardViewModel
{
NombreArtistes = this.artisteRepository.FindAll().Count(),
ArtisteLePlusChronique = artisteLePlusChronique.Key.Nom,
AlbumLePlusChronique = albumLePlusChronique.Key.Nom,
NombreBiographies = this.artisteRepository.FindAll().Count(a => !string.IsNullOrEmpty(a.Biographie)),
IdMusiqueLaPlusJouee = musiqueLaPlusJouee.IdTitre,
MusiqueLaPlusJouee = musiqueLaPlusJouee.Libelle,
NombreTitres = this.titreRepository.Count(),
NombreGenres = this.styleRepository.FindAll().Count(),
NombreLectures = this.titreRepository.FindAll().Sum(t => t.NbLectures),
NombreLikes = this.titreRepository.FindAll().Sum(t => t.NbLikes),
};
return this.View(model);
return this.View(data);
}
}

View File

@@ -1,4 +1,5 @@
@model Webzine.WebApplication.Areas.Administration.ViewModels.DashboardViewModel
@using Webzine.Business.Contracts.Dto
@model DashboardDTO
<h1 class="mb-4">Tableau de bord</h1>

View File

@@ -6,6 +6,8 @@ using Microsoft.EntityFrameworkCore;
using NLog;
using NLog.Web;
using Webzine.Business;
using Webzine.Business.Contracts;
using Webzine.EntitiesContext;
using Webzine.Entity;
using Webzine.Entity.Fixtures;
@@ -67,6 +69,8 @@ try
builder.Services.AddSingleton<InMemoryDataStore>();
}
builder.Services.AddScoped<IDashboardService, DashboardService>();
// https://learn.microsoft.com/fr-fr/aspnet/core/performance/response-compression?view=aspnetcore-10.0#configuration
// Ajoute le service de compression des réponses HTTP pour réduire la taille des données envoyées au client et améliorer les performances de l'application.
builder.Services.AddResponseCompression();
@@ -78,6 +82,8 @@ try
using (var scope = app.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<WebzineDbContext>();
db.Database.EnsureCreated();
if (shouldSeed)
{
db.Database.EnsureDeleted();

View File

@@ -34,6 +34,8 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Webzine.Business.Contracts\Webzine.Business.Contracts.csproj" />
<ProjectReference Include="..\Webzine.Business\Webzine.Business.csproj" />
<ProjectReference Include="..\Webzine.EntitiesContext\Webzine.EntitiesContext.csproj" />
<ProjectReference Include="..\Webzine.Entity\Webzine.Entity.csproj" />
<ProjectReference Include="..\Webzine.Repository\Webzine.Repository.csproj" />