-
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.
- Loading branch information
Showing
7 changed files
with
278 additions
and
4 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,115 @@ | ||
use std::sync::Arc; | ||
|
||
use crate::{ | ||
models::{AchievementCategoryDocument, AchievementQuery, AppState, UserAchievements}, | ||
utils::get_error, | ||
}; | ||
use axum::{ | ||
extract::{Query, State}, | ||
http::StatusCode, | ||
response::IntoResponse, | ||
Json, | ||
}; | ||
use futures::stream::StreamExt; | ||
use mongodb::bson::{doc, from_document}; | ||
use starknet::core::types::FieldElement; | ||
|
||
pub async fn handler( | ||
State(state): State<Arc<AppState>>, | ||
Query(query): Query<AchievementQuery>, | ||
) -> impl IntoResponse { | ||
if query.addr == FieldElement::ZERO { | ||
return get_error("Please connect your wallet first".to_string()); | ||
} | ||
let addr = FieldElement::to_string(&query.addr); | ||
let achievement_categories = state | ||
.db | ||
.collection::<AchievementCategoryDocument>("achievement_categories"); | ||
let pipeline = vec![ | ||
doc! { | ||
"$lookup": { | ||
"from": "achievements", | ||
"localField": "id", | ||
"foreignField": "category_id", | ||
"as": "achievement" | ||
} | ||
}, | ||
doc! {"$unwind": "$achievement" }, | ||
doc! { | ||
"$lookup": { | ||
"from": "achieved", | ||
"let": { "achievement_id": "$achievement.id" }, | ||
"pipeline": [ | ||
{ "$match": { | ||
"$expr": { | ||
"$and": [ | ||
{ "$eq": ["$achievement_id", "$$achievement_id"] }, | ||
{ "$eq": ["$addr", addr] } | ||
] | ||
} | ||
} } | ||
], | ||
"as": "achieved" | ||
} | ||
}, | ||
doc! { | ||
"$project": { | ||
"_id": 0, | ||
"category_name": "$name", | ||
"category_desc": "$desc", | ||
"achievements": { | ||
"name": "$achievement.name", | ||
"short_desc": "$achievement.short_desc", | ||
"title": { | ||
"$cond": [ | ||
{ "$eq": [{ "$size": "$achieved" }, 0] }, | ||
"$achievement.todo_title", | ||
"$achievement.done_title" | ||
] | ||
}, | ||
"desc": { | ||
"$cond": [ | ||
{ "$eq": [{ "$size": "$achieved" }, 0] }, | ||
"$achievement.todo_desc", | ||
"$achievement.done_desc" | ||
] | ||
}, | ||
"completed": { "$ne": [{ "$size": "$achieved" }, 0] }, | ||
"verify_type": "$achievement.verify_type" | ||
} | ||
} | ||
}, | ||
doc! { | ||
"$group": { | ||
"_id": { "category_name": "$category_name", "category_desc": "$category_desc" }, | ||
"achievements": { "$push": "$achievements" } | ||
} | ||
}, | ||
doc! { | ||
"$project": { | ||
"category_name": "$_id.category_name", | ||
"category_desc": "$_id.category_desc", | ||
"achievements": 1, | ||
"_id": 0 | ||
} | ||
}, | ||
]; | ||
|
||
match achievement_categories.aggregate(pipeline, None).await { | ||
Ok(mut cursor) => { | ||
let mut achievements: Vec<UserAchievements> = Vec::new(); | ||
while let Some(result) = cursor.next().await { | ||
match result { | ||
Ok(document) => { | ||
if let Ok(achievement) = from_document::<UserAchievements>(document) { | ||
achievements.push(achievement); | ||
} | ||
} | ||
_ => continue, | ||
} | ||
} | ||
(StatusCode::OK, Json(achievements)).into_response() | ||
} | ||
Err(e) => get_error(format!("Error fetching user achievements: {}", e)), | ||
} | ||
} |
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,2 @@ | ||
pub mod fetch; | ||
pub mod verify_default; |
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,49 @@ | ||
use std::sync::Arc; | ||
|
||
use crate::{ | ||
models::{AchievedDocument, AppState, VerifyAchievementQuery}, | ||
utils::{get_error, AchievementsTrait}, | ||
}; | ||
use axum::{ | ||
extract::{Query, State}, | ||
http::StatusCode, | ||
response::IntoResponse, | ||
Json, | ||
}; | ||
use mongodb::bson::doc; | ||
use serde_json::json; | ||
use starknet::core::types::FieldElement; | ||
|
||
pub async fn handler( | ||
State(state): State<Arc<AppState>>, | ||
Query(query): Query<VerifyAchievementQuery>, | ||
) -> impl IntoResponse { | ||
let addr = query.addr; | ||
if addr == FieldElement::ZERO { | ||
return get_error("Please connect your wallet first".to_string()); | ||
} | ||
let achievement_id = query.id; | ||
let achieved_collection = state.db.collection::<AchievedDocument>("achieved"); | ||
let filter = doc! { | ||
"addr": FieldElement::to_string(&addr), | ||
"achievement_id": achievement_id | ||
}; | ||
match achieved_collection.find_one(filter, None).await { | ||
Ok(Some(_)) => (StatusCode::OK, Json(json!({"achieved": true}))).into_response(), | ||
Ok(None) => match state.get_achievement(achievement_id).await { | ||
Ok(Some(achievement)) => { | ||
// todo: add verifying logic here | ||
match state | ||
.upsert_completed_achievement(addr, achievement_id) | ||
.await | ||
{ | ||
Ok(_) => (StatusCode::OK, Json(json!({"achieved": true}))).into_response(), | ||
Err(e) => get_error(format!("{}", e)), | ||
} | ||
} | ||
Ok(None) => get_error("Achievement not found".to_string()), | ||
Err(e) => get_error(format!("Error querying achievement : {}", e)), | ||
}, | ||
Err(e) => get_error(format!("Error querying user achievement : {}", e)), | ||
} | ||
} |
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 quests; | ||
pub mod get_quiz; | ||
pub mod achievements; | ||
pub mod get_quest; | ||
pub mod get_quests; | ||
pub mod get_tasks; | ||
pub mod get_quiz; | ||
pub mod get_tasks; | ||
pub mod 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
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
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