diff --git a/Webzine.Repository/DbArtisteRepository.cs b/Webzine.Repository/DbArtisteRepository.cs index de34724..0692fbb 100644 --- a/Webzine.Repository/DbArtisteRepository.cs +++ b/Webzine.Repository/DbArtisteRepository.cs @@ -217,8 +217,7 @@ namespace Webzine.Repository .AsNoTracking() .OrderBy(a => a.Nom) .Include(t => t.Titres) - .Skip(offset) - .Take(limit); + .Paginate(offset, limit); this.logger.LogDebug("Page {PageNumber} d'artistes récupérée avec {PageSize} artistes par page.", offset, limit); return artistes; } diff --git a/Webzine.Repository/DbCommentaireRepository.cs b/Webzine.Repository/DbCommentaireRepository.cs index 087fa0f..7d669b2 100644 --- a/Webzine.Repository/DbCommentaireRepository.cs +++ b/Webzine.Repository/DbCommentaireRepository.cs @@ -103,8 +103,7 @@ public class DbCommentaireRepository : ICommentaireRepository .AsNoTracking() .Include(c => c.Titre) .OrderByDescending(c => c.DateCreation) - .Skip(offset) - .Take(limit); + .Paginate(offset, limit); return commentaires; } diff --git a/Webzine.Repository/DbStyleRepository.cs b/Webzine.Repository/DbStyleRepository.cs index d831ead..5bcdf6d 100644 --- a/Webzine.Repository/DbStyleRepository.cs +++ b/Webzine.Repository/DbStyleRepository.cs @@ -191,8 +191,7 @@ public class DbStyleRepository : IStyleRepository var styles = this.context.Styles .AsNoTracking() .OrderBy(s => s.Libelle) - .Skip(offset) - .Take(limit); + .Paginate(offset, limit); this.logger.LogDebug("La liste paginée de styles a été récupérée."); return styles; diff --git a/Webzine.Repository/DbTitreRepository.cs b/Webzine.Repository/DbTitreRepository.cs index 049715c..918d543 100644 --- a/Webzine.Repository/DbTitreRepository.cs +++ b/Webzine.Repository/DbTitreRepository.cs @@ -107,8 +107,7 @@ public class DbTitreRepository : ITitreRepository .ThenBy(t => t.Libelle) .Include(t => t.Artiste) .Include(t => t.Styles) - .Skip(offset) - .Take(limit) + .Paginate(offset, limit) .AsNoTracking(); return titres; diff --git a/Webzine.Repository/LocalArtisteRepository.cs b/Webzine.Repository/LocalArtisteRepository.cs index 8443258..a1e95b9 100644 --- a/Webzine.Repository/LocalArtisteRepository.cs +++ b/Webzine.Repository/LocalArtisteRepository.cs @@ -112,8 +112,7 @@ namespace Webzine.Repository { return this.dataStore.Artistes .OrderBy(a => a.Nom) - .Skip(offset) - .Take(limit) + .Paginate(offset, limit) .ToList(); } } diff --git a/Webzine.Repository/LocalCommentaireRepository.cs b/Webzine.Repository/LocalCommentaireRepository.cs index d24a1c7..23372b5 100644 --- a/Webzine.Repository/LocalCommentaireRepository.cs +++ b/Webzine.Repository/LocalCommentaireRepository.cs @@ -63,8 +63,7 @@ namespace Webzine.Repository { return this.dataStore.Commentaires .OrderByDescending(c => c.DateCreation) - .Skip(offset) - .Take(limit); + .Paginate(offset, limit); } /// diff --git a/Webzine.Repository/LocalStyleRepository.cs b/Webzine.Repository/LocalStyleRepository.cs index 549a6c1..d064c88 100644 --- a/Webzine.Repository/LocalStyleRepository.cs +++ b/Webzine.Repository/LocalStyleRepository.cs @@ -77,8 +77,7 @@ public class LocalStyleRepository : IStyleRepository { return this.dataStore.Styles .OrderBy(s => s.Libelle) - .Skip(offset) - .Take(limit) + .Paginate(offset, limit) .ToList(); } } \ No newline at end of file diff --git a/Webzine.Repository/LocalTitreRepository.cs b/Webzine.Repository/LocalTitreRepository.cs index 5dc0355..605a5c2 100644 --- a/Webzine.Repository/LocalTitreRepository.cs +++ b/Webzine.Repository/LocalTitreRepository.cs @@ -50,8 +50,7 @@ public class LocalTitreRepository : ITitreRepository return this.dataStore.Titres .OrderByDescending(t => t.DateCreation) .ThenBy(t => t.Libelle) - .Skip(offset) - .Take(limit); + .Paginate(offset, limit); } /// diff --git a/Webzine.Repository/RepositoryPaginationExtensions.cs b/Webzine.Repository/RepositoryPaginationExtensions.cs new file mode 100644 index 0000000..add2a68 --- /dev/null +++ b/Webzine.Repository/RepositoryPaginationExtensions.cs @@ -0,0 +1,56 @@ +namespace Webzine.Repository; + +/// +/// Fournit des méthodes génériques pour paginer des collections dans la couche Repository. +/// +public static class RepositoryPaginationExtensions +{ + /// + /// Retourne une portion paginée d'une requête. + /// + /// Type des éléments paginés. + /// Source à paginer. + /// Nombre d'éléments à ignorer. + /// Nombre maximal d'éléments à retourner. + /// La requête paginée. + public static IQueryable Paginate(this IQueryable source, int offset, int limit) + { + ArgumentNullException.ThrowIfNull(source); + ValidatePaginationArguments(offset, limit); + + return source + .Skip(offset) + .Take(limit); + } + + /// + /// Retourne une portion paginée d'une collection en mémoire. + /// + /// Type des éléments paginés. + /// Source à paginer. + /// Nombre d'éléments à ignorer. + /// Nombre maximal d'éléments à retourner. + /// La collection paginée. + public static IEnumerable Paginate(this IEnumerable source, int offset, int limit) + { + ArgumentNullException.ThrowIfNull(source); + ValidatePaginationArguments(offset, limit); + + return source + .Skip(offset) + .Take(limit); + } + + private static void ValidatePaginationArguments(int offset, int limit) + { + if (offset < 0) + { + throw new ArgumentOutOfRangeException(nameof(offset), "L'offset doit être supérieur ou égal à 0."); + } + + if (limit <= 0) + { + throw new ArgumentOutOfRangeException(nameof(limit), "La limite doit être strictement supérieure à 0."); + } + } +} \ No newline at end of file diff --git a/Webzine.WebApplication/Areas/Administration/Controllers/StyleController.cs b/Webzine.WebApplication/Areas/Administration/Controllers/StyleController.cs index 51b9d80..9cb8f1b 100644 --- a/Webzine.WebApplication/Areas/Administration/Controllers/StyleController.cs +++ b/Webzine.WebApplication/Areas/Administration/Controllers/StyleController.cs @@ -1,174 +1,175 @@ -namespace Webzine.WebApplication.Areas.Administration.Controllers; - -using Microsoft.AspNetCore.Mvc; - -using ViewModels.Styles; - -using Webzine.Entity; -using Webzine.Repository.Contracts; - -/// -/// Controleur pour la gestion des styles dans l'administration du webzine. -/// -[Area("Administration")] -public class StyleController : Controller +namespace Webzine.WebApplication.Areas.Administration.Controllers { - private readonly ILogger logger; - private readonly IStyleRepository styleRepository; - private readonly IConfiguration configuration; + using Microsoft.AspNetCore.Mvc; + + using Webzine.Entity; + using Webzine.Repository.Contracts; + using Webzine.WebApplication.Areas.Administration.ViewModels.Style; + using Webzine.WebApplication.Areas.Administration.ViewModels.Styles; /// - /// Initializes a new instance of the class. + /// Controleur pour la gestion des styles dans l'administration du webzine. /// - /// Service de journalisation injecte. - /// Repository des styles injecte. - /// Service de configuration injecte pour acceder aux parametres de configuration. - public StyleController( - ILogger logger, - IStyleRepository styleRepository, - IConfiguration configuration) + [Area("Administration")] + public class StyleController : Controller { - this.logger = logger; - this.styleRepository = styleRepository; - this.configuration = configuration; + private readonly ILogger logger; + private readonly IStyleRepository styleRepository; + private readonly IConfiguration configuration; - this.logger.LogInformation("Initialisation du controleur StyleController."); - } - - /// - /// Affiche la liste des styles dans la vue Index. - /// - /// Le numero de page pour la pagination des styles (par defaut a 0). - /// La vue Index avec la liste des styles. - public IActionResult Index(int page = 0) - { - int styles_par_page = this.configuration.GetValue("Webzine:NombreDeLignesAdministration"); - IEnumerable