-
Notifications
You must be signed in to change notification settings - Fork 56
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
add custom_api directory with create and update #272
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
71fcdd6
add custom_api directory with create and update
GideonBature d7b081e
add mod custom_api to admin/mod.rs and removed unused code
GideonBature 7870a4b
Merge branch 'lfglabs-dev:testnet' into testnet
GideonBature be4806f
mod for custom_url set up properly
GideonBature b51dd89
Merge branch 'lfglabs-dev:testnet' into testnet
GideonBature c282cfe
Merge branch 'testnet' of github.com:GideonBature/api.starknet.quest …
GideonBature 33a9fa6
remove unused import in custom_url
GideonBature 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
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,89 @@ | ||
use crate::models::{QuestDocument, QuestTaskDocument}; | ||
use crate::utils::verify_quest_auth; | ||
use crate::{models::AppState, utils::get_error}; | ||
use crate::middleware::auth::auth_middleware; | ||
use axum::{ | ||
extract::{Extension, State}, | ||
http::StatusCode, | ||
response::{IntoResponse, Json} | ||
}; | ||
use axum_auto_routes::route; | ||
use mongodb::bson::doc; | ||
use mongodb::options::FindOneOptions; | ||
use serde::Deserialize; | ||
use serde_json::json; | ||
use starknet::core::types::FieldElement; | ||
use std::str::FromStr; | ||
use std::sync::Arc; | ||
|
||
pub_struct!(Deserialize; CreateCustomAPI { | ||
quest_id: i64, | ||
name: String, | ||
desc: String, | ||
href: String, | ||
cta: String, | ||
api_url: String, | ||
regex: String, | ||
}); | ||
|
||
#[route(post, "/admin/tasks/custom_api/create", auth_middleware)] | ||
pub async fn handler( | ||
State(state): State<Arc<AppState>>, | ||
Extension(sub): Extension<String>, | ||
Json(body): Json<CreateCustomAPI>, | ||
) -> impl IntoResponse { | ||
let collection = state.db.collection::<QuestTaskDocument>("tasks"); | ||
// Get the last id in increasing order | ||
let last_id_filter = doc! {}; | ||
let options = FindOneOptions::builder().sort(doc! {"id": -1}).build(); | ||
let last_doc = &collection.find_one(last_id_filter, options).await.unwrap(); | ||
|
||
let quests_collection = state.db.collection::<QuestDocument>("quests"); | ||
|
||
let res = verify_quest_auth(sub, &quests_collection, &(body.quest_id as i64)).await; | ||
if !res { | ||
return get_error("Error creating task".to_string()); | ||
}; | ||
|
||
let mut next_id = 1; | ||
if let Some(doc) = last_doc { | ||
let last_id = doc.id; | ||
next_id = last_id + 1; | ||
} | ||
|
||
// Build a vector of FieldElement from the comma separated contracts string | ||
let parsed_contracts: Vec<FieldElement> = body | ||
.contracts | ||
.split(",") | ||
.map(|x| FieldElement::from_str(&x).unwrap()) | ||
.collect(); | ||
|
||
let new_document = QuestTaskDocument { | ||
name: body.name.clone(), | ||
desc: body.desc.clone(), | ||
verify_redirect: None, | ||
href: body.href.clone(), | ||
total_amount: None, | ||
quest_id: body.quest_id, | ||
id: next_id, | ||
cta: body.cta.clone(), | ||
verify_endpoint: "quests/verify_custom_api".to_string(), | ||
verify_endpoint_type: "default".to_string(), | ||
task_type: Some("custom_api".to_string()), | ||
discord_guild_id: None, | ||
quiz_name: None, | ||
contracts: None, | ||
api_url: Some(body.api_url.clone()), | ||
regex: Some(body.regex.clone()), | ||
}; | ||
|
||
// insert document to boost collection | ||
return match collection.insert_one(new_document, None).await { | ||
Ok(_) => ( | ||
StatusCode::OK, | ||
Json(json!({"message": "Task created successfully"})).into_response(), | ||
) | ||
.into_response(), | ||
Err(_e) => get_error("Error creating tasks".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,2 @@ | ||
pub mod create_custom_api; | ||
pub mod update_custom_api; | ||
Comment on lines
+1
to
+2
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here this mod.rs is setup perfectly |
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,87 @@ | ||
use crate::models::QuestTaskDocument; | ||
use crate::{models::AppState, utils::get_error}; | ||
use crate::middleware::auth::auth_middleware; | ||
use crate::utils::verify_task_auth; | ||
|
||
use axum::{ | ||
extract::{Extension, State}, | ||
http::StatusCode, | ||
response::{IntoResponse, Json}, | ||
}; | ||
use axum_auto_routes::route; | ||
use mongodb::bson::doc; | ||
use serde::Deserialize; | ||
use serde_json::json; | ||
use starknet::core::types::FieldElement; | ||
use std::str::FromStr; | ||
use std::sync::Arc; | ||
|
||
pub_struct!(Deserialize; UpdateCustomAPI { | ||
id: i64, | ||
name: Option<String>, | ||
desc: Option<String>, | ||
href: Option<String>, | ||
cta: Option<String>, | ||
api_url: Option<String>, | ||
regex: Option<String>, | ||
}); | ||
|
||
// Helper function to convert FieldElement to Bson | ||
fn field_element_to_bson(fe: &FieldElement) -> mongodb::bson::Bson { | ||
mongodb::bson::Bson::String(fe.to_string()) | ||
} | ||
|
||
#[route(post, "/admin/tasks/custom_api/update", auth_middleware)] | ||
pub async fn handler( | ||
State(state): State<Arc<AppState>>, | ||
Extension(sub): Extension<String>, | ||
Json(body): Json<UpdateCustomAPI>, | ||
) -> impl IntoResponse { | ||
let collection = state.db.collection::<QuestTaskDocument>("tasks"); | ||
|
||
let res = verify_task_auth(sub, &collection, &(body.id as i32)).await; | ||
if !res { | ||
return get_error("Error updating tasks".to_string()); | ||
} | ||
|
||
// filter to get existing quest | ||
let filter = doc! { | ||
"id": &body.id, | ||
}; | ||
|
||
let mut update_doc = doc! {}; | ||
|
||
if let Some(name) = &body.name { | ||
update_doc.insert("name", name); | ||
} | ||
if let Some(desc) = &body.desc { | ||
update_doc.insert("desc", desc); | ||
} | ||
if let Some(href) = &body.href { | ||
update_doc.insert("href", href); | ||
} | ||
if let Some(cta) = &body.cta { | ||
update_doc.insert("cta", cta); | ||
} | ||
if let Some(api_url) = &body.api_url { | ||
update_doc.insert("api_url", api_url); | ||
} | ||
if let Some(regex) = &body.regex { | ||
update_doc.insert("regex", regex); | ||
} | ||
|
||
// update quest query | ||
let update = doc! { | ||
"$set": update_doc | ||
}; | ||
|
||
// insert document to boost collection | ||
return match collection.find_one_and_update(filter, update, None).await { | ||
Ok(_) => ( | ||
StatusCode::OK, | ||
Json(json!({"message": "Task updated successfully"})).into_response(), | ||
) | ||
.into_response(), | ||
Err(_e) => get_error("Error updating tasks".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
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
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.
This part is unused