Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: get quest participants #95

Merged
merged 1 commit into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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