-
Notifications
You must be signed in to change notification settings - Fork 27
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: add additional boost endpoints #142
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,52 @@ | ||
use crate::{ | ||
models::{AppState, QuestDocument}, | ||
utils::get_error, | ||
}; | ||
use axum::{ | ||
extract::{Query, State}, | ||
http::StatusCode, | ||
response::{IntoResponse, Json}, | ||
}; | ||
use futures::StreamExt; | ||
use mongodb::bson::doc; | ||
use serde::Deserialize; | ||
use std::sync::Arc; | ||
|
||
#[derive(Deserialize)] | ||
pub struct GetQuestsQuery { | ||
id: u32, | ||
} | ||
|
||
pub async fn handler( | ||
State(state): State<Arc<AppState>>, | ||
Query(query): Query<GetQuestsQuery>, | ||
) -> impl IntoResponse { | ||
let collection = state.db.collection::<QuestDocument>("boosts"); | ||
let pipeline = [ | ||
doc! { | ||
"$match": { | ||
"id": query.id | ||
} | ||
}, | ||
doc! { | ||
"$project":{ | ||
"_id":0 | ||
} | ||
}, | ||
]; | ||
|
||
match collection.aggregate(pipeline, None).await { | ||
Ok(mut cursor) => { | ||
while let Some(result) = cursor.next().await { | ||
match result { | ||
Ok(document) => { | ||
return (StatusCode::OK, Json(document)).into_response(); | ||
} | ||
_ => continue, | ||
} | ||
} | ||
get_error("Quest not found".to_string()) | ||
} | ||
Err(_) => get_error("Error querying quest".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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use crate::models::BoostTable; | ||
use crate::{models::AppState, utils::get_error}; | ||
use axum::{ | ||
extract::State, | ||
http::StatusCode, | ||
response::{IntoResponse, Json}, | ||
}; | ||
use chrono::Utc; | ||
use futures::StreamExt; | ||
use mongodb::bson::doc; | ||
use mongodb::bson::from_document; | ||
use std::sync::Arc; | ||
|
||
pub async fn handler(State(state): State<Arc<AppState>>) -> impl IntoResponse { | ||
let pipeline = vec![doc! { | ||
"$match": { | ||
"expiry":{ | ||
"$gt": Utc::now().timestamp_millis() | ||
}, | ||
"winner": { | ||
"$eq": null, | ||
}, | ||
} | ||
}]; | ||
let collection = state.db.collection::<BoostTable>("boosts"); | ||
match collection.aggregate(pipeline, None).await { | ||
Ok(mut cursor) => { | ||
let mut quests: Vec<BoostTable> = Vec::new(); | ||
while let Some(result) = cursor.next().await { | ||
match result { | ||
Ok(document) => { | ||
quests.push(from_document(document).unwrap()); | ||
} | ||
_ => continue, | ||
} | ||
} | ||
if quests.len() == 0 { | ||
return get_error("No boosts found".to_string()); | ||
} | ||
(StatusCode::OK, Json(quests)).into_response() | ||
} | ||
Err(_) => get_error("Error querying boosts".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 |
---|---|---|
@@ -0,0 +1,126 @@ | ||
use crate::models::{BoostTable, QuestDocument}; | ||
use crate::{models::AppState, utils::get_error}; | ||
use axum::extract::Query; | ||
use axum::{ | ||
extract::State, | ||
http::StatusCode, | ||
response::{IntoResponse, Json}, | ||
}; | ||
use futures::{StreamExt}; | ||
use mongodb::bson::doc; | ||
use mongodb::bson::from_document; | ||
use serde::{Deserialize, Serialize}; | ||
use std::sync::Arc; | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct GetQuestForBoostQuery { | ||
boost_id: u32, | ||
} | ||
|
||
pub async fn handler( | ||
State(state): State<Arc<AppState>>, | ||
Query(query): Query<GetQuestForBoostQuery>, | ||
) -> impl IntoResponse { | ||
let boost_id = query.boost_id; | ||
let pipeline = vec![ | ||
doc! { | ||
"$match": doc! { | ||
"id": boost_id | ||
} | ||
}, | ||
doc! { | ||
"$unwind": doc! { | ||
"path": "$quests" | ||
} | ||
}, | ||
doc! { | ||
"$lookup": doc! { | ||
"from": "quests", | ||
"let": doc! { | ||
"task_id": "$quests" | ||
}, | ||
"pipeline": [ | ||
doc! { | ||
"$match": doc! { | ||
"$expr": doc! { | ||
"$eq": [ | ||
"$id", | ||
"$$task_id" | ||
] | ||
} | ||
} | ||
} | ||
], | ||
"as": "quest" | ||
} | ||
}, | ||
doc! { | ||
"$group": doc! { | ||
"_id": 0, | ||
"quest_list": doc! { | ||
"$push": doc! { | ||
"$arrayElemAt": [ | ||
"$quest", | ||
0 | ||
] | ||
} | ||
} | ||
} | ||
}, | ||
doc! { | ||
"$project": doc! { | ||
"_id": 0 | ||
} | ||
}, | ||
doc! { | ||
"$project": doc! { | ||
"quests": doc! { | ||
"$map": doc! { | ||
"input": "$quest_list", | ||
"as": "item", | ||
"in": doc! { | ||
"id": "$$item.id", | ||
"category": "$$item.category", | ||
"name": "$$item.name", | ||
"desc": "$$item.desc", | ||
"issuer": "$$item.issuer", | ||
"logo": "$$item.logo", | ||
"rewards_img": "$$item.rewards_img", | ||
"rewards_title": "$$item.rewards_title", | ||
"img_card": "$$item.img_card", | ||
"title_card": "$$item.title_card", | ||
"hidden": "$$item.hidden", | ||
"disabled": "$$item.disabled", | ||
"expiry": "$$item.expiry", | ||
"expiry_timestamp": "$$item.expiry_timestamp", | ||
"mandatory_domain": "$$item.mandatory_domain", | ||
"expired": "$$item.expired", | ||
"experience": "$$item.experience", | ||
"rewards_title": "$$item.rewards_title", | ||
"rewards_nfts": "$$item.rewards_nfts", | ||
"rewards_description": "$$item.rewards_description", | ||
"rewards_endpoint": "$$item.rewards_endpoint", | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
]; | ||
let collection = state.db.collection::<BoostTable>("boosts"); | ||
match collection.aggregate(pipeline, None).await { | ||
Ok(mut cursor) => { | ||
let mut res: Vec<QuestDocument> = Vec::new(); | ||
while let Some(result) = cursor.next().await { | ||
let document = result.unwrap(); | ||
let quest_list = document.get_array("quests").unwrap(); | ||
for quest in quest_list { | ||
let quest_doc = quest.as_document().unwrap(); | ||
let quest = from_document::<QuestDocument>(quest_doc.clone()).unwrap(); | ||
res.push(quest); | ||
} | ||
} | ||
(StatusCode::OK, Json(res)).into_response() | ||
} | ||
Err(_) => get_error("Error querying boosts".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 +1,4 @@ | ||
pub mod get_claim_params; | ||
pub mod get_boost; | ||
pub mod get_boosts; | ||
pub mod get_claim_params; | ||
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If those are the same, could you try to use "$mergeObjects": ["$$item"]?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done