-
-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Paginate sessions list, added filtering (#161)
- Loading branch information
Showing
34 changed files
with
592 additions
and
130 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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,55 @@ | ||
use poem_openapi::types::{ParseFromJSON, ToJSON}; | ||
use poem_openapi::Object; | ||
use sea_orm::{ConnectionTrait, EntityTrait, FromQueryResult, PaginatorTrait, QuerySelect, Select}; | ||
|
||
#[derive(Object)] | ||
#[oai(inline)] | ||
pub struct PaginatedResponse<T: ParseFromJSON + ToJSON + Send + Sync> { | ||
items: Vec<T>, | ||
offset: u64, | ||
total: u64, | ||
} | ||
|
||
pub struct PaginationParams { | ||
pub offset: Option<u64>, | ||
pub limit: Option<u64>, | ||
} | ||
|
||
impl<T: ParseFromJSON + ToJSON + Send + Sync> PaginatedResponse<T> { | ||
pub async fn new<E, M, C, P>( | ||
query: Select<E>, | ||
params: PaginationParams, | ||
db: &'_ C, | ||
postprocess: P, | ||
) -> poem::Result<PaginatedResponse<T>> | ||
where | ||
E: EntityTrait<Model = M>, | ||
C: ConnectionTrait, | ||
M: FromQueryResult + Sized + Send + Sync + 'static, | ||
P: FnMut(E::Model) -> T, | ||
{ | ||
let offset = params.offset.unwrap_or(0); | ||
let limit = params.limit.unwrap_or(100); | ||
|
||
let paginator = query.clone().paginate(db, limit as usize); | ||
|
||
let total = paginator | ||
.num_items() | ||
.await | ||
.map_err(poem::error::InternalServerError)? as u64; | ||
|
||
let query = query.offset(offset).limit(limit); | ||
|
||
let items = query | ||
.all(db) | ||
.await | ||
.map_err(poem::error::InternalServerError)?; | ||
|
||
let items = items.into_iter().map(postprocess).collect::<Vec<_>>(); | ||
Ok(PaginatedResponse { | ||
items, | ||
offset, | ||
total, | ||
}) | ||
} | ||
} |
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
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
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
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
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
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
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
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
Oops, something went wrong.