Skip to content

Commit

Permalink
Merge pull request #95 from starknet-id/feat/get-quest-participants
Browse files Browse the repository at this point in the history
feat: get quest participants
  • Loading branch information
Th0rgal authored Oct 17, 2023
2 parents 00cef0b + 46d7e71 commit b0cbbd8
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
100 changes: 100 additions & 0 deletions src/endpoints/get_quest_participants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
use crate::{models::AppState, utils::get_error};
use axum::{
extract::{Query, State},
response::IntoResponse,
Json,
};

use futures::StreamExt;
use mongodb::bson::{doc, Document};
use reqwest::StatusCode;
use serde::{Deserialize, Serialize};
use std::sync::Arc;

#[derive(Debug, Serialize, Deserialize)]

pub struct GetQuestParticipantsQuery {
quest_id: u32,
}

pub async fn handler(
State(state): State<Arc<AppState>>,
Query(query): Query<GetQuestParticipantsQuery>,
) -> impl IntoResponse {
// Convert to int
let quest_id = query.quest_id.to_string().parse::<i64>().unwrap();
let tasks_collection = state.db.collection::<Document>("tasks");
let tasks_ids = tasks_collection
.find(doc! { "quest_id": quest_id }, None)
.await
.unwrap()
.map(|task_doc| {
task_doc
.unwrap()
.get("id")
.unwrap()
.to_string()
.parse::<i64>()
.unwrap()
})
.collect::<Vec<i64>>()
.await;

let pipeline = vec![
doc! {
"$match": {
"task_id": {
"$in": tasks_ids
}
}
},
doc! {
"$group": {
"_id": "$address",
}
},
doc! {
"$facet": {
"count": [
{
"$count": "count"
}
],
"firstParticipants": [
{
"$limit": 3
}
]
}
},
doc! {
"$project": {
"count": {
"$arrayElemAt": [
"$count.count",
0
]
},
"firstParticipants": "$firstParticipants._id"
}
},
];

let completed_tasks_collection = state.db.collection::<Document>("completed_tasks");
let mut cursor = completed_tasks_collection
.aggregate(pipeline, None)
.await
.unwrap();

let mut res: Document = Document::new();
while let Some(result) = cursor.next().await {
match result {
Ok(document) => {
res = document;
}
Err(_) => return get_error("Error querying quest participants".to_string()),
}
}

return (StatusCode::OK, Json(res)).into_response();
}
1 change: 1 addition & 0 deletions src/endpoints/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod achievements;
pub mod get_completed_quests;
pub mod get_quest;
pub mod get_quest_participants;
pub mod get_quests;
pub mod get_quiz;
pub mod get_tasks;
Expand Down
4 changes: 4 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ async fn main() {
"/get_completed_quests",
get(endpoints::get_completed_quests::handler),
)
.route(
"/get_quest_participants",
get(endpoints::get_quest_participants::handler),
)
.route(
"/get_trending_quests",
get(endpoints::get_trending_quests::handler),
Expand Down

0 comments on commit b0cbbd8

Please sign in to comment.