Skip to content

Commit

Permalink
dev: add pagination in db query
Browse files Browse the repository at this point in the history
  • Loading branch information
ayushtom committed Nov 1, 2023
1 parent 80daf60 commit 4e42c69
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
31 changes: 27 additions & 4 deletions src/endpoints/leaderboard/get_ranking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
(last_index === -1 then no previous page && last_index === total.documents.length then no next page)
*/

// TODO: get paginated data

use crate::{models::AppState};
use axum::{
extract::{Query, State},
Expand Down Expand Up @@ -137,22 +135,47 @@ pub async fn handler(
doc! {
"$unwind": doc! {
"path": "$docs",
"includeArrayIndex": "rownum",

},
},
doc! {
"$addFields": {
"docs.rank": {
"$add": ["$rownum", 1],
},
},
},
doc! {
"$replaceRoot": doc! {
"newRoot": "$docs",
}
},
doc! {
"$match": doc!{
"rank":doc!{
"$gte":0,
"$lte":10
}
}
},
doc! {
"$project":{
"_id":0,
"address":"$_id",
"total_points":1,
}
}
];


match users_collection.aggregate(paginated_leaderboard_pipeline, None).await {
Ok(mut cursor) => {
let mut quest = Vec::new();
while let Some(result) = cursor.try_next().await.unwrap() {
println!("result: {}", result);
quest.push(result);
}
(StatusCode::OK, Json("ehy")).into_response()
(StatusCode::OK, Json(quest)).into_response()
}
Err(_) => get_error("Error querying quests".to_string()),
}
Expand Down
5 changes: 3 additions & 2 deletions src/endpoints/leaderboard/get_static_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub async fn get_leaderboard_toppers(
doc! {
"$match": doc! {
// Filter documents with a date field greater than or equal to one month ago
"timestamp": doc!{
"timestamp": doc! {
"$gte": time_gap
}
}
Expand All @@ -64,6 +64,8 @@ pub async fn get_leaderboard_toppers(
],
//getting the total number of users
"totalUsers": vec![doc!{ "$count": "total" }],

//getting the rank of the user
"rank": vec![
doc! {
"$group": {
Expand Down Expand Up @@ -112,7 +114,6 @@ pub async fn get_leaderboard_toppers(
"$first":"$rank.rank"

}

},
},
];
Expand Down

0 comments on commit 4e42c69

Please sign in to comment.