-
Notifications
You must be signed in to change notification settings - Fork 27
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 #170 from starknet-id/feat/get_completed_boosts-route
Feat/get completed boosts route
- Loading branch information
Showing
3 changed files
with
106 additions
and
3 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,98 @@ | ||
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(); | ||
let pipeline = vec![ | ||
// Existing pipeline to get completed quests | ||
doc! { | ||
"$lookup": doc! { | ||
"from": "tasks", | ||
"localField": "quests", | ||
"foreignField": "quest_id", // Replace 'questId' with the actual field name in your tasks collection | ||
"as": "tasks" | ||
} | ||
}, | ||
doc! { | ||
"$lookup": doc! { | ||
"from": "completed_tasks", | ||
"let": { | ||
"task_ids": { | ||
"$map": { | ||
"input": "$tasks", | ||
"as": "taskObj", | ||
"in": "$$taskObj.id" // Extract the id from each object in the tasks array | ||
} | ||
} | ||
}, | ||
"pipeline" : [ | ||
{ | ||
"$match": { | ||
"$expr": { | ||
"$and": [ | ||
{ | ||
"$in": ["$task_id", "$$task_ids"], | ||
}, | ||
{ | ||
"$eq": ["$address", address], | ||
} | ||
] | ||
} | ||
} | ||
} | ||
], | ||
"as": "completed_tasks" | ||
} | ||
}, | ||
doc! { | ||
"$match": doc! { | ||
"$expr": { | ||
"$eq": [ | ||
{ | ||
"$size": "$tasks", | ||
}, | ||
{ | ||
"$size": "$completed_tasks", | ||
}, | ||
], | ||
}, | ||
} | ||
}, | ||
doc! { | ||
"$project": { | ||
"id": 1, | ||
} | ||
}, | ||
]; | ||
let collection = state.db.collection::<Document>("boosts"); | ||
match collection.aggregate(pipeline, None).await { | ||
Ok(mut cursor) => { | ||
let mut boosts: Vec<u32> = Vec::new(); | ||
while let Some(result) = cursor.try_next().await.unwrap() { | ||
boosts.push(result.get("id").unwrap().as_i64().unwrap() as u32); | ||
} | ||
(StatusCode::OK, Json(boosts)).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,5 +1,6 @@ | ||
pub mod get_boost; | ||
pub mod get_boosts; | ||
pub mod get_claim_params; | ||
pub mod get_quests; | ||
pub mod get_completed_boosts; | ||
pub mod get_pending_claims; | ||
pub mod get_quests; |
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