diff --git a/src/endpoints/admin/quiz/create_question.rs b/src/endpoints/admin/quiz/create_question.rs index bf4ad3b..a279985 100644 --- a/src/endpoints/admin/quiz/create_question.rs +++ b/src/endpoints/admin/quiz/create_question.rs @@ -1,6 +1,6 @@ use crate::middleware::auth::auth_middleware; use crate::models::{QuestDocument, QuestTaskDocument, QuizInsertDocument, QuizQuestionDocument}; -use crate::utils::get_next_task_id; +use crate::utils::get_next_question_id; use crate::utils::verify_quest_auth; use crate::{models::AppState, utils::get_error}; use axum::{ @@ -47,7 +47,7 @@ pub async fn handler( let res = verify_quest_auth(sub, &quests_collection, &quest_id).await; if !res { - return get_error("Error creating task".to_string()); + return get_error("Error creating question".to_string()); }; // filter to get existing quiz @@ -63,16 +63,18 @@ pub async fn handler( return get_error("quiz does not exist".to_string()); } - let state_last_id = state.last_task_id.lock().await; + let mut state_last_id = state.last_question_id.lock().await; - let next_quiz_question_id = get_next_task_id(&tasks_collection, state_last_id.clone()).await; + let next_quiz_question_id = get_next_question_id(&quiz_questions_collection, state_last_id.clone()).await; + + *state_last_id = next_quiz_question_id; let new_quiz_document = QuizQuestionDocument { quiz_id: body.quiz_id.clone(), question: body.question.clone(), options: body.options.clone(), correct_answers: body.correct_answers.clone(), - id: next_quiz_question_id.into(), + id: next_quiz_question_id, kind: "text_choice".to_string(), layout: "default".to_string(), }; @@ -83,9 +85,9 @@ pub async fn handler( { Ok(_) => ( StatusCode::OK, - Json(json!({"message": "Task created successfully"})).into_response(), + Json(json!({"message": "Question created successfully"})).into_response(), ) .into_response(), - Err(_e) => return get_error("Error creating task".to_string()), + Err(_e) => return get_error("Error creating question".to_string()), }; } diff --git a/src/main.rs b/src/main.rs index 352e718..b543ae8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -55,6 +55,7 @@ async fn main() { let shared_state = Arc::new(models::AppState { last_task_id: sync::Mutex::new(0), + last_question_id: sync::Mutex::new(0), logger: logger.clone(), conf: conf.clone(), provider: JsonRpcClient::new(HttpTransport::new( diff --git a/src/models.rs b/src/models.rs index 3498230..9cdf373 100644 --- a/src/models.rs +++ b/src/models.rs @@ -12,6 +12,7 @@ use tokio::sync::Mutex; pub_struct!(;AppState { last_task_id: Mutex, + last_question_id: Mutex, conf: Config, provider: JsonRpcClient, db: Database, diff --git a/src/utils.rs b/src/utils.rs index d77155e..a1bd78a 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,6 +1,6 @@ use crate::logger::Logger; use crate::models::{ - AchievementDocument, AppState, BoostTable, CompletedTasks, LeaderboardTable, QuestDocument, QuestTaskDocument, RewardSource, UserExperience + AchievementDocument, AppState, BoostTable, CompletedTasks, LeaderboardTable, QuestDocument, QuestTaskDocument, QuizQuestionDocument, RewardSource, UserExperience }; use async_trait::async_trait; use axum::{ @@ -884,6 +884,27 @@ pub async fn get_next_task_id( } } +pub async fn get_next_question_id( + question_collection: &Collection, + last_question_id: i64, +) -> i64 { + let last_id_filter = doc! {}; + let options = FindOneOptions::builder().sort(doc! {"id": -1}).build(); + + let last_doc = question_collection + .find_one(last_id_filter, options) + .await + .unwrap(); + + if let Some(doc) = last_doc { + let db_last_id = doc.id; + + return std::cmp::max(db_last_id, last_question_id) + 1; + } else { + return last_question_id + 1; + } +} + pub async fn read_contract( state: &AppState, contract: FieldElement,