From eed027d3f6bf6edc8b18b57ce52150b29ccbbaa8 Mon Sep 17 00:00:00 2001 From: mubarak23 Date: Sun, 29 Sep 2024 19:25:57 +0100 Subject: [PATCH 01/10] add new state --- src/main.rs | 1 + src/models.rs | 2 ++ 2 files changed, 3 insertions(+) diff --git a/src/main.rs b/src/main.rs index 31d4fdf..75835e4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -52,6 +52,7 @@ async fn main() { .unwrap(); let shared_state = Arc::new(models::AppState { + last_task_id: 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 c3f5190..2aedee0 100644 --- a/src/models.rs +++ b/src/models.rs @@ -6,10 +6,12 @@ use starknet::{ providers::{jsonrpc::HttpTransport, JsonRpcClient}, }; +use std::{net::SocketAddr, sync::Mutex}; use crate::endpoints::quests::uri::Attribute; use crate::{config::Config, logger::Logger}; pub_struct!(;AppState { + last_task_id: Mutex, conf: Config, provider: JsonRpcClient, db: Database, From 55ee6769a97d035dcd603c25898e30ac5f4c4af7 Mon Sep 17 00:00:00 2001 From: mubarak23 Date: Mon, 30 Sep 2024 07:05:54 +0100 Subject: [PATCH 02/10] call to get next task id inside create balance function --- src/endpoints/admin/balance/create_balance.rs | 12 ++++--- src/utils.rs | 33 +++++++++++++++++++ 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/src/endpoints/admin/balance/create_balance.rs b/src/endpoints/admin/balance/create_balance.rs index f5d2b73..6c699e5 100644 --- a/src/endpoints/admin/balance/create_balance.rs +++ b/src/endpoints/admin/balance/create_balance.rs @@ -1,6 +1,7 @@ use crate::models::{QuestDocument, QuestTaskDocument}; use crate::utils::verify_quest_auth; use crate::{models::AppState, utils::get_error}; +use crate::utils::get_next_task_id; use crate::middleware::auth::auth_middleware; use axum::{ extract::{Extension, State}, @@ -44,12 +45,13 @@ pub async fn handler( 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; - } + // let mut next_id = 1; + // if let Some(doc) = last_doc { + // let last_id = doc.id; + // next_id = last_id + 1; + // } + let next_id = get_next_task_id(&collection, state.clone()).await; // Build a vector of FieldElement from the comma separated contracts string let parsed_contracts: Vec = body .contracts diff --git a/src/utils.rs b/src/utils.rs index 39c2dd6..87d7bf6 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -16,6 +16,7 @@ use mongodb::{ bson::doc, options::UpdateOptions, results::UpdateResult, Collection, Cursor, Database, IndexModel, }; +use mongodb::options::FindOneOptions; use rand::distributions::{Distribution, Uniform}; use serde_json::json; use starknet::signers::Signer; @@ -840,3 +841,35 @@ pub fn parse_string(input: &str, address: FieldElement) -> String { result } + +pub async fn get_next_task_id( + task_collection: &Collection, + state: Arc, +) -> i32 { + let mut state_last_id =state.last_task_id.lock().unwrap(); + + let last_id_filter = doc! {}; + let options = FindOneOptions::builder().sort(doc! {"id": -1}).build(); + + let last_doc = task_collection.find_one(last_id_filter, options).await.unwrap(); + + + let mut next_id: i32 = 1; + + if let Some(doc) = last_doc { + let db_last_id = doc.id; + + + next_id = std::cmp::max(db_last_id as i32, (*state_last_id).try_into().unwrap()) + 1; + } else { + + // next_id = *state_last_id; + next_id = (*state_last_id as i32)+ 1; + } + + + *state_last_id = next_id.try_into().unwrap(); + + + next_id.into() +} \ No newline at end of file From b6316b200e0b683def1ce67da331ea4aa861c02f Mon Sep 17 00:00:00 2001 From: mubarak23 Date: Tue, 1 Oct 2024 07:36:20 +0100 Subject: [PATCH 03/10] pick specific state value --- src/endpoints/admin/balance/create_balance.rs | 6 +++++- src/utils.rs | 16 +++++++++------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/endpoints/admin/balance/create_balance.rs b/src/endpoints/admin/balance/create_balance.rs index 6c699e5..32046c8 100644 --- a/src/endpoints/admin/balance/create_balance.rs +++ b/src/endpoints/admin/balance/create_balance.rs @@ -36,6 +36,7 @@ pub async fn handler( // 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::("quests"); @@ -51,7 +52,10 @@ pub async fn handler( // next_id = last_id + 1; // } - let next_id = get_next_task_id(&collection, state.clone()).await; + let state_last_id =state.last_task_id.lock().unwrap(); + // let next_id = get_next_task_id(*last_doc.clone(), state_last_id.clone()).await; + let next_id = get_next_task_id(&collection, state_last_id.clone()).await; + // Build a vector of FieldElement from the comma separated contracts string let parsed_contracts: Vec = body .contracts diff --git a/src/utils.rs b/src/utils.rs index 87d7bf6..bd13a13 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -843,15 +843,17 @@ pub fn parse_string(input: &str, address: FieldElement) -> String { } pub async fn get_next_task_id( - task_collection: &Collection, - state: Arc, + // last_doc: Option, + task_collection: &Collection, + last_task_id: i64 + ) -> i32 { - let mut state_last_id =state.last_task_id.lock().unwrap(); + // let mut state_last_id =state.last_task_id.lock().unwrap(); let last_id_filter = doc! {}; let options = FindOneOptions::builder().sort(doc! {"id": -1}).build(); - let last_doc = task_collection.find_one(last_id_filter, options).await.unwrap(); + let last_doc = task_collection.find_one(last_id_filter, options).await.unwrap(); let mut next_id: i32 = 1; @@ -860,15 +862,15 @@ pub async fn get_next_task_id( let db_last_id = doc.id; - next_id = std::cmp::max(db_last_id as i32, (*state_last_id).try_into().unwrap()) + 1; + next_id = std::cmp::max(db_last_id as i32, (last_task_id).try_into().unwrap()) + 1; } else { // next_id = *state_last_id; - next_id = (*state_last_id as i32)+ 1; + next_id = (last_task_id as i32) + 1; } - *state_last_id = next_id.try_into().unwrap(); + // last_task_id = next_id.try_into().unwrap(); next_id.into() From 6d34add44d2a53d8b495d2ef786c5ef6816a2cae Mon Sep 17 00:00:00 2001 From: mubarak23 Date: Fri, 4 Oct 2024 09:21:43 +0100 Subject: [PATCH 04/10] using tokio mutex --- src/endpoints/admin/balance/create_balance.rs | 4 ++-- src/main.rs | 3 ++- src/models.rs | 3 ++- src/utils.rs | 1 + 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/endpoints/admin/balance/create_balance.rs b/src/endpoints/admin/balance/create_balance.rs index 32046c8..b80a47c 100644 --- a/src/endpoints/admin/balance/create_balance.rs +++ b/src/endpoints/admin/balance/create_balance.rs @@ -52,8 +52,8 @@ pub async fn handler( // next_id = last_id + 1; // } - let state_last_id =state.last_task_id.lock().unwrap(); - // let next_id = get_next_task_id(*last_doc.clone(), state_last_id.clone()).await; + let state_last_id = state.last_task_id.lock().await; + let next_id = get_next_task_id(&collection, state_last_id.clone()).await; // Build a vector of FieldElement from the comma separated contracts string diff --git a/src/main.rs b/src/main.rs index 75835e4..b903397 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,7 +15,8 @@ use reqwest::Url; use serde_derive::Serialize; use starknet::providers::{jsonrpc::HttpTransport, JsonRpcClient}; use std::{borrow::Cow, sync::Arc}; -use std::{net::SocketAddr, sync::Mutex}; +use tokio::sync::Mutex; +use std::{net::SocketAddr}; use tower_http::cors::{Any, CorsLayer}; use utils::WithState; diff --git a/src/models.rs b/src/models.rs index 2aedee0..18c0536 100644 --- a/src/models.rs +++ b/src/models.rs @@ -6,7 +6,8 @@ use starknet::{ providers::{jsonrpc::HttpTransport, JsonRpcClient}, }; -use std::{net::SocketAddr, sync::Mutex}; +use tokio::sync::Mutex; +use std::{net::SocketAddr}; use crate::endpoints::quests::uri::Attribute; use crate::{config::Config, logger::Logger}; diff --git a/src/utils.rs b/src/utils.rs index bd13a13..2931ef9 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -33,6 +33,7 @@ use std::result::Result; use std::str::FromStr; use std::{fmt::Write, sync::Arc}; use tokio::time::{sleep, Duration}; +use tokio::sync::Mutex; #[macro_export] macro_rules! pub_struct { From ad7d70a7a35a73a1df9881996d6f49bd0dced37e Mon Sep 17 00:00:00 2001 From: nicolasito1411 <60229704+Marchand-Nicolas@users.noreply.github.com> Date: Fri, 4 Oct 2024 11:35:36 +0200 Subject: [PATCH 05/10] fixing errors --- src/main.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index b903397..352e718 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,8 +4,8 @@ mod common; mod config; mod endpoints; mod logger; -mod models; mod middleware; +mod models; use crate::utils::{add_leaderboard_table, run_boosts_raffle}; use axum::{http::StatusCode, Router}; @@ -14,9 +14,10 @@ use mongodb::{bson::doc, options::ClientOptions, Client}; use reqwest::Url; use serde_derive::Serialize; use starknet::providers::{jsonrpc::HttpTransport, JsonRpcClient}; +use std::net::SocketAddr; +use std::sync::Mutex; use std::{borrow::Cow, sync::Arc}; -use tokio::sync::Mutex; -use std::{net::SocketAddr}; +use tokio::sync; use tower_http::cors::{Any, CorsLayer}; use utils::WithState; @@ -53,7 +54,7 @@ async fn main() { .unwrap(); let shared_state = Arc::new(models::AppState { - last_task_id: Mutex::new(0), + last_task_id: sync::Mutex::new(0), logger: logger.clone(), conf: conf.clone(), provider: JsonRpcClient::new(HttpTransport::new( From 97a9db34410d6a5c2add7e0ffbfec234bf88563e Mon Sep 17 00:00:00 2001 From: mubarak23 Date: Fri, 4 Oct 2024 11:25:59 +0100 Subject: [PATCH 06/10] generate unique next_id --- src/endpoints/admin/balance/create_balance.rs | 10 +++--- .../admin/contract/create_contract.rs | 15 ++++---- src/endpoints/admin/custom/create_custom.rs | 20 ++++++----- .../admin/custom_api/create_custom_api.rs | 13 ++++--- src/endpoints/admin/discord/create_discord.rs | 15 ++++---- src/endpoints/admin/domain/create_domain.rs | 13 ++++--- src/endpoints/admin/nft_uri/create_uri.rs | 18 +++++----- .../admin/quest_boost/create_boost.rs | 16 ++++----- src/endpoints/admin/quiz/create_question.rs | 17 +++++----- .../admin/twitter/create_twitter_fw.rs | 13 +++---- src/models.rs | 4 +-- src/utils.rs | 34 +++++++++---------- 12 files changed, 94 insertions(+), 94 deletions(-) diff --git a/src/endpoints/admin/balance/create_balance.rs b/src/endpoints/admin/balance/create_balance.rs index b80a47c..04f2850 100644 --- a/src/endpoints/admin/balance/create_balance.rs +++ b/src/endpoints/admin/balance/create_balance.rs @@ -1,12 +1,12 @@ +use crate::middleware::auth::auth_middleware; use crate::models::{QuestDocument, QuestTaskDocument}; +use crate::utils::get_next_task_id; use crate::utils::verify_quest_auth; use crate::{models::AppState, utils::get_error}; -use crate::utils::get_next_task_id; -use crate::middleware::auth::auth_middleware; use axum::{ extract::{Extension, State}, http::StatusCode, - response::{IntoResponse, Json} + response::{IntoResponse, Json}, }; use axum_auto_routes::route; use mongodb::bson::doc; @@ -52,8 +52,8 @@ pub async fn handler( // next_id = last_id + 1; // } - let state_last_id = state.last_task_id.lock().await; - + let state_last_id = state.last_task_id.lock().await; + let next_id = get_next_task_id(&collection, state_last_id.clone()).await; // Build a vector of FieldElement from the comma separated contracts string diff --git a/src/endpoints/admin/contract/create_contract.rs b/src/endpoints/admin/contract/create_contract.rs index a31b679..7b0785b 100644 --- a/src/endpoints/admin/contract/create_contract.rs +++ b/src/endpoints/admin/contract/create_contract.rs @@ -1,11 +1,12 @@ -use crate::models::{QuestDocument, QuestTaskDocument, Call}; +use crate::middleware::auth::auth_middleware; +use crate::models::{Call, QuestDocument, QuestTaskDocument}; +use crate::utils::get_next_task_id; 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} + response::{IntoResponse, Json}, }; use axum_auto_routes::route; use mongodb::bson::doc; @@ -42,11 +43,9 @@ pub async fn handler( 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; - } + let state_last_id = state.last_task_id.lock().await; + + let next_id = get_next_task_id(&collection, state_last_id.clone()).await; let new_document = QuestTaskDocument { name: body.name.clone(), diff --git a/src/endpoints/admin/custom/create_custom.rs b/src/endpoints/admin/custom/create_custom.rs index f88c01e..85672c1 100644 --- a/src/endpoints/admin/custom/create_custom.rs +++ b/src/endpoints/admin/custom/create_custom.rs @@ -1,9 +1,10 @@ +use crate::middleware::auth::auth_middleware; use crate::models::{QuestDocument, QuestTaskDocument}; +use crate::utils::get_next_task_id; use crate::utils::verify_quest_auth; use crate::{models::AppState, utils::get_error}; -use crate::middleware::auth::auth_middleware; use axum::{ - extract::{State, Extension}, + extract::{Extension, State}, http::StatusCode, response::{IntoResponse, Json}, }; @@ -41,13 +42,16 @@ pub async fn handler( 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; - } + // let mut next_id = 1; + // if let Some(doc) = last_doc { + // let last_id = doc.id; + // next_id = last_id + 1; + // } + + let state_last_id = state.last_task_id.lock().await; + + let next_id = get_next_task_id(&collection, state_last_id.clone()).await; let new_document = QuestTaskDocument { name: body.name.clone(), desc: body.desc.clone(), diff --git a/src/endpoints/admin/custom_api/create_custom_api.rs b/src/endpoints/admin/custom_api/create_custom_api.rs index 7b33631..d1c93f6 100644 --- a/src/endpoints/admin/custom_api/create_custom_api.rs +++ b/src/endpoints/admin/custom_api/create_custom_api.rs @@ -1,11 +1,12 @@ +use crate::middleware::auth::auth_middleware; use crate::models::{QuestDocument, QuestTaskDocument}; +use crate::utils::get_next_task_id; 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} + response::{IntoResponse, Json}, }; use axum_auto_routes::route; use mongodb::bson::doc; @@ -43,11 +44,9 @@ pub async fn handler( 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; - } + let state_last_id = state.last_task_id.lock().await; + + let next_id = get_next_task_id(&collection, state_last_id.clone()).await; let new_document = QuestTaskDocument { name: body.name.clone(), diff --git a/src/endpoints/admin/discord/create_discord.rs b/src/endpoints/admin/discord/create_discord.rs index 2d5c1b5..c0f53d4 100644 --- a/src/endpoints/admin/discord/create_discord.rs +++ b/src/endpoints/admin/discord/create_discord.rs @@ -1,9 +1,10 @@ -use crate::models::{QuestDocument,QuestTaskDocument}; +use crate::middleware::auth::auth_middleware; +use crate::models::{QuestDocument, QuestTaskDocument}; +use crate::utils::get_next_task_id; use crate::utils::verify_quest_auth; use crate::{models::AppState, utils::get_error}; -use crate::middleware::auth::auth_middleware; use axum::{ - extract::{State, Extension}, + extract::{Extension, State}, http::StatusCode, response::{IntoResponse, Json}, }; @@ -41,11 +42,9 @@ pub async fn handler( 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; - } + let state_last_id = state.last_task_id.lock().await; + + let next_id = get_next_task_id(&collection, state_last_id.clone()).await; let new_document = QuestTaskDocument { name: body.name.clone(), diff --git a/src/endpoints/admin/domain/create_domain.rs b/src/endpoints/admin/domain/create_domain.rs index b119a36..05da536 100644 --- a/src/endpoints/admin/domain/create_domain.rs +++ b/src/endpoints/admin/domain/create_domain.rs @@ -1,9 +1,10 @@ +use crate::middleware::auth::auth_middleware; use crate::models::{QuestDocument, QuestTaskDocument}; +use crate::utils::get_next_task_id; use crate::utils::verify_quest_auth; use crate::{models::AppState, utils::get_error}; -use crate::middleware::auth::auth_middleware; use axum::{ - extract::{State, Extension}, + extract::{Extension, State}, http::StatusCode, response::{IntoResponse, Json}, }; @@ -38,11 +39,9 @@ pub async fn handler( let options = FindOneOptions::builder().sort(doc! {"id": -1}).build(); let last_doc = &collection.find_one(last_id_filter, options).await.unwrap(); - let mut next_id = 1; - if let Some(doc) = last_doc { - let last_id = doc.id; - next_id = last_id + 1; - } + let state_last_id = state.last_task_id.lock().await; + + let next_id = get_next_task_id(&collection, state_last_id.clone()).await; let new_document = QuestTaskDocument { name: body.name.clone(), diff --git a/src/endpoints/admin/nft_uri/create_uri.rs b/src/endpoints/admin/nft_uri/create_uri.rs index 1a58c00..2bbb3a2 100644 --- a/src/endpoints/admin/nft_uri/create_uri.rs +++ b/src/endpoints/admin/nft_uri/create_uri.rs @@ -1,9 +1,10 @@ -use crate::models::{NFTUri, QuestDocument}; +use crate::middleware::auth::auth_middleware; +use crate::models::{NFTUri, QuestDocument, QuestTaskDocument}; +use crate::utils::get_next_task_id; use crate::utils::verify_quest_auth; use crate::{models::AppState, utils::get_error}; -use crate::middleware::auth::auth_middleware; use axum::{ - extract::{State, Extension}, + extract::{Extension, State}, http::StatusCode, response::{IntoResponse, Json}, }; @@ -29,6 +30,7 @@ pub async fn handler( ) -> impl IntoResponse { let collection = state.db.collection::("nft_uri"); let quests_collection = state.db.collection::("quests"); + let insert_collection = state.db.collection::("tasks"); let res = verify_quest_auth(sub, &quests_collection, &(body.quest_id as i64)).await; if !res { @@ -40,18 +42,16 @@ pub async fn handler( let options = FindOneOptions::builder().sort(doc! {"id": -1}).build(); let last_doc = &collection.find_one(last_id_filter, options).await.unwrap(); - let mut next_id = 1; - if let Some(doc) = last_doc { - let last_id = doc.id; - next_id = last_id + 1; - } + let state_last_id = state.last_task_id.lock().await; + + let next_id = get_next_task_id(&insert_collection, state_last_id.clone()).await; let new_document = NFTUri { name: body.name.clone(), description: body.desc.clone(), image: body.image.clone(), quest_id: body.quest_id.clone() as i64, - id: next_id, + id: next_id.into(), attributes: None, }; diff --git a/src/endpoints/admin/quest_boost/create_boost.rs b/src/endpoints/admin/quest_boost/create_boost.rs index 42ed123..9fc1b2e 100644 --- a/src/endpoints/admin/quest_boost/create_boost.rs +++ b/src/endpoints/admin/quest_boost/create_boost.rs @@ -1,9 +1,10 @@ -use crate::models::{BoostTable, QuestDocument}; +use crate::middleware::auth::auth_middleware; +use crate::models::{BoostTable, QuestDocument, QuestTaskDocument}; +use crate::utils::get_next_task_id; use crate::utils::verify_quest_auth; use crate::{models::AppState, utils::get_error}; -use crate::middleware::auth::auth_middleware; use axum::{ - extract::{State, Extension}, + extract::{Extension, State}, http::StatusCode, response::{IntoResponse, Json}, }; @@ -35,6 +36,7 @@ pub async fn handler( ) -> impl IntoResponse { let collection = state.db.collection::("boosts"); let quests_collection = state.db.collection::("quests"); + let insert_collection = state.db.collection::("quests"); let res = verify_quest_auth(sub, &quests_collection, &(body.quest_id as i64)).await; if !res { @@ -46,11 +48,9 @@ pub async fn handler( let options = FindOneOptions::builder().sort(doc! {"id": -1}).build(); let last_doc = &collection.find_one(last_id_filter, options).await.unwrap(); - let mut next_id = 1; - if let Some(doc) = last_doc { - let last_id = doc.id; - next_id = last_id + 1; - } + let state_last_id = state.last_task_id.lock().await; + + let next_id = get_next_task_id(&insert_collection, state_last_id.clone()).await; let new_document = BoostTable { name: body.name.clone(), diff --git a/src/endpoints/admin/quiz/create_question.rs b/src/endpoints/admin/quiz/create_question.rs index f88586f..498c08e 100644 --- a/src/endpoints/admin/quiz/create_question.rs +++ b/src/endpoints/admin/quiz/create_question.rs @@ -1,9 +1,10 @@ +use crate::middleware::auth::auth_middleware; use crate::models::{ - QuestDocument, QuestTaskDocument, QuizInsertDocument, QuizQuestionDocument, + QuestDocument, QuestInsertDocument, QuestTaskDocument, QuizInsertDocument, QuizQuestionDocument, }; +use crate::utils::get_next_task_id; 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, @@ -73,18 +74,18 @@ pub async fn handler( .await .unwrap(); - let mut next_quiz_question_id = 1; - if let Some(doc) = last_quiz_question_doc { - let last_id = doc.id; - next_quiz_question_id = last_id + 1; - } + + + let state_last_id = state.last_task_id.lock().await; + + let next_quiz_question_id = get_next_task_id(&tasks_collection, state_last_id.clone()).await; 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, + id: next_quiz_question_id.into(), kind: "text_choice".to_string(), layout: "default".to_string(), }; diff --git a/src/endpoints/admin/twitter/create_twitter_fw.rs b/src/endpoints/admin/twitter/create_twitter_fw.rs index 53c3c59..b8c1580 100644 --- a/src/endpoints/admin/twitter/create_twitter_fw.rs +++ b/src/endpoints/admin/twitter/create_twitter_fw.rs @@ -1,7 +1,8 @@ +use crate::middleware::auth::auth_middleware; use crate::models::{QuestDocument, QuestTaskDocument}; +use crate::utils::get_next_task_id; 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, @@ -39,11 +40,11 @@ pub async fn handler( 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; - } + + + let state_last_id = state.last_task_id.lock().await; + + let next_id = get_next_task_id(&collection, state_last_id.clone()).await; let new_document = QuestTaskDocument { name: body.name.clone(), diff --git a/src/models.rs b/src/models.rs index 18c0536..a118083 100644 --- a/src/models.rs +++ b/src/models.rs @@ -6,10 +6,10 @@ use starknet::{ providers::{jsonrpc::HttpTransport, JsonRpcClient}, }; -use tokio::sync::Mutex; -use std::{net::SocketAddr}; use crate::endpoints::quests::uri::Attribute; use crate::{config::Config, logger::Logger}; +use std::net::SocketAddr; +use tokio::sync::Mutex; pub_struct!(;AppState { last_task_id: Mutex, diff --git a/src/utils.rs b/src/utils.rs index 2931ef9..3bf007e 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -12,11 +12,11 @@ use axum::{ }; use chrono::{Duration as dur, Utc}; use futures::TryStreamExt; +use mongodb::options::FindOneOptions; use mongodb::{ bson::doc, options::UpdateOptions, results::UpdateResult, Collection, Cursor, Database, IndexModel, }; -use mongodb::options::FindOneOptions; use rand::distributions::{Distribution, Uniform}; use serde_json::json; use starknet::signers::Signer; @@ -32,8 +32,8 @@ use std::hash::{Hash, Hasher}; use std::result::Result; use std::str::FromStr; use std::{fmt::Write, sync::Arc}; -use tokio::time::{sleep, Duration}; use tokio::sync::Mutex; +use tokio::time::{sleep, Duration}; #[macro_export] macro_rules! pub_struct { @@ -843,36 +843,34 @@ pub fn parse_string(input: &str, address: FieldElement) -> String { result } +// QuestInsertDocument pub async fn get_next_task_id( - // last_doc: Option, - task_collection: &Collection, - last_task_id: i64 - + // last_doc: Option, + task_collection: &Collection, + last_task_id: i64, ) -> i32 { - // let mut state_last_id =state.last_task_id.lock().unwrap(); + // let mut state_last_id =state.last_task_id.lock().unwrap(); let last_id_filter = doc! {}; - let options = FindOneOptions::builder().sort(doc! {"id": -1}).build(); - - let last_doc = task_collection.find_one(last_id_filter, options).await.unwrap(); + let options = FindOneOptions::builder().sort(doc! {"id": -1}).build(); + let last_doc = task_collection + .find_one(last_id_filter, options) + .await + .unwrap(); - let mut next_id: i32 = 1; + let mut next_id: i32 = 1; if let Some(doc) = last_doc { let db_last_id = doc.id; - - - next_id = std::cmp::max(db_last_id as i32, (last_task_id).try_into().unwrap()) + 1; + + next_id = std::cmp::max(db_last_id as i32, (last_task_id).try_into().unwrap()) + 1; } else { - // next_id = *state_last_id; next_id = (last_task_id as i32) + 1; } - // last_task_id = next_id.try_into().unwrap(); - next_id.into() -} \ No newline at end of file +} From f010a1fb1cdc3a2aab01887a68946f370b7fce00 Mon Sep 17 00:00:00 2001 From: mubarak23 Date: Fri, 4 Oct 2024 13:44:16 +0100 Subject: [PATCH 07/10] fix review changes --- src/endpoints/admin/balance/create_balance.rs | 8 -------- src/endpoints/admin/contract/create_contract.rs | 1 - src/endpoints/admin/custom/create_custom.rs | 7 ------- .../admin/custom_api/create_custom_api.rs | 1 - src/endpoints/admin/discord/create_discord.rs | 1 - src/endpoints/admin/domain/create_domain.rs | 1 - src/endpoints/admin/nft_uri/create_uri.rs | 1 - src/endpoints/admin/quest/create_quest.rs | 17 ++++++++--------- src/endpoints/admin/quest_boost/create_boost.rs | 1 - src/endpoints/admin/quiz/create_quiz.rs | 4 ++-- .../admin/twitter/create_twitter_fw.rs | 3 --- .../admin/twitter/create_twitter_rw.rs | 13 ++++++------- src/models.rs | 1 - src/utils.rs | 8 -------- 14 files changed, 16 insertions(+), 51 deletions(-) diff --git a/src/endpoints/admin/balance/create_balance.rs b/src/endpoints/admin/balance/create_balance.rs index 04f2850..56bcd5b 100644 --- a/src/endpoints/admin/balance/create_balance.rs +++ b/src/endpoints/admin/balance/create_balance.rs @@ -37,8 +37,6 @@ pub async fn handler( 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::("quests"); let res = verify_quest_auth(sub, &quests_collection, &(body.quest_id as i64)).await; @@ -46,12 +44,6 @@ pub async fn handler( 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; - // } - let state_last_id = state.last_task_id.lock().await; let next_id = get_next_task_id(&collection, state_last_id.clone()).await; diff --git a/src/endpoints/admin/contract/create_contract.rs b/src/endpoints/admin/contract/create_contract.rs index 7b0785b..bb53ada 100644 --- a/src/endpoints/admin/contract/create_contract.rs +++ b/src/endpoints/admin/contract/create_contract.rs @@ -34,7 +34,6 @@ pub async fn handler( // 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::("quests"); diff --git a/src/endpoints/admin/custom/create_custom.rs b/src/endpoints/admin/custom/create_custom.rs index 85672c1..5a711aa 100644 --- a/src/endpoints/admin/custom/create_custom.rs +++ b/src/endpoints/admin/custom/create_custom.rs @@ -34,7 +34,6 @@ pub async fn handler( // 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::("quests"); @@ -43,12 +42,6 @@ pub async fn handler( 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; - // } - let state_last_id = state.last_task_id.lock().await; let next_id = get_next_task_id(&collection, state_last_id.clone()).await; diff --git a/src/endpoints/admin/custom_api/create_custom_api.rs b/src/endpoints/admin/custom_api/create_custom_api.rs index d1c93f6..b87a2b1 100644 --- a/src/endpoints/admin/custom_api/create_custom_api.rs +++ b/src/endpoints/admin/custom_api/create_custom_api.rs @@ -35,7 +35,6 @@ pub async fn handler( // 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::("quests"); diff --git a/src/endpoints/admin/discord/create_discord.rs b/src/endpoints/admin/discord/create_discord.rs index c0f53d4..5173725 100644 --- a/src/endpoints/admin/discord/create_discord.rs +++ b/src/endpoints/admin/discord/create_discord.rs @@ -33,7 +33,6 @@ pub async fn handler( // 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::("quests"); diff --git a/src/endpoints/admin/domain/create_domain.rs b/src/endpoints/admin/domain/create_domain.rs index 05da536..3fb7e93 100644 --- a/src/endpoints/admin/domain/create_domain.rs +++ b/src/endpoints/admin/domain/create_domain.rs @@ -37,7 +37,6 @@ pub async fn handler( // 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 state_last_id = state.last_task_id.lock().await; diff --git a/src/endpoints/admin/nft_uri/create_uri.rs b/src/endpoints/admin/nft_uri/create_uri.rs index 2bbb3a2..603d0ac 100644 --- a/src/endpoints/admin/nft_uri/create_uri.rs +++ b/src/endpoints/admin/nft_uri/create_uri.rs @@ -40,7 +40,6 @@ pub async fn handler( // 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 state_last_id = state.last_task_id.lock().await; diff --git a/src/endpoints/admin/quest/create_quest.rs b/src/endpoints/admin/quest/create_quest.rs index febe418..1197078 100644 --- a/src/endpoints/admin/quest/create_quest.rs +++ b/src/endpoints/admin/quest/create_quest.rs @@ -1,8 +1,9 @@ -use crate::models::QuestInsertDocument; -use crate::{models::AppState, utils::get_error}; use crate::middleware::auth::auth_middleware; +use crate::models::{QuestInsertDocument, QuestTaskDocument}; +use crate::utils::get_next_task_id; +use crate::{models::AppState, utils::get_error}; use axum::{ - extract::{State, Extension}, + extract::{Extension, State}, http::StatusCode, response::{IntoResponse, Json}, }; @@ -35,16 +36,14 @@ pub async fn handler( Json(body): Json, ) -> impl IntoResponse { let collection = state.db.collection::("quests"); + let insert_collection = state.db.collection::("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 mut next_id = 1; - if let Some(doc) = last_doc { - let last_id = doc.id; - next_id = last_id + 1; - } + let state_last_id = state.last_task_id.lock().await; + + let next_id = get_next_task_id(&insert_collection, state_last_id.clone()).await; let nft_reward = doc! { "img": body.img_card.clone().to_string(), diff --git a/src/endpoints/admin/quest_boost/create_boost.rs b/src/endpoints/admin/quest_boost/create_boost.rs index 9fc1b2e..bfd6f72 100644 --- a/src/endpoints/admin/quest_boost/create_boost.rs +++ b/src/endpoints/admin/quest_boost/create_boost.rs @@ -46,7 +46,6 @@ pub async fn handler( // 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 state_last_id = state.last_task_id.lock().await; diff --git a/src/endpoints/admin/quiz/create_quiz.rs b/src/endpoints/admin/quiz/create_quiz.rs index 708dd27..89acec5 100644 --- a/src/endpoints/admin/quiz/create_quiz.rs +++ b/src/endpoints/admin/quiz/create_quiz.rs @@ -1,9 +1,9 @@ +use crate::middleware::auth::auth_middleware; use crate::models::{QuestDocument, QuestTaskDocument, QuizInsertDocument}; use crate::utils::verify_quest_auth; use crate::{models::AppState, utils::get_error}; -use crate::middleware::auth::auth_middleware; use axum::{ - extract::{State, Extension}, + extract::{Extension, State}, http::StatusCode, response::{IntoResponse, Json}, }; diff --git a/src/endpoints/admin/twitter/create_twitter_fw.rs b/src/endpoints/admin/twitter/create_twitter_fw.rs index b8c1580..0a77a17 100644 --- a/src/endpoints/admin/twitter/create_twitter_fw.rs +++ b/src/endpoints/admin/twitter/create_twitter_fw.rs @@ -32,7 +32,6 @@ pub async fn handler( // 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::("quests"); let res = verify_quest_auth(sub, &quests_collection, &body.quest_id).await; @@ -40,8 +39,6 @@ pub async fn handler( return get_error("Error creating task".to_string()); }; - - let state_last_id = state.last_task_id.lock().await; let next_id = get_next_task_id(&collection, state_last_id.clone()).await; diff --git a/src/endpoints/admin/twitter/create_twitter_rw.rs b/src/endpoints/admin/twitter/create_twitter_rw.rs index bb37a0f..6547722 100644 --- a/src/endpoints/admin/twitter/create_twitter_rw.rs +++ b/src/endpoints/admin/twitter/create_twitter_rw.rs @@ -1,7 +1,8 @@ +use crate::middleware::auth::auth_middleware; use crate::models::{QuestDocument, QuestTaskDocument}; +use crate::utils::get_next_task_id; 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, @@ -31,7 +32,6 @@ pub async fn handler( // 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::("quests"); @@ -40,11 +40,10 @@ pub async fn handler( 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; - } + let state_last_id = state.last_task_id.lock().await; + + let next_id = get_next_task_id(&collection, state_last_id.clone()).await; + let new_document = QuestTaskDocument { name: body.name.clone(), diff --git a/src/models.rs b/src/models.rs index a118083..18662b7 100644 --- a/src/models.rs +++ b/src/models.rs @@ -8,7 +8,6 @@ use starknet::{ use crate::endpoints::quests::uri::Attribute; use crate::{config::Config, logger::Logger}; -use std::net::SocketAddr; use tokio::sync::Mutex; pub_struct!(;AppState { diff --git a/src/utils.rs b/src/utils.rs index 3bf007e..58cd9d9 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -32,7 +32,6 @@ use std::hash::{Hash, Hasher}; use std::result::Result; use std::str::FromStr; use std::{fmt::Write, sync::Arc}; -use tokio::sync::Mutex; use tokio::time::{sleep, Duration}; #[macro_export] @@ -843,14 +842,10 @@ pub fn parse_string(input: &str, address: FieldElement) -> String { result } -// QuestInsertDocument pub async fn get_next_task_id( - // last_doc: Option, task_collection: &Collection, last_task_id: i64, ) -> i32 { - // let mut state_last_id =state.last_task_id.lock().unwrap(); - let last_id_filter = doc! {}; let options = FindOneOptions::builder().sort(doc! {"id": -1}).build(); @@ -866,11 +861,8 @@ pub async fn get_next_task_id( next_id = std::cmp::max(db_last_id as i32, (last_task_id).try_into().unwrap()) + 1; } else { - // next_id = *state_last_id; next_id = (last_task_id as i32) + 1; } - // last_task_id = next_id.try_into().unwrap(); - next_id.into() } From a854216978d3c2b16e134a7094bcda2d34643e2e Mon Sep 17 00:00:00 2001 From: mubarak23 Date: Fri, 4 Oct 2024 14:36:35 +0100 Subject: [PATCH 08/10] remove unused variables --- src/endpoints/admin/balance/create_balance.rs | 5 +---- src/endpoints/admin/contract/create_contract.rs | 3 --- src/endpoints/admin/custom/create_custom.rs | 3 --- src/endpoints/admin/custom_api/create_custom_api.rs | 4 +--- src/endpoints/admin/discord/create_discord.rs | 4 +--- src/endpoints/admin/domain/create_domain.rs | 4 +--- src/endpoints/admin/nft_uri/create_uri.rs | 3 --- src/endpoints/admin/quest/create_quest.rs | 5 +---- src/endpoints/admin/quest_boost/create_boost.rs | 4 +--- src/endpoints/admin/quiz/create_question.rs | 6 +----- src/endpoints/admin/twitter/create_twitter_rw.rs | 4 +--- 11 files changed, 8 insertions(+), 37 deletions(-) diff --git a/src/endpoints/admin/balance/create_balance.rs b/src/endpoints/admin/balance/create_balance.rs index 56bcd5b..67f39b3 100644 --- a/src/endpoints/admin/balance/create_balance.rs +++ b/src/endpoints/admin/balance/create_balance.rs @@ -32,10 +32,7 @@ pub async fn handler( Extension(sub): Extension, Json(body): Json, ) -> impl IntoResponse { - let collection = state.db.collection::("tasks"); - // Get the last id in increasing order - let last_id_filter = doc! {}; - let options = FindOneOptions::builder().sort(doc! {"id": -1}).build(); + let collection = state.db.collection::("tasks"); let quests_collection = state.db.collection::("quests"); diff --git a/src/endpoints/admin/contract/create_contract.rs b/src/endpoints/admin/contract/create_contract.rs index bb53ada..35d07d4 100644 --- a/src/endpoints/admin/contract/create_contract.rs +++ b/src/endpoints/admin/contract/create_contract.rs @@ -31,9 +31,6 @@ pub async fn handler( Json(body): Json, ) -> impl IntoResponse { let collection = state.db.collection::("tasks"); - // Get the last id in increasing order - let last_id_filter = doc! {}; - let options = FindOneOptions::builder().sort(doc! {"id": -1}).build(); let quests_collection = state.db.collection::("quests"); diff --git a/src/endpoints/admin/custom/create_custom.rs b/src/endpoints/admin/custom/create_custom.rs index 5a711aa..8b92d6c 100644 --- a/src/endpoints/admin/custom/create_custom.rs +++ b/src/endpoints/admin/custom/create_custom.rs @@ -31,9 +31,6 @@ pub async fn handler( Json(body): Json, ) -> impl IntoResponse { let collection = state.db.collection::("tasks"); - // Get the last id in increasing order - let last_id_filter = doc! {}; - let options = FindOneOptions::builder().sort(doc! {"id": -1}).build(); let quests_collection = state.db.collection::("quests"); diff --git a/src/endpoints/admin/custom_api/create_custom_api.rs b/src/endpoints/admin/custom_api/create_custom_api.rs index b87a2b1..c7bef4e 100644 --- a/src/endpoints/admin/custom_api/create_custom_api.rs +++ b/src/endpoints/admin/custom_api/create_custom_api.rs @@ -32,9 +32,7 @@ pub async fn handler( Json(body): Json, ) -> impl IntoResponse { let collection = state.db.collection::("tasks"); - // Get the last id in increasing order - let last_id_filter = doc! {}; - let options = FindOneOptions::builder().sort(doc! {"id": -1}).build(); + let quests_collection = state.db.collection::("quests"); diff --git a/src/endpoints/admin/discord/create_discord.rs b/src/endpoints/admin/discord/create_discord.rs index 5173725..b2f2bd6 100644 --- a/src/endpoints/admin/discord/create_discord.rs +++ b/src/endpoints/admin/discord/create_discord.rs @@ -30,9 +30,7 @@ pub async fn handler( Json(body): Json, ) -> impl IntoResponse { let collection = state.db.collection::("tasks"); - // Get the last id in increasing order - let last_id_filter = doc! {}; - let options = FindOneOptions::builder().sort(doc! {"id": -1}).build(); + let quests_collection = state.db.collection::("quests"); diff --git a/src/endpoints/admin/domain/create_domain.rs b/src/endpoints/admin/domain/create_domain.rs index 3fb7e93..e444075 100644 --- a/src/endpoints/admin/domain/create_domain.rs +++ b/src/endpoints/admin/domain/create_domain.rs @@ -34,9 +34,7 @@ pub async fn handler( if !res { return get_error("Error creating task".to_string()); }; - // Get the last id in increasing order - let last_id_filter = doc! {}; - let options = FindOneOptions::builder().sort(doc! {"id": -1}).build(); + let state_last_id = state.last_task_id.lock().await; diff --git a/src/endpoints/admin/nft_uri/create_uri.rs b/src/endpoints/admin/nft_uri/create_uri.rs index 603d0ac..e8b481f 100644 --- a/src/endpoints/admin/nft_uri/create_uri.rs +++ b/src/endpoints/admin/nft_uri/create_uri.rs @@ -37,9 +37,6 @@ pub async fn handler( return get_error("Error creating task".to_string()); }; - // Get the last id in increasing order - let last_id_filter = doc! {}; - let options = FindOneOptions::builder().sort(doc! {"id": -1}).build(); let state_last_id = state.last_task_id.lock().await; diff --git a/src/endpoints/admin/quest/create_quest.rs b/src/endpoints/admin/quest/create_quest.rs index 1197078..b3fa83c 100644 --- a/src/endpoints/admin/quest/create_quest.rs +++ b/src/endpoints/admin/quest/create_quest.rs @@ -37,10 +37,7 @@ pub async fn handler( ) -> impl IntoResponse { let collection = state.db.collection::("quests"); let insert_collection = state.db.collection::("tasks"); - // Get the last id in increasing order - let last_id_filter = doc! {}; - let options = FindOneOptions::builder().sort(doc! {"id": -1}).build(); - + let state_last_id = state.last_task_id.lock().await; let next_id = get_next_task_id(&insert_collection, state_last_id.clone()).await; diff --git a/src/endpoints/admin/quest_boost/create_boost.rs b/src/endpoints/admin/quest_boost/create_boost.rs index bfd6f72..2f36186 100644 --- a/src/endpoints/admin/quest_boost/create_boost.rs +++ b/src/endpoints/admin/quest_boost/create_boost.rs @@ -43,9 +43,7 @@ pub async fn handler( return get_error("Error creating boost".to_string()); }; - // Get the last id in increasing order - let last_id_filter = doc! {}; - let options = FindOneOptions::builder().sort(doc! {"id": -1}).build(); + let state_last_id = state.last_task_id.lock().await; diff --git a/src/endpoints/admin/quiz/create_question.rs b/src/endpoints/admin/quiz/create_question.rs index 498c08e..25596a0 100644 --- a/src/endpoints/admin/quiz/create_question.rs +++ b/src/endpoints/admin/quiz/create_question.rs @@ -1,7 +1,5 @@ use crate::middleware::auth::auth_middleware; -use crate::models::{ - QuestDocument, QuestInsertDocument, QuestTaskDocument, QuizInsertDocument, QuizQuestionDocument, -}; +use crate::models::{QuestDocument, QuestTaskDocument, QuizInsertDocument, QuizQuestionDocument}; use crate::utils::get_next_task_id; use crate::utils::verify_quest_auth; use crate::{models::AppState, utils::get_error}; @@ -74,8 +72,6 @@ pub async fn handler( .await .unwrap(); - - let state_last_id = state.last_task_id.lock().await; let next_quiz_question_id = get_next_task_id(&tasks_collection, state_last_id.clone()).await; diff --git a/src/endpoints/admin/twitter/create_twitter_rw.rs b/src/endpoints/admin/twitter/create_twitter_rw.rs index 6547722..14eca99 100644 --- a/src/endpoints/admin/twitter/create_twitter_rw.rs +++ b/src/endpoints/admin/twitter/create_twitter_rw.rs @@ -29,9 +29,7 @@ pub async fn handler( Json(body): Json, ) -> impl IntoResponse { let collection = state.db.collection::("tasks"); - // Get the last id in increasing order - let last_id_filter = doc! {}; - let options = FindOneOptions::builder().sort(doc! {"id": -1}).build(); + let quests_collection = state.db.collection::("quests"); From db65903f0a2070a3bea724064e9e13b1e43a527f Mon Sep 17 00:00:00 2001 From: mubarak23 Date: Fri, 4 Oct 2024 14:45:33 +0100 Subject: [PATCH 09/10] remove unsed next_id variable --- src/utils.rs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/utils.rs b/src/utils.rs index 58cd9d9..6f03d63 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -854,15 +854,11 @@ pub async fn get_next_task_id( .await .unwrap(); - let mut next_id: i32 = 1; - if let Some(doc) = last_doc { let db_last_id = doc.id; - next_id = std::cmp::max(db_last_id as i32, (last_task_id).try_into().unwrap()) + 1; + return std::cmp::max(db_last_id as i32, (last_task_id).try_into().unwrap()) + 1; } else { - next_id = (last_task_id as i32) + 1; + return (last_task_id as i32) + 1; } - - next_id.into() } From cea82a7589986fda90ba198ec807e24559284835 Mon Sep 17 00:00:00 2001 From: mubarak23 Date: Fri, 4 Oct 2024 16:24:46 +0100 Subject: [PATCH 10/10] remove all unused variables --- src/endpoints/admin/balance/create_balance.rs | 3 +-- src/endpoints/admin/contract/create_contract.rs | 2 +- src/endpoints/admin/custom/create_custom.rs | 1 - src/endpoints/admin/custom_api/create_custom_api.rs | 2 -- src/endpoints/admin/discord/create_discord.rs | 2 -- src/endpoints/admin/domain/create_domain.rs | 2 -- src/endpoints/admin/nft_uri/create_uri.rs | 2 -- src/endpoints/admin/quest/create_quest.rs | 3 +-- src/endpoints/admin/quest_boost/create_boost.rs | 3 --- src/endpoints/admin/quiz/create_question.rs | 8 -------- src/endpoints/admin/twitter/create_twitter_fw.rs | 5 +---- src/endpoints/admin/twitter/create_twitter_rw.rs | 3 --- 12 files changed, 4 insertions(+), 32 deletions(-) diff --git a/src/endpoints/admin/balance/create_balance.rs b/src/endpoints/admin/balance/create_balance.rs index 67f39b3..818e18a 100644 --- a/src/endpoints/admin/balance/create_balance.rs +++ b/src/endpoints/admin/balance/create_balance.rs @@ -10,7 +10,6 @@ use axum::{ }; 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; @@ -32,7 +31,7 @@ pub async fn handler( Extension(sub): Extension, Json(body): Json, ) -> impl IntoResponse { - let collection = state.db.collection::("tasks"); + let collection = state.db.collection::("tasks"); let quests_collection = state.db.collection::("quests"); diff --git a/src/endpoints/admin/contract/create_contract.rs b/src/endpoints/admin/contract/create_contract.rs index 35d07d4..5ee1fcb 100644 --- a/src/endpoints/admin/contract/create_contract.rs +++ b/src/endpoints/admin/contract/create_contract.rs @@ -10,7 +10,7 @@ use axum::{ }; use axum_auto_routes::route; use mongodb::bson::doc; -use mongodb::options::FindOneOptions; + use serde::Deserialize; use serde_json::json; use std::sync::Arc; diff --git a/src/endpoints/admin/custom/create_custom.rs b/src/endpoints/admin/custom/create_custom.rs index 8b92d6c..1901fdf 100644 --- a/src/endpoints/admin/custom/create_custom.rs +++ b/src/endpoints/admin/custom/create_custom.rs @@ -10,7 +10,6 @@ use axum::{ }; use axum_auto_routes::route; use mongodb::bson::doc; -use mongodb::options::FindOneOptions; use serde::Deserialize; use serde_json::json; use std::sync::Arc; diff --git a/src/endpoints/admin/custom_api/create_custom_api.rs b/src/endpoints/admin/custom_api/create_custom_api.rs index c7bef4e..4818d6c 100644 --- a/src/endpoints/admin/custom_api/create_custom_api.rs +++ b/src/endpoints/admin/custom_api/create_custom_api.rs @@ -10,7 +10,6 @@ use axum::{ }; use axum_auto_routes::route; use mongodb::bson::doc; -use mongodb::options::FindOneOptions; use serde::Deserialize; use serde_json::json; use std::sync::Arc; @@ -32,7 +31,6 @@ pub async fn handler( Json(body): Json, ) -> impl IntoResponse { let collection = state.db.collection::("tasks"); - let quests_collection = state.db.collection::("quests"); diff --git a/src/endpoints/admin/discord/create_discord.rs b/src/endpoints/admin/discord/create_discord.rs index b2f2bd6..8ff4388 100644 --- a/src/endpoints/admin/discord/create_discord.rs +++ b/src/endpoints/admin/discord/create_discord.rs @@ -10,7 +10,6 @@ use axum::{ }; use axum_auto_routes::route; use mongodb::bson::doc; -use mongodb::options::FindOneOptions; use serde::Deserialize; use serde_json::json; use std::sync::Arc; @@ -30,7 +29,6 @@ pub async fn handler( Json(body): Json, ) -> impl IntoResponse { let collection = state.db.collection::("tasks"); - let quests_collection = state.db.collection::("quests"); diff --git a/src/endpoints/admin/domain/create_domain.rs b/src/endpoints/admin/domain/create_domain.rs index e444075..152a78c 100644 --- a/src/endpoints/admin/domain/create_domain.rs +++ b/src/endpoints/admin/domain/create_domain.rs @@ -10,7 +10,6 @@ use axum::{ }; use axum_auto_routes::route; use mongodb::bson::doc; -use mongodb::options::FindOneOptions; use serde::Deserialize; use serde_json::json; use std::sync::Arc; @@ -34,7 +33,6 @@ pub async fn handler( if !res { return get_error("Error creating task".to_string()); }; - let state_last_id = state.last_task_id.lock().await; diff --git a/src/endpoints/admin/nft_uri/create_uri.rs b/src/endpoints/admin/nft_uri/create_uri.rs index e8b481f..b286762 100644 --- a/src/endpoints/admin/nft_uri/create_uri.rs +++ b/src/endpoints/admin/nft_uri/create_uri.rs @@ -10,7 +10,6 @@ use axum::{ }; use axum_auto_routes::route; use mongodb::bson::doc; -use mongodb::options::FindOneOptions; use serde::Deserialize; use serde_json::json; use std::sync::Arc; @@ -37,7 +36,6 @@ pub async fn handler( return get_error("Error creating task".to_string()); }; - let state_last_id = state.last_task_id.lock().await; let next_id = get_next_task_id(&insert_collection, state_last_id.clone()).await; diff --git a/src/endpoints/admin/quest/create_quest.rs b/src/endpoints/admin/quest/create_quest.rs index b3fa83c..fceb2c3 100644 --- a/src/endpoints/admin/quest/create_quest.rs +++ b/src/endpoints/admin/quest/create_quest.rs @@ -9,7 +9,6 @@ use axum::{ }; use axum_auto_routes::route; use mongodb::bson::{doc, from_document}; -use mongodb::options::FindOneOptions; use serde::Deserialize; use serde_json::json; use std::sync::Arc; @@ -37,7 +36,7 @@ pub async fn handler( ) -> impl IntoResponse { let collection = state.db.collection::("quests"); let insert_collection = state.db.collection::("tasks"); - + let state_last_id = state.last_task_id.lock().await; let next_id = get_next_task_id(&insert_collection, state_last_id.clone()).await; diff --git a/src/endpoints/admin/quest_boost/create_boost.rs b/src/endpoints/admin/quest_boost/create_boost.rs index 2f36186..c959476 100644 --- a/src/endpoints/admin/quest_boost/create_boost.rs +++ b/src/endpoints/admin/quest_boost/create_boost.rs @@ -10,7 +10,6 @@ use axum::{ }; use axum_auto_routes::route; use mongodb::bson::doc; -use mongodb::options::FindOneOptions; use serde::Deserialize; use serde_json::json; use std::sync::Arc; @@ -43,8 +42,6 @@ pub async fn handler( return get_error("Error creating boost".to_string()); }; - - let state_last_id = state.last_task_id.lock().await; let next_id = get_next_task_id(&insert_collection, state_last_id.clone()).await; diff --git a/src/endpoints/admin/quiz/create_question.rs b/src/endpoints/admin/quiz/create_question.rs index 25596a0..22080fe 100644 --- a/src/endpoints/admin/quiz/create_question.rs +++ b/src/endpoints/admin/quiz/create_question.rs @@ -10,7 +10,6 @@ use axum::{ }; use axum_auto_routes::route; use mongodb::bson::doc; -use mongodb::options::FindOneOptions; use serde::Deserialize; use serde_json::json; use std::sync::Arc; @@ -64,13 +63,6 @@ pub async fn handler( 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 state_last_id = state.last_task_id.lock().await; diff --git a/src/endpoints/admin/twitter/create_twitter_fw.rs b/src/endpoints/admin/twitter/create_twitter_fw.rs index 0a77a17..5db395b 100644 --- a/src/endpoints/admin/twitter/create_twitter_fw.rs +++ b/src/endpoints/admin/twitter/create_twitter_fw.rs @@ -10,7 +10,6 @@ use axum::{ }; use axum_auto_routes::route; use mongodb::bson::doc; -use mongodb::options::FindOneOptions; use serde::Deserialize; use serde_json::json; use std::sync::Arc; @@ -29,9 +28,7 @@ pub async fn handler( body: Json, ) -> impl IntoResponse { let collection = state.db.collection::("tasks"); - // Get the last id in increasing order - let last_id_filter = doc! {}; - let options = FindOneOptions::builder().sort(doc! {"id": -1}).build(); + let quests_collection = state.db.collection::("quests"); let res = verify_quest_auth(sub, &quests_collection, &body.quest_id).await; diff --git a/src/endpoints/admin/twitter/create_twitter_rw.rs b/src/endpoints/admin/twitter/create_twitter_rw.rs index 14eca99..5509394 100644 --- a/src/endpoints/admin/twitter/create_twitter_rw.rs +++ b/src/endpoints/admin/twitter/create_twitter_rw.rs @@ -10,7 +10,6 @@ use axum::{ }; use axum_auto_routes::route; use mongodb::bson::doc; -use mongodb::options::FindOneOptions; use serde::Deserialize; use serde_json::json; use std::sync::Arc; @@ -29,7 +28,6 @@ pub async fn handler( Json(body): Json, ) -> impl IntoResponse { let collection = state.db.collection::("tasks"); - let quests_collection = state.db.collection::("quests"); @@ -42,7 +40,6 @@ pub async fn handler( let next_id = get_next_task_id(&collection, state_last_id.clone()).await; - let new_document = QuestTaskDocument { name: body.name.clone(), desc: body.desc.clone(),