Skip to content

Commit

Permalink
Merge pull request #232 from starknet-id/fix/quiz-deserialization
Browse files Browse the repository at this point in the history
fix: quiz deserialization
  • Loading branch information
Th0rgal authored Jul 22, 2024
2 parents 5138e3b + b6bf787 commit 378ba72
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 28 deletions.
45 changes: 18 additions & 27 deletions src/endpoints/quests/verify_quiz.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::sync::Arc;

use crate::models::QuestTaskDocument;
use crate::{
common::verify_quiz::verify_quiz,
models::{AppState, VerifyQuizQuery},
Expand All @@ -11,7 +12,6 @@ use futures::TryStreamExt;
use mongodb::bson::doc;
use serde_json::json;
use starknet::core::types::FieldElement;
use crate::models::QuestTaskDocument;

#[route(post, "/quests/verify_quiz", crate::endpoints::quests::verify_quiz)]
pub async fn handler(
Expand All @@ -22,13 +22,11 @@ pub async fn handler(
return get_error("Please connect your wallet first".to_string());
}

let pipeline = vec![
doc! {
"$match": doc! {
"quiz_name": &body.quiz_name
}
},
];
let pipeline = vec![doc! {
"$match": doc! {
"quiz_name": &body.quiz_name
}
}];

let tasks_collection = state.db.collection::<QuestTaskDocument>("tasks");
let task_id = match tasks_collection.aggregate(pipeline, None).await {
Expand All @@ -42,25 +40,18 @@ pub async fn handler(
Err(_) => return get_error("Quiz name does not match".to_string()),
};

let user_answers_numbers: Result<Vec<Vec<usize>>, _> = body
.user_answers_list
.iter()
.map(|inner_list| {
inner_list
.iter()
.map(|s| s.parse::<usize>())
.collect::<Result<Vec<_>, _>>()
})
.collect();

match user_answers_numbers {
Ok(responses) => match verify_quiz(&state.db, body.addr, &body.quiz_name, &responses).await {
true => match state.upsert_completed_task(body.addr, task_id).await {
Ok(_) => (StatusCode::OK, Json(json!({"res": true}))).into_response(),
Err(e) => get_error(format!("{}", e)),
},
false => get_error("Incorrect answers".to_string()),
match verify_quiz(
&state.db,
body.addr,
&body.quiz_name,
&body.user_answers_list,
)
.await
{
true => match state.upsert_completed_task(body.addr, task_id).await {
Ok(_) => (StatusCode::OK, Json(json!({"res": true}))).into_response(),
Err(e) => get_error(format!("{}", e)),
},
Err(e) => get_error(format!("{}", e)),
false => get_error("Incorrect answers".to_string()),
}
}
2 changes: 1 addition & 1 deletion src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ pub_struct!(Deserialize; EmailQuery {
pub_struct!(Deserialize; VerifyQuizQuery {
addr: FieldElement,
quiz_name: i64,
user_answers_list: Vec<Vec<String>>,
user_answers_list: Vec<Vec<usize>>,
});

pub_struct!(Deserialize; VerifyBalanceQuery {
Expand Down

0 comments on commit 378ba72

Please sign in to comment.