-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding paginate and simplePagination
- Loading branch information
1 parent
967150d
commit 3333935
Showing
5 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
class PaginationResult { | ||
final int total; | ||
final int perPage; | ||
final int page; | ||
final int lastPage; | ||
final int? previousPage; | ||
final int? nextPage; | ||
final String nextLink; | ||
final String previousLink; | ||
final String lastLink; | ||
final String firstLink; | ||
final List<dynamic> data; | ||
|
||
PaginationResult({ | ||
required this.total, | ||
required this.perPage, | ||
required this.page, | ||
required this.lastPage, | ||
required this.previousPage, | ||
required this.nextPage, | ||
required this.nextLink, | ||
required this.previousLink, | ||
required this.lastLink, | ||
required this.firstLink, | ||
required this.data, | ||
}); | ||
|
||
Map response() => { | ||
'total': total, | ||
'perPage': perPage, | ||
'page': page, | ||
'lastPage': lastPage, | ||
'previousPage': previousPage, | ||
'nextPage': nextPage, | ||
'nextLink': nextLink, | ||
'previousLink': previousLink, | ||
'lastLink': lastLink, | ||
'firstLink': firstLink, | ||
'data': data, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
class SimplePaginationResult { | ||
final int? next; | ||
final int? previous; | ||
final int last; | ||
final int first; | ||
final int total; | ||
final List<dynamic> data; | ||
|
||
SimplePaginationResult({ | ||
required this.next, | ||
required this.previous, | ||
required this.last, | ||
this.first = 1, | ||
required this.total, | ||
required this.data, | ||
}); | ||
|
||
Map response() => { | ||
'next': next, | ||
'previous': previous, | ||
'last': last, | ||
'first': first, | ||
'total': total, | ||
'data': data, // Serialize data objects to JSON | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import 'package:vania/src/database/pagination.dart'; | ||
import 'package:vania/vania.dart'; | ||
|
||
extension Pagination on QueryBuilder { | ||
Future<Map> paginate([int perPage = 15, int page = 1]) async { | ||
final String basePath = Config().get('url'); | ||
final total = await count(); | ||
final lastPage = (total / perPage).ceil(); | ||
final data = await limit(perPage).offset((page - 1) * perPage).get(); | ||
|
||
return PaginationResult( | ||
total: total, | ||
perPage: perPage, | ||
page: page, | ||
lastPage: lastPage, | ||
previousPage: page > 1 ? page - 1 : null, | ||
nextPage: page < lastPage ? page + 1 : null, | ||
nextLink: page < lastPage ? '$basePath?page=${page + 1}' : '', | ||
previousLink: page > 1 ? '$basePath?page=${page - 1}' : '', | ||
lastLink: '$basePath?page=$lastPage', | ||
firstLink: '$basePath?page=1', | ||
data: data, | ||
).response(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import 'package:vania/src/database/simple_pagination.dart'; | ||
import 'package:vania/vania.dart'; | ||
|
||
extension SimplePagination on QueryBuilder { | ||
Future<Map> simplePagination([int perPage = 15, int page = 1]) async { | ||
final total = await count(); | ||
final lastPage = (total / perPage).ceil(); | ||
final data = await limit(perPage).offset((page - 1) * perPage).get(); | ||
|
||
return SimplePaginationResult( | ||
next: page < lastPage ? page + 1 : null, | ||
previous: page > 1 ? page - 1 : null, | ||
last: lastPage, | ||
total: total, | ||
data: data, | ||
).response(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters