From b754d0104b5b7b960b055b3da23814126afa60ce Mon Sep 17 00:00:00 2001 From: Loic Masi Date: Wed, 8 Apr 2026 18:38:46 +0200 Subject: [PATCH] =?UTF-8?q?#211=20:=20Optimisation=20de=20la=20recherche?= =?UTF-8?q?=20des=20derni=C3=A8res=20chroniques=20et=20des=20titres=20les?= =?UTF-8?q?=20plus=20populaires.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ITitreRepository.cs | 14 +++++++ Webzine.Repository/DbTitreRepository.cs | 41 +++++++++++++++++++ Webzine.Repository/LocalTitreRepository.cs | 35 ++++++++++++++++ .../Controllers/AccueilController.cs | 10 ++--- 4 files changed, 94 insertions(+), 6 deletions(-) diff --git a/Webzine.Repository.Contracts/ITitreRepository.cs b/Webzine.Repository.Contracts/ITitreRepository.cs index 8c574c1..4f19c38 100644 --- a/Webzine.Repository.Contracts/ITitreRepository.cs +++ b/Webzine.Repository.Contracts/ITitreRepository.cs @@ -107,5 +107,19 @@ /// /// 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(); + + /// + /// Récupération des chroniques. + /// + /// Nombre d'éléments. + /// Limite d'éléments. + /// Liste des derniers titres chroniqués. + IEnumerable DerniereChronique(int offset, int limit); + + /// + /// Récupération du top titre liké. + /// + /// Liste de titre les plus likés. + IEnumerable TopTitre(int offset, int limit); } } \ No newline at end of file diff --git a/Webzine.Repository/DbTitreRepository.cs b/Webzine.Repository/DbTitreRepository.cs index 37ecdb0..a581fe9 100644 --- a/Webzine.Repository/DbTitreRepository.cs +++ b/Webzine.Repository/DbTitreRepository.cs @@ -409,4 +409,45 @@ public class DbTitreRepository : ITitreRepository throw; } } + + /// + public IEnumerable DerniereChronique(int offset, int limit) + { + try + { + this.logger.LogInformation("Récupération des dernières chroniques."); + + return this.context.Titres + .AsNoTracking() + .OrderByDescending(t => t.DateCreation) + .Paginate(offset, limit) + .Include(t => t.Artiste); + } + catch (Exception ex) + { + this.logger.LogError(ex, "Erreur lors de la recuperation des dernieres chroniques."); + throw; + } + } + + /// + public IEnumerable TopTitre(int offset, int limit) + { + try + { + this.logger.LogInformation("Récupération du Top Titre."); + var titres = this.context.Titres + .AsNoTracking() + .OrderByDescending(t => t.NbLikes) + .Paginate(offset, limit) + .Include(a => a.Artiste); + + return titres; + } + catch (Exception ex) + { + this.logger.LogError(ex, "Erreur lors de la récupération du Top Titre."); + throw; + } + } } \ No newline at end of file diff --git a/Webzine.Repository/LocalTitreRepository.cs b/Webzine.Repository/LocalTitreRepository.cs index 13e995e..cb9877f 100644 --- a/Webzine.Repository/LocalTitreRepository.cs +++ b/Webzine.Repository/LocalTitreRepository.cs @@ -181,4 +181,39 @@ public class LocalTitreRepository : ITitreRepository return titre == null ? null : (titre.IdTitre, titre.Libelle); } + + /// + public IEnumerable DerniereChronique(int offset, int limit) + { + try + { + return this.dataStore.Titres + .OrderByDescending(t => t.DateCreation) + .Paginate(offset, limit); + } + catch (Exception ex) + { + this.logger.LogError(ex, "Erreur lors de la recuperation des dernieres chroniques."); + throw; + } + } + + /// + public IEnumerable TopTitre(int offset, int limit) + { + try + { + this.logger.LogInformation("Récupération du Top Titre."); + var titres = this.dataStore.Titres + .OrderByDescending(t => t.NbLikes) + .Paginate(offset, limit); + + return titres; + } + catch (Exception ex) + { + this.logger.LogError(ex, "Erreur lors de la récupération du Top Titre."); + throw; + } + } } \ No newline at end of file diff --git a/Webzine.WebApplication/Controllers/AccueilController.cs b/Webzine.WebApplication/Controllers/AccueilController.cs index fd7c03e..f7832da 100644 --- a/Webzine.WebApplication/Controllers/AccueilController.cs +++ b/Webzine.WebApplication/Controllers/AccueilController.cs @@ -47,15 +47,13 @@ var totalTitres = this.titreRepository.Count(); var totalPages = (int)Math.Ceiling((double)totalTitres / derniereChronique); - var titresPagines = this.titreRepository.FindTitres(page * derniereChronique, derniereChronique); - var topTitres = this.titreRepository.FindAll() - .OrderByDescending(t => t.NbLikes) - .Take(nbTopTitres); + var titresPagines = this.titreRepository.DerniereChronique(page * derniereChronique, derniereChronique).ToList(); + var topTitres = this.titreRepository.TopTitre(nbTopTitres, nbTopTitres).ToList(); var vm = new AccueilIndexViewModel { - DerniersTitres = titresPagines.ToList(), - TopTitres = topTitres.ToList(), + DerniersTitres = titresPagines, + TopTitres = topTitres, Page = page, TotalPages = totalPages, };