-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c2efd57
commit 81ddcc7
Showing
3 changed files
with
96 additions
and
1 deletion.
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,90 @@ | ||
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 GetCompletedQuestsQuery { | ||
addr: FieldElement, | ||
} | ||
|
||
pub async fn handler( | ||
State(state): State<Arc<AppState>>, | ||
Query(query): Query<GetCompletedQuestsQuery>, | ||
) -> impl IntoResponse { | ||
let address = query.addr.to_string(); | ||
println!("address: {}", address); | ||
let pipeline = vec![ | ||
doc! { | ||
"$match": doc! { | ||
"address": address | ||
} | ||
}, | ||
doc! { | ||
"$lookup": doc! { | ||
"from": "tasks", | ||
"localField": "task_id", | ||
"foreignField": "id", | ||
"as": "associatedTask" | ||
} | ||
}, | ||
doc! { | ||
"$unwind": "$associatedTask" | ||
}, | ||
doc! { | ||
"$group": doc! { | ||
"_id": "$associatedTask.quest_id", | ||
"done": doc! { | ||
"$sum": 1 | ||
} | ||
} | ||
}, | ||
doc! { | ||
"$lookup": doc! { | ||
"from": "tasks", | ||
"localField": "_id", | ||
"foreignField": "quest_id", | ||
"as": "tasks" | ||
} | ||
}, | ||
doc! { | ||
"$match": doc! { | ||
"$expr": doc! { | ||
"$eq": [ | ||
"$done", | ||
doc! { | ||
"$size": "$tasks" | ||
} | ||
] | ||
} | ||
} | ||
}, | ||
doc! { | ||
"$project": doc! { | ||
"quest_id": "$_id", | ||
"_id": 0 | ||
} | ||
}, | ||
]; | ||
let tasks_collection = state.db.collection::<Document>("completed_tasks"); | ||
match tasks_collection.aggregate(pipeline, None).await { | ||
Ok(mut cursor) => { | ||
let mut quests: Vec<Document> = Vec::new(); | ||
while let Some(result) = cursor.try_next().await.unwrap() { | ||
quests.push(result); | ||
} | ||
(StatusCode::OK, Json(quests)).into_response() | ||
} | ||
Err(_) => get_error("Error querying quests".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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
pub mod achievements; | ||
pub mod get_completed_quests; | ||
pub mod get_quest; | ||
pub mod get_quests; | ||
pub mod get_quiz; | ||
|
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