Skip to content

Commit

Permalink
Merge pull request #308 from saimeunt/fix/fix-question-ids
Browse files Browse the repository at this point in the history
Fix question ids
  • Loading branch information
Marchand-Nicolas authored Oct 30, 2024
2 parents 04488f9 + fb17109 commit 0a4cb08
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
16 changes: 9 additions & 7 deletions src/endpoints/admin/quiz/create_question.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down Expand Up @@ -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
Expand All @@ -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(),
};
Expand All @@ -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()),
};
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
1 change: 1 addition & 0 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use tokio::sync::Mutex;

pub_struct!(;AppState {
last_task_id: Mutex<i64>,
last_question_id: Mutex<i64>,
conf: Config,
provider: JsonRpcClient<HttpTransport>,
db: Database,
Expand Down
23 changes: 22 additions & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down Expand Up @@ -884,6 +884,27 @@ pub async fn get_next_task_id(
}
}

pub async fn get_next_question_id(
question_collection: &Collection<QuizQuestionDocument>,
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,
Expand Down

0 comments on commit 0a4cb08

Please sign in to comment.