Skip to content

Commit

Permalink
Merge pull request #225 from starknet-id/ayush/admin-api-bug-fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Th0rgal authored Jun 19, 2024
2 parents 0b02284 + e1ef820 commit bfa1917
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 55 deletions.
3 changes: 2 additions & 1 deletion src/endpoints/admin/custom/create_custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub_struct!(Deserialize; CreateCustom {
desc: String,
cta: String,
href: String,
api: String,
});

#[route(post, "/admin/tasks/custom/create", crate::endpoints::admin::custom::create_custom)]
Expand Down Expand Up @@ -59,7 +60,7 @@ pub async fn handler(
quest_id : body.quest_id,
id: next_id,
cta: body.cta.clone(),
verify_endpoint: "/quests/verify_custom".to_string(),
verify_endpoint: body.api.clone(),
verify_endpoint_type: "default".to_string(),
task_type: Some("custom".to_string()),
discord_guild_id: None,
Expand Down
1 change: 0 additions & 1 deletion src/endpoints/admin/quest/get_quest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ pub async fn handler(
doc! {
"$project": doc! {
"_id": 0,
"hidden": 0
}
}
],
Expand Down
1 change: 0 additions & 1 deletion src/endpoints/admin/quest_boost/update_boost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ pub async fn handler(
let quest_id = res.as_ref().unwrap().quests[0];
let res = verify_quest_auth(user, &questcollection, &(quest_id as i32)).await;

println!("res: {}", res);
if !res {
return get_error("Error updating boost".to_string());
};
Expand Down
48 changes: 29 additions & 19 deletions src/endpoints/admin/quiz/create_question.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
use crate::models::{JWTClaims, QuestDocument, QuestTaskDocument, QuizInsertDocument, QuizQuestionDocument};
use crate::utils::verify_quest_auth;
use crate::{models::AppState, utils::get_error};
use axum::http::HeaderMap;
use axum::{
extract::State,
http::StatusCode,
response::{IntoResponse, Json},
};
use axum_auto_routes::route;
use mongodb::bson::{doc};
use mongodb::options::{FindOneOptions};
use jsonwebtoken::{decode, Algorithm, DecodingKey, Validation};
use mongodb::bson::doc;
use mongodb::options::FindOneOptions;
use serde::Deserialize;
use serde_json::json;
use std::sync::Arc;
use serde::Deserialize;
use crate::models::{QuestDocument, QuizInsertDocument, QuizQuestionDocument, JWTClaims};
use axum::http::HeaderMap;
use crate::utils::verify_quest_auth;
use jsonwebtoken::{Validation, Algorithm, decode, DecodingKey};


pub_struct!(Deserialize; CreateQuizQuestion {
quiz_id: i64,
Expand All @@ -23,31 +22,36 @@ pub_struct!(Deserialize; CreateQuizQuestion {
correct_answers: Vec<i64>,
});

#[route(post, "/admin/tasks/quiz/question/create", crate::endpoints::admin::quiz::create_question)]
#[route(
post,
"/admin/tasks/quiz/question/create",
crate::endpoints::admin::quiz::create_question
)]
pub async fn handler(
State(state): State<Arc<AppState>>,
headers: HeaderMap,
body: Json<CreateQuizQuestion>,
) -> impl IntoResponse {
let user = check_authorization!(headers, &state.conf.auth.secret_key.as_ref()) as String;
let quiz_collection = state.db.collection::<QuizInsertDocument>("quizzes");
let quiz_questions_collection = state.db.collection::<QuizQuestionDocument>("quiz_questions");
let quiz_questions_collection = state
.db
.collection::<QuizQuestionDocument>("quiz_questions");
let quests_collection = state.db.collection::<QuestDocument>("quests");
let tasks_collection = state.db.collection::<QuestTaskDocument>("tasks");


let pipeline = doc! {
"$match": {
"quiz_name": &body.quiz_id,
}
};
let res = &quests_collection.find_one(pipeline, None).await.unwrap();
let pipeline = doc! {
"quiz_name": &body.quiz_id,
};
let res = &tasks_collection.find_one(pipeline, None).await.unwrap();
if res.is_none() {
return get_error("quiz does not exist".to_string());
}

// get the quest id
let quest_id = res.as_ref().unwrap().id as i32;


let res = verify_quest_auth(user, &quests_collection, &quest_id).await;
if !res {
return get_error("Error creating task".to_string());
Expand All @@ -58,15 +62,21 @@ pub async fn handler(
"id": &body.quiz_id,
};

let existing_quiz = &quiz_collection.find_one(filter.clone(), None).await.unwrap();
let existing_quiz = &quiz_collection
.find_one(filter.clone(), None)
.await
.unwrap();
if existing_quiz.is_none() {
return get_error("quiz does not exist".to_string());
}

// Get the last id in increasing order
let last_id_filter = doc! {};
let options = FindOneOptions::builder().sort(doc! {"id": -1}).build();
let last_quiz_question_doc = &quiz_questions_collection.find_one(last_id_filter.clone(), options.clone()).await.unwrap();
let last_quiz_question_doc = &quiz_questions_collection
.find_one(last_id_filter.clone(), options.clone())
.await
.unwrap();

let mut next_quiz_question_id = 1;
if let Some(doc) = last_quiz_question_doc {
Expand Down
1 change: 0 additions & 1 deletion src/endpoints/admin/quiz/create_quiz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ pub async fn handler(
task_type: Some("quiz".to_string()),
discord_guild_id: None,
verify_redirect: None,

};

return match tasks_collection
Expand Down
2 changes: 1 addition & 1 deletion src/endpoints/admin/quiz/get_quiz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub async fn handler(
_ => continue,
}
}
get_error("NFT Uri not found".to_string())
get_error("Quiz not found".to_string())
}
Err(_) => get_error("Error querying quest".to_string()),
}
Expand Down
58 changes: 36 additions & 22 deletions src/endpoints/admin/quiz/update_question.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,35 @@
use crate::models::{
JWTClaims, QuestDocument, QuestTaskDocument, QuizInsertDocument, QuizQuestionDocument,
};
use crate::utils::verify_quest_auth;
use crate::{models::AppState, utils::get_error};
use axum::http::HeaderMap;
use axum::{
extract::State,
http::StatusCode,
response::{IntoResponse, Json},
};
use axum_auto_routes::route;
use jsonwebtoken::{decode, Algorithm, DecodingKey, Validation};
use mongodb::bson::{doc, Document};
use mongodb::options::{FindOneAndUpdateOptions};
use mongodb::options::FindOneAndUpdateOptions;
use serde::Deserialize;
use serde_json::json;
use std::sync::Arc;
use serde::Deserialize;
use crate::models::{QuestDocument, QuestTaskDocument, JWTClaims};
use crate::utils::verify_quest_auth;
use axum::http::HeaderMap;
use jsonwebtoken::{Validation, Algorithm, decode, DecodingKey};


pub_struct!(Deserialize; UpdateQuiz {
id:u32,
quiz_id:u32,
question: Option<String>,
options:Option<Vec<String>>,
correct_answers: Option<String>,
correct_answers: Option<Vec<i64>>,
});

#[route(post, "/admin/tasks/quiz/question/update", crate::endpoints::admin::quiz::update_question)]
#[route(
post,
"/admin/tasks/quiz/question/update",
crate::endpoints::admin::quiz::update_question
)]
pub async fn handler(
State(state): State<Arc<AppState>>,
headers: HeaderMap,
Expand All @@ -32,35 +38,38 @@ pub async fn handler(
let user = check_authorization!(headers, &state.conf.auth.secret_key.as_ref()) as String;

let tasks_collection = state.db.collection::<QuestTaskDocument>("tasks");
let quiz_collection = state.db.collection::<QuizInsertDocument>("quizzes");

let quests_collection = state.db.collection::<QuestDocument>("quests");
let quiz_questions_collection = state
.db
.collection::<QuizQuestionDocument>("quiz_questions");

let pipeline = doc! {
"$match": {
"quiz_name": &body.id,
}
};
let res = &quests_collection.find_one(pipeline, None).await.unwrap();
"quiz_name": &body.quiz_id,
};
let res = &tasks_collection.find_one(pipeline, None).await.unwrap();
if res.is_none() {
return get_error("quiz does not exist".to_string());
}

// get the quest id
let quest_id = res.as_ref().unwrap().id as i32;


let res = verify_quest_auth(user, &quests_collection, &quest_id).await;
if res {
if !res {
return get_error("Error creating task".to_string());
};

// filter to get existing task
// filter to get existing quiz
let filter = doc! {
"id": &body.id,
"id": &body.quiz_id,
};
let existing_task = &tasks_collection.find_one(filter.clone(), None).await.unwrap();
let existing_task = &quiz_collection
.find_one(filter.clone(), None)
.await
.unwrap();

// create a quiz if it does not exist
if existing_task.is_none() {
return get_error("No quiz found".to_string());
}
Expand All @@ -77,20 +86,25 @@ pub async fn handler(
update_doc.insert("correct_answers", correct_answers);
}

let question_filter = doc! {
"id": &body.id,
};

// update question
let update = doc! {
"$set": update_doc,
};
let options = FindOneAndUpdateOptions::default();
return match tasks_collection
.find_one_and_update(filter, update, options)
return match quiz_questions_collection
.find_one_and_update(question_filter, update.clone(), options)
.await
{
Ok(_) => (
StatusCode::OK,
Json(json!({"message": "updated successfully"})),
)
.into_response(),

Err(_e) => get_error("error updating task".to_string()),
};
}
52 changes: 43 additions & 9 deletions src/endpoints/admin/quiz/update_quiz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,20 @@ use serde_json::json;
use std::sync::Arc;
use mongodb::bson::Document;
use serde::Deserialize;
use crate::models::{QuestTaskDocument,JWTClaims};
use crate::models::{QuestTaskDocument, JWTClaims, QuizInsertDocument};
use axum::http::HeaderMap;
use crate::utils::verify_task_auth;
use jsonwebtoken::{Validation,Algorithm,decode,DecodingKey};


pub_struct!(Deserialize; UpdateQuiz {
id:u32,
quiz_id:u32,
name: Option<String>,
desc: Option<String>,
help_link: Option<String>,
cta: Option<String>,
intro: Option<String>,
});

#[route(post, "/admin/tasks/quiz/update", crate::endpoints::admin::quiz::update_quiz)]
Expand All @@ -33,25 +35,54 @@ pub async fn handler(
) -> impl IntoResponse {
let user = check_authorization!(headers, &state.conf.auth.secret_key.as_ref()) as String;
let tasks_collection = state.db.collection::<QuestTaskDocument>("tasks");
let quiz_collection = state.db.collection::<QuizInsertDocument>("quizzes");


let res= verify_task_auth(user, &tasks_collection,&(body.id as i32)).await;
if !res{
return get_error("Error updating tasks".to_string());
}


// filter to get existing boost
// filter to get existing quiz
let filter = doc! {
"id": &body.id,
"id": &body.quiz_id,
};
let existing_task = &tasks_collection.find_one(filter.clone(), None).await.unwrap();
let existing_quiz = &quiz_collection.find_one(filter.clone(), None).await.unwrap();

// create a quiz if it does not exist
if existing_task.is_none() {
if existing_quiz.is_none() {
return get_error("No quiz found".to_string());
}

let mut quiz_update_doc = Document::new();

if let Some(name) = &body.name {
quiz_update_doc.insert("name", name);
}
if let Some(desc) = &body.desc {
quiz_update_doc.insert("desc", desc);
}
if let Some(cta) = &body.intro {
quiz_update_doc.insert("intro", cta);
}

// update quiz
let update = doc! {
"$set": quiz_update_doc
};
let options = FindOneAndUpdateOptions::default();
match quiz_collection
.find_one_and_update(filter, update, options)
.await
{
Ok(_) => (
StatusCode::OK,
Json(json!({"message": "updated successfully"})),
)
.into_response(),
Err(_e) => get_error("error updating task".to_string()),
};


let mut update_doc = Document::new();

Expand All @@ -68,13 +99,16 @@ pub async fn handler(
update_doc.insert("cta", cta);
}

// update boost
let update = doc! {
// update quiz
let task_update = doc! {
"$set": update_doc
};
let task_filter = doc! {
"id": &body.id,
};
let options = FindOneAndUpdateOptions::default();
return match tasks_collection
.find_one_and_update(filter, update, options)
.find_one_and_update(task_filter, task_update, options)
.await
{
Ok(_) => (
Expand Down

0 comments on commit bfa1917

Please sign in to comment.