-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #141 from starknet-id/ayush/has_completed_quest
feat: add endpoint to get quest completion status
- Loading branch information
Showing
3 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
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,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()), | ||
} | ||
} |
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 |
---|---|---|
|
@@ -9,3 +9,4 @@ pub mod get_tasks; | |
pub mod get_trending_quests; | ||
pub mod quests; | ||
pub mod leaderboard; | ||
pub mod has_completed_quest; |
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