Skip to content

Commit

Permalink
feat: get_completed_quests route
Browse files Browse the repository at this point in the history
  • Loading branch information
Marchand-Nicolas committed Sep 30, 2023
1 parent c2efd57 commit 81ddcc7
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 1 deletion.
90 changes: 90 additions & 0 deletions src/endpoints/get_completed_quests.rs
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()),
}
}
1 change: 1 addition & 0 deletions src/endpoints/mod.rs
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;
Expand Down
6 changes: 5 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ async fn main() {
.route("/get_quiz", get(endpoints::get_quiz::handler))
.route("/get_quest", get(endpoints::get_quest::handler))
.route("/get_quests", get(endpoints::get_quests::handler))
.route(
"/get_completed_quests",
get(endpoints::get_completed_quests::handler),
)
.route(
"/get_trending_quests",
get(endpoints::get_trending_quests::handler),
Expand Down Expand Up @@ -250,7 +254,7 @@ async fn main() {
.with_state(shared_state)
.layer(cors);

let addr = SocketAddr::from(([0, 0, 0, 0], conf.server.port));
let addr = SocketAddr::from(([127, 0, 0, 1], conf.server.port));
println!("server: listening on http://0.0.0.0:{}", conf.server.port);
axum::Server::bind(&addr)
.serve(app.into_make_service_with_connect_info::<SocketAddr>())
Expand Down

0 comments on commit 81ddcc7

Please sign in to comment.