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