feat: implémenter le service de tableau de bord et DTO pour les statistiques du tableau de bord
This commit is contained in:
@@ -1,9 +1,9 @@
|
|||||||
namespace Webzine.WebApplication.Areas.Administration.ViewModels;
|
namespace Webzine.Business.Contracts.Dto;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// ViewModel pour le tableau de bord de l'administration du webzine.
|
/// DTO pour le tableau de bord de l'administration du webzine.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class DashboardViewModel
|
public class DashboardDTO
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Définit le nombre total d'artistes chroniqués dans le webzine.
|
/// Définit le nombre total d'artistes chroniqués dans le webzine.
|
||||||
16
Webzine.Business.Contracts/IDashboardService.cs
Normal file
16
Webzine.Business.Contracts/IDashboardService.cs
Normal 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();
|
||||||
|
}
|
||||||
71
Webzine.Business/DashboardService.cs
Normal file
71
Webzine.Business/DashboardService.cs
Normal 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),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -21,4 +21,9 @@
|
|||||||
</Content>
|
</Content>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\Webzine.Business.Contracts\Webzine.Business.Contracts.csproj" />
|
||||||
|
<ProjectReference Include="..\Webzine.Repository.Contracts\Webzine.Repository.Contracts.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -51,5 +51,18 @@ namespace Webzine.Repository.Contracts
|
|||||||
/// <param name="nom">Nom de l'artiste.</param>
|
/// <param name="nom">Nom de l'artiste.</param>
|
||||||
/// <returns>IEnumarble.<Artiste> qui contient la chaine de caractere.</returns>
|
/// <returns>IEnumarble.<Artiste> qui contient la chaine de caractere.</returns>
|
||||||
IEnumerable<Artiste> Search(string nom);
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -37,5 +37,11 @@ namespace Webzine.Repository.Contracts
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="style">L'objet style à mettre à jour.</param
|
/// <param name="style">L'objet style à mettre à jour.</param
|
||||||
void Update(Style style);
|
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -176,5 +176,37 @@ namespace Webzine.Repository
|
|||||||
throw new Exception("Erreur lors de la recherche d'artiste {error}", ex);
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -178,4 +178,20 @@ public class DbStyleRepository : IStyleRepository
|
|||||||
throw;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -93,5 +93,17 @@ namespace Webzine.Repository
|
|||||||
.Where(a => a.Nom.ToLower().Contains(mot.ToLower()))
|
.Where(a => a.Nom.ToLower().Contains(mot.ToLower()))
|
||||||
.ToList();
|
.ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc/>
|
||||||
|
public int Count()
|
||||||
|
{
|
||||||
|
return this.dataStore.Artistes.Count;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc/>
|
||||||
|
public int Count(Func<Artiste, bool> predicate)
|
||||||
|
{
|
||||||
|
return this.dataStore.Artistes.Count(predicate);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -56,4 +56,10 @@ public class LocalStyleRepository : IStyleRepository
|
|||||||
{
|
{
|
||||||
throw new NotSupportedException("Mode local");
|
throw new NotSupportedException("Mode local");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc/>
|
||||||
|
public int Count()
|
||||||
|
{
|
||||||
|
return this.dataStore.Styles.Count;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,30 +1,26 @@
|
|||||||
namespace Webzine.WebApplication.Areas.Administration.Controllers;
|
namespace Webzine.WebApplication.Areas.Administration.Controllers;
|
||||||
|
|
||||||
|
using Webzine.Business.Contracts;
|
||||||
|
using Webzine.Business.Contracts.Dto;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
using Webzine.Repository.Contracts;
|
using Webzine.Repository.Contracts;
|
||||||
using Webzine.WebApplication.Areas.Administration.ViewModels;
|
|
||||||
|
|
||||||
[Area("Administration")]
|
[Area("Administration")]
|
||||||
public class DashboardController : Controller
|
public class DashboardController : Controller
|
||||||
{
|
{
|
||||||
private readonly ILogger<DashboardController> logger;
|
private readonly ILogger<DashboardController> logger;
|
||||||
private readonly IStyleRepository styleRepository;
|
private readonly IDashboardService dashboardService;
|
||||||
private readonly IArtisteRepository artisteRepository;
|
|
||||||
private readonly ITitreRepository titreRepository;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="DashboardController"/> class.
|
/// Initializes a new instance of the <see cref="DashboardController"/> class.
|
||||||
/// Initialise une nouvelle instance de la classe <see cref="DashboardController"/>.
|
/// Initialise une nouvelle instance de la classe <see cref="DashboardController"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="logger">Service de journalisation injecté.</param>
|
/// <param name="logger">Service de journalisation injecté.</param>
|
||||||
/// <param name="styleRepository">Repository des styles injecté.</param>
|
/// <param name="dashboardService">Service de calcul des statistiques du tableau de bord.</param>
|
||||||
public DashboardController(ILogger<DashboardController> logger, IStyleRepository styleRepository, IArtisteRepository artisteRepository, ITitreRepository titreRepository)
|
public DashboardController(ILogger<DashboardController> logger, IDashboardService dashboardService)
|
||||||
{
|
{
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
this.styleRepository = styleRepository;
|
this.dashboardService = dashboardService;
|
||||||
this.artisteRepository = artisteRepository;
|
|
||||||
this.titreRepository = titreRepository;
|
|
||||||
|
|
||||||
this.logger.LogInformation("Initialisation du contrôleur TitreController.");
|
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>
|
/// <returns>La vue Index du tableau de bord.</returns>
|
||||||
public IActionResult Index()
|
public IActionResult Index()
|
||||||
{
|
{
|
||||||
var artisteLePlusChronique = this.titreRepository.FindAll()
|
DashboardDTO data = dashboardService.GetDashboardData();
|
||||||
.GroupBy(t => t.Artiste)
|
|
||||||
.OrderByDescending(g => g.Count())
|
|
||||||
.First();
|
|
||||||
|
|
||||||
var albumLePlusChronique = this.titreRepository.FindAll()
|
return this.View(data);
|
||||||
.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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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>
|
<h1 class="mb-4">Tableau de bord</h1>
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,8 @@ using Microsoft.EntityFrameworkCore;
|
|||||||
using NLog;
|
using NLog;
|
||||||
using NLog.Web;
|
using NLog.Web;
|
||||||
|
|
||||||
|
using Webzine.Business;
|
||||||
|
using Webzine.Business.Contracts;
|
||||||
using Webzine.EntitiesContext;
|
using Webzine.EntitiesContext;
|
||||||
using Webzine.Entity;
|
using Webzine.Entity;
|
||||||
using Webzine.Entity.Fixtures;
|
using Webzine.Entity.Fixtures;
|
||||||
@@ -67,6 +69,8 @@ try
|
|||||||
builder.Services.AddSingleton<InMemoryDataStore>();
|
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
|
// 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.
|
// 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();
|
builder.Services.AddResponseCompression();
|
||||||
@@ -78,6 +82,8 @@ try
|
|||||||
using (var scope = app.Services.CreateScope())
|
using (var scope = app.Services.CreateScope())
|
||||||
{
|
{
|
||||||
var db = scope.ServiceProvider.GetRequiredService<WebzineDbContext>();
|
var db = scope.ServiceProvider.GetRequiredService<WebzineDbContext>();
|
||||||
|
db.Database.EnsureCreated();
|
||||||
|
|
||||||
if (shouldSeed)
|
if (shouldSeed)
|
||||||
{
|
{
|
||||||
db.Database.EnsureDeleted();
|
db.Database.EnsureDeleted();
|
||||||
|
|||||||
@@ -34,6 +34,8 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<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.EntitiesContext\Webzine.EntitiesContext.csproj" />
|
||||||
<ProjectReference Include="..\Webzine.Entity\Webzine.Entity.csproj" />
|
<ProjectReference Include="..\Webzine.Entity\Webzine.Entity.csproj" />
|
||||||
<ProjectReference Include="..\Webzine.Repository\Webzine.Repository.csproj" />
|
<ProjectReference Include="..\Webzine.Repository\Webzine.Repository.csproj" />
|
||||||
|
|||||||
Reference in New Issue
Block a user