Skip to content

Commit

Permalink
Merge pull request #141 from starknet-id/ayush/has_completed_quest
Browse files Browse the repository at this point in the history
feat: add endpoint to get quest completion status
  • Loading branch information
Th0rgal authored Nov 26, 2023
2 parents 5f6085f + 0e67abe commit 4c9bb9a
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 0 deletions.
106 changes: 106 additions & 0 deletions src/endpoints/has_completed_quest.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
use crate::{models::AppState, utils::get_error};
use axum::{
extract::{Query, State},
response::IntoResponse,
Json,
};

use futures::TryStreamExt;
use mongodb::bson::{doc, Document};
use reqwest::StatusCode;
use serde::{Deserialize, Serialize};
use starknet::core::types::FieldElement;
use std::sync::Arc;

#[derive(Debug, Serialize, Deserialize)]
pub struct HasCompletedQuestsQuery {
addr: FieldElement,
quest_id: u32,
}

pub async fn handler(
State(state): State<Arc<AppState>>,
Query(query): Query<HasCompletedQuestsQuery>,
) -> impl IntoResponse {
let address = query.addr.to_string();
let quest_id = query.quest_id;
let pipeline = vec![
doc! {
"$match": doc! {
"address": address,
}
},
doc! {
"$lookup": doc! {
"from": "tasks",
"localField": "task_id",
"foreignField": "id",
"as": "associatedTask"
}
},
doc! {
"$unwind": "$associatedTask"
},
doc! {
"$project": doc! {
"_id": 0,
"address": 1,
"task_id": 1,
"quest_id": "$associatedTask.quest_id"
}
},
doc! {
"$group": doc! {
"_id": "$quest_id",
"done": doc! {
"$sum": 1
}
}
},
doc! {
"$match": doc! {
"_id": quest_id,
}
},
doc! {
"$lookup": doc! {
"from": "tasks",
"localField": "_id",
"foreignField": "quest_id",
"as": "tasks"
}
},
doc! {
"$project": doc! {
"_id": 0,
"result": doc! {
"$cond": doc! {
"if": doc! {
"$eq": [
doc! {
"$size": "$tasks"
},
"$done"
]
},
"then": true,
"else": false
}
}
}
},
];
let tasks_collection = state.db.collection::<Document>("completed_tasks");
match tasks_collection.aggregate(pipeline, None).await {
Ok(cursor) => {
let mut cursor = cursor;
let mut result = false;
while let Some(doc) = cursor.try_next().await.unwrap() {
result = doc.get("result").unwrap().as_bool().unwrap();
}
let response = serde_json::json!({ "completed": result });
(StatusCode::OK, Json(response)).into_response()
}
Err(_) => get_error("Error querying status".to_string()),
}
}
1 change: 1 addition & 0 deletions src/endpoints/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ pub mod get_tasks;
pub mod get_trending_quests;
pub mod quests;
pub mod leaderboard;
pub mod has_completed_quest;
4 changes: 4 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ async fn main() {
"/get_completed_quests",
get(endpoints::get_completed_quests::handler),
)
.route(
"/has_completed_quests",
get(endpoints::has_completed_quest::handler),
)
.route(
"/get_quest_participants",
get(endpoints::get_quest_participants::handler),
Expand Down

0 comments on commit 4c9bb9a

Please sign in to comment.