Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improving quest routes #79

Merged
merged 2 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions src/endpoints/get_quests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use futures::TryStreamExt;
use mongodb::bson;
use mongodb::bson::doc;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;

#[derive(Debug, Serialize, Deserialize)]
Expand Down Expand Up @@ -48,9 +49,10 @@ pub async fn handler(State(state): State<Arc<AppState>>) -> impl IntoResponse {
}
]
};

match collection.find(Some(filter), None).await {
Ok(cursor) => {
let quests: Vec<QuestDocument> = cursor
let quests_temp: Vec<QuestDocument> = cursor
.map(|result| {
result.map(|mut quest: QuestDocument| {
if let Some(expiry) = &quest.expiry {
Expand All @@ -63,10 +65,21 @@ pub async fn handler(State(state): State<Arc<AppState>>) -> impl IntoResponse {
.try_collect()
.await
.unwrap_or_else(|_| vec![]);
if quests.is_empty() {
let mut res: HashMap<String, Vec<QuestDocument>> = HashMap::new();
for quest in quests_temp {
let category = quest.category.clone();
if res.contains_key(&category) {
let quests = res.get_mut(&category).unwrap();
quests.push(quest);
} else {
res.insert(category, vec![quest]);
}
}

if res.is_empty() {
get_error("No quests found".to_string())
} else {
(StatusCode::OK, Json(quests)).into_response()
(StatusCode::OK, Json(res)).into_response()
}
}
Err(_) => get_error("Error querying quests".to_string()),
Expand Down
77 changes: 77 additions & 0 deletions src/endpoints/get_trending_quests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use crate::{
models::{AppState, QuestDocument},
utils::get_error,
};
use axum::{
extract::State,
http::StatusCode,
response::{IntoResponse, Json},
};
use chrono::Utc;
use futures::StreamExt;
use futures::TryStreamExt;
use mongodb::bson;
use mongodb::bson::doc;
use serde::{Deserialize, Serialize};
use std::sync::Arc;

#[derive(Debug, Serialize, Deserialize)]
pub struct NFTItem {
img: String,
level: u32,
}

pub async fn handler(State(state): State<Arc<AppState>>) -> impl IntoResponse {
let collection = state.db.collection::<QuestDocument>("quests");
let current_timestamp = bson::DateTime::from_millis(Utc::now().timestamp_millis());
let filter = doc! {
"$and": [
{
"$or": [
{
"expiry": {
"$exists": false
}
},
{
"expiry": {
"$gt": current_timestamp
}
}
]
},
{
"disabled": false
},
{
"hidden": false
},
{
"is_trending": true
}
]
};
match collection.find(Some(filter), None).await {
Ok(cursor) => {
let quests: Vec<QuestDocument> = cursor
.map(|result| {
result.map(|mut quest: QuestDocument| {
if let Some(expiry) = &quest.expiry {
let timestamp = expiry.timestamp_millis().to_string();
quest.expiry_timestamp = Some(timestamp);
}
quest
})
})
.try_collect()
.await
.unwrap_or_else(|_| vec![]);
if quests.is_empty() {
get_error("No quests found".to_string())
} else {
(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
Expand Up @@ -3,4 +3,5 @@ pub mod get_quest;
pub mod get_quests;
pub mod get_quiz;
pub mod get_tasks;
pub mod get_trending_quests;
pub mod quests;
4 changes: 4 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,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_trending_quests",
get(endpoints::get_trending_quests::handler),
)
.route("/get_tasks", get(endpoints::get_tasks::handler))
.route(
"/quests/starknetid/claimable",
Expand Down