diff --git a/Webzine.Business/DashboardService.cs b/Webzine.Business/DashboardService.cs index f1b566d..f68d1d5 100644 --- a/Webzine.Business/DashboardService.cs +++ b/Webzine.Business/DashboardService.cs @@ -2,7 +2,6 @@ namespace Webzine.Business; using Webzine.Business.Contracts; using Webzine.Business.Contracts.Dto; -using Webzine.Entity; using Webzine.Repository.Contracts; /// @@ -34,37 +33,22 @@ public class DashboardService : IDashboardService /// public DashboardDTO GetDashboardData() { - IEnumerable 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(); + string artisteLePlusChronique = this.titreRepository.FindMostReviewedArtistName() ?? string.Empty; + string albumLePlusChronique = this.titreRepository.FindArtistNameWithMostReviewedAlbums() ?? string.Empty; + var musiqueLaPlusJouee = this.titreRepository.FindMostPlayedTitle(); 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, + ArtisteLePlusChronique = artisteLePlusChronique, + AlbumLePlusChronique = albumLePlusChronique, + NombreBiographies = this.artisteRepository.CountWithBiography(), + IdMusiqueLaPlusJouee = musiqueLaPlusJouee?.IdTitre ?? 0, + MusiqueLaPlusJouee = musiqueLaPlusJouee?.Libelle ?? string.Empty, NombreTitres = this.titreRepository.Count(), NombreGenres = this.styleRepository.Count(), - NombreLectures = titres.Sum(t => t.NbLectures), - NombreLikes = titres.Sum(t => t.NbLikes), + NombreLectures = this.titreRepository.CountLecture(), + NombreLikes = this.titreRepository.CountLike(), }; } } \ No newline at end of file diff --git a/Webzine.Repository.Contracts/IArtisteRepository.cs b/Webzine.Repository.Contracts/IArtisteRepository.cs index b480816..1f9637e 100644 --- a/Webzine.Repository.Contracts/IArtisteRepository.cs +++ b/Webzine.Repository.Contracts/IArtisteRepository.cs @@ -72,5 +72,11 @@ namespace Webzine.Repository.Contracts /// Le prédicat de filtrage. /// Le nombre d'artistes correspondants. int Count(Func predicate); + + /// + /// Récupère le nombre d'artistes ayant une biographie renseignée. + /// + /// Le nombre d'artistes avec biographie. + int CountWithBiography(); } } \ No newline at end of file diff --git a/Webzine.Repository.Contracts/ITitreRepository.cs b/Webzine.Repository.Contracts/ITitreRepository.cs index 92427aa..8c574c1 100644 --- a/Webzine.Repository.Contracts/ITitreRepository.cs +++ b/Webzine.Repository.Contracts/ITitreRepository.cs @@ -77,5 +77,35 @@ /// /// L'objet titre à mettre à jour. void Update(Titre titre); + + /// + /// Retourne le nombre total de likes. + /// + /// Integer. + int CountLike(); + + /// + /// Retourne le nombre total de lecture. + /// + /// Integer. + int CountLecture(); + + /// + /// Retourne le nom de l'artiste ayant le plus de titres chroniqués. + /// + /// Le nom de l'artiste le plus chroniqué, ou null si aucun titre n'existe. + string? FindMostReviewedArtistName(); + + /// + /// Retourne le nom de l'artiste ayant le plus d'albums chroniqués. + /// + /// Le nom de l'artiste concerné, ou null si aucun titre n'existe. + string? FindArtistNameWithMostReviewedAlbums(); + + /// + /// Retourne l'identifiant et le libellé du titre le plus joué. + /// + /// Un tuple contenant l'identifiant et le libellé du titre le plus joué, ou null si aucun titre n'existe. + (int IdTitre, string Libelle)? FindMostPlayedTitle(); } } \ No newline at end of file diff --git a/Webzine.Repository/DbArtisteRepository.cs b/Webzine.Repository/DbArtisteRepository.cs index 0692fbb..98bb8d2 100644 --- a/Webzine.Repository/DbArtisteRepository.cs +++ b/Webzine.Repository/DbArtisteRepository.cs @@ -208,6 +208,22 @@ namespace Webzine.Repository } } + /// + public int CountWithBiography() + { + try + { + int count = this.context.Artistes.Count(a => !string.IsNullOrEmpty(a.Biographie)); + this.logger.LogDebug("Nombre d'artistes avec biographie: {Count}", count); + return count; + } + catch (Exception ex) + { + this.logger.LogError(ex, "Erreur lors du comptage des artistes avec biographie."); + throw; + } + } + /// public IEnumerable FindArtistes(int offset, int limit) { diff --git a/Webzine.Repository/DbTitreRepository.cs b/Webzine.Repository/DbTitreRepository.cs index 4b11261..37ecdb0 100644 --- a/Webzine.Repository/DbTitreRepository.cs +++ b/Webzine.Repository/DbTitreRepository.cs @@ -313,4 +313,100 @@ public class DbTitreRepository : ITitreRepository throw; } } + + /// + public int CountLike() + { + try + { + var likes = this.context.Titres.Sum(t => t.NbLikes); + return likes; + } + catch (Exception ex) + { + this.logger.LogError(ex, "Erreur lors de la récupération des likes."); + throw; + } + } + + /// + public int CountLecture() + { + try + { + var lectures = this.context.Titres.Sum(t => t.NbLectures); + return lectures; + } + catch (Exception ex) + { + this.logger.LogError(ex, "Erreur lors de la récupération des lectures."); + throw; + } + } + + /// + public string? FindMostReviewedArtistName() + { + try + { + return this.context.Titres + .AsNoTracking() + .GroupBy(t => new { t.IdArtiste, t.Artiste.Nom }) + .OrderByDescending(g => g.Count()) + .ThenBy(g => g.Key.Nom) + .Select(g => g.Key.Nom) + .FirstOrDefault(); + } + catch (Exception ex) + { + this.logger.LogError(ex, "Erreur lors de la recherche de l'artiste le plus chroniqué."); + throw; + } + } + + /// + public string? FindArtistNameWithMostReviewedAlbums() + { + try + { + return this.context.Titres + .AsNoTracking() + .GroupBy(t => new { t.IdArtiste, t.Artiste.Nom }) + .Select(g => new + { + g.Key.Nom, + AlbumCount = g.Select(t => t.Album).Distinct().Count(), + }) + .OrderByDescending(x => x.AlbumCount) + .ThenBy(x => x.Nom) + .Select(x => x.Nom) + .FirstOrDefault(); + } + catch (Exception ex) + { + this.logger.LogError(ex, "Erreur lors de la recherche de l'artiste avec le plus d'albums chroniqués."); + throw; + } + } + + /// + public (int IdTitre, string Libelle)? FindMostPlayedTitle() + { + try + { + var result = this.context.Titres + .AsNoTracking() + .OrderByDescending(t => t.NbLectures) + .ThenBy(t => t.Libelle) + .Select(t => new { t.IdTitre, t.Libelle }) + .FirstOrDefault(); + + return result == null ? null : (result.IdTitre, result.Libelle); + } + catch (Exception ex) + { + this.logger.LogError(ex, "Erreur lors de la recherche du titre le plus joué."); + throw; + } + } } \ No newline at end of file diff --git a/Webzine.Repository/LocalArtisteRepository.cs b/Webzine.Repository/LocalArtisteRepository.cs index a1e95b9..754cb8e 100644 --- a/Webzine.Repository/LocalArtisteRepository.cs +++ b/Webzine.Repository/LocalArtisteRepository.cs @@ -107,6 +107,12 @@ namespace Webzine.Repository return this.dataStore.Artistes.Count(predicate); } + /// + public int CountWithBiography() + { + return this.dataStore.Artistes.Count(a => !string.IsNullOrEmpty(a.Biographie)); + } + /// public IEnumerable FindArtistes(int offset, int limit) { diff --git a/Webzine.Repository/LocalTitreRepository.cs b/Webzine.Repository/LocalTitreRepository.cs index 605a5c2..13e995e 100644 --- a/Webzine.Repository/LocalTitreRepository.cs +++ b/Webzine.Repository/LocalTitreRepository.cs @@ -131,4 +131,54 @@ public class LocalTitreRepository : ITitreRepository existingTitre.IdArtiste = titre.IdArtiste; existingTitre.Styles = titre.Styles; } + + /// + public int CountLike() + { + return this.dataStore.Titres.Sum(t => t.NbLikes); + } + + /// + public int CountLecture() + { + return this.dataStore.Titres.Sum(t => t.NbLectures); + } + + /// + public string? FindMostReviewedArtistName() + { + return this.dataStore.Titres + .GroupBy(t => t.Artiste) + .OrderByDescending(g => g.Count()) + .ThenBy(g => g.Key?.Nom) + .Select(g => g.Key?.Nom) + .FirstOrDefault(); + } + + /// + public string? FindArtistNameWithMostReviewedAlbums() + { + return this.dataStore.Titres + .GroupBy(t => t.Artiste) + .Select(g => new + { + ArtistName = g.Key?.Nom, + AlbumCount = g.Select(t => t.Album).Distinct().Count(), + }) + .OrderByDescending(x => x.AlbumCount) + .ThenBy(x => x.ArtistName) + .Select(x => x.ArtistName) + .FirstOrDefault(); + } + + /// + public (int IdTitre, string Libelle)? FindMostPlayedTitle() + { + Titre? titre = this.dataStore.Titres + .OrderByDescending(t => t.NbLectures) + .ThenBy(t => t.Libelle) + .FirstOrDefault(); + + return titre == null ? null : (titre.IdTitre, titre.Libelle); + } } \ No newline at end of file diff --git a/Webzine.WebApplication/Program.cs b/Webzine.WebApplication/Program.cs index 1022096..4490aad 100644 --- a/Webzine.WebApplication/Program.cs +++ b/Webzine.WebApplication/Program.cs @@ -127,7 +127,7 @@ try if (seederType == SeederType.Local) { - repo.SeedBaseDeDonnees(); + repo.SeedBaseDeDonnees(nbArtistes: 1000, nbTitres: 50000, maxStyles: 50); } else if (seederType == SeederType.Spotify) { diff --git a/Webzine.WebApplication/appsettings.Development.json b/Webzine.WebApplication/appsettings.Development.json index 9777510..cc00e7d 100644 --- a/Webzine.WebApplication/appsettings.Development.json +++ b/Webzine.WebApplication/appsettings.Development.json @@ -1,5 +1,5 @@ { - "Seeder": "Spotify", + "Seeder": "Local", "Repository": "Db", "SpotifySeeder": { "ClientId": "",