@@ -134,6 +134,57 @@ export class JsonServer {
134134 }
135135 }
136136
137+ /**
138+ * Creates pagination metadata based on request parameters
139+ *
140+ * @param total - Total number of items
141+ * @param page - Current page number
142+ * @param perPage - Items per page
143+ * @returns Pagination metadata with first, prev, next, last, pages, and items
144+ */
145+ private createPaginationMetadata (
146+ total : number ,
147+ page : number ,
148+ perPage : number
149+ ) : Record < string , any > {
150+ const totalPages = Math . ceil ( total / perPage ) ;
151+
152+ return {
153+ first : 1 ,
154+ prev : page > 1 ? page - 1 : null ,
155+ next : page < totalPages ? page + 1 : null ,
156+ last : totalPages || 1 ,
157+ pages : totalPages || 1 ,
158+ items : total ,
159+ } ;
160+ }
161+
162+ /**
163+ * Get paginated data with metadata
164+ *
165+ * @param collection - Data collection to paginate
166+ * @param page - Current page number (default: 1)
167+ * @param perPage - Items per page (default: 10)
168+ * @returns Object with pagination metadata and data
169+ */
170+ private getPaginatedData (
171+ collection : any [ ] ,
172+ page : number = 1 ,
173+ perPage : number = 10
174+ ) : Record < string , any > {
175+ const total = collection . length ;
176+ const start = ( page - 1 ) * perPage ;
177+ const end = Math . min ( start + perPage , total ) ;
178+ const data = collection . slice ( start , end ) ;
179+
180+ const paginationMeta = this . createPaginationMetadata ( total , page , perPage ) ;
181+
182+ return {
183+ ...paginationMeta ,
184+ data,
185+ } ;
186+ }
187+
137188 /**
138189 * Get paginated resources from a collection
139190 *
@@ -190,7 +241,10 @@ export class JsonServer {
190241 * @returns Boolean indicating if there are more pages to iterate
191242 */
192243 continueToIterate ( currentPage : number , pageSize : number , totalItems : number ) : boolean {
193- return currentPage * pageSize < totalItems ;
244+ // The pagination should continue if there are still more items to display
245+ const startIndex = ( currentPage - 1 ) * pageSize ;
246+ const endIndex = startIndex + pageSize ;
247+ return endIndex < totalItems ;
194248 }
195249
196250 /**
0 commit comments