Skip to content

Commit

Permalink
Merge pull request #277 from mubarak23/ft_next_id_generate
Browse files Browse the repository at this point in the history
Ft next id generate
  • Loading branch information
Marchand-Nicolas authored Oct 4, 2024
2 parents 1b944a3 + cea82a7 commit 53d651e
Show file tree
Hide file tree
Showing 16 changed files with 111 additions and 159 deletions.
18 changes: 6 additions & 12 deletions src/endpoints/admin/balance/create_balance.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
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;
use mongodb::options::FindOneOptions;
use serde::Deserialize;
use serde_json::json;
use starknet::core::types::FieldElement;
Expand All @@ -32,10 +32,6 @@ pub async fn handler(
Json(body): Json<CreateBalance>,
) -> impl IntoResponse {
let collection = state.db.collection::<QuestTaskDocument>("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 quests_collection = state.db.collection::<QuestDocument>("quests");

Expand All @@ -44,11 +40,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;

// Build a vector of FieldElement from the comma separated contracts string
let parsed_contracts: Vec<FieldElement> = body
Expand Down
21 changes: 8 additions & 13 deletions src/endpoints/admin/contract/create_contract.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
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;
use mongodb::options::FindOneOptions;

use serde::Deserialize;
use serde_json::json;
use std::sync::Arc;
Expand All @@ -30,10 +31,6 @@ pub async fn handler(
Json(body): Json<CreateContract>,
) -> impl IntoResponse {
let collection = state.db.collection::<QuestTaskDocument>("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 quests_collection = state.db.collection::<QuestDocument>("quests");

Expand All @@ -42,11 +39,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(),
Expand Down
19 changes: 6 additions & 13 deletions src/endpoints/admin/custom/create_custom.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
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},
};
use axum_auto_routes::route;
use mongodb::bson::doc;
use mongodb::options::FindOneOptions;
use serde::Deserialize;
use serde_json::json;
use std::sync::Arc;
Expand All @@ -30,24 +30,17 @@ pub async fn handler(
Json(body): Json<CreateCustom>,
) -> impl IntoResponse {
let collection = state.db.collection::<QuestTaskDocument>("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 quests_collection = state.db.collection::<QuestDocument>("quests");

let res = verify_quest_auth(sub, &quests_collection, &(body.quest_id as i64)).await;
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 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(),
Expand Down
18 changes: 6 additions & 12 deletions src/endpoints/admin/custom_api/create_custom_api.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
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;
use mongodb::options::FindOneOptions;
use serde::Deserialize;
use serde_json::json;
use std::sync::Arc;
Expand All @@ -31,10 +31,6 @@ pub async fn handler(
Json(body): Json<CreateCustomAPI>,
) -> impl IntoResponse {
let collection = state.db.collection::<QuestTaskDocument>("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 quests_collection = state.db.collection::<QuestDocument>("quests");

Expand All @@ -43,11 +39,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(),
Expand Down
20 changes: 7 additions & 13 deletions src/endpoints/admin/discord/create_discord.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
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},
};
use axum_auto_routes::route;
use mongodb::bson::doc;
use mongodb::options::FindOneOptions;
use serde::Deserialize;
use serde_json::json;
use std::sync::Arc;
Expand All @@ -29,10 +29,6 @@ pub async fn handler(
Json(body): Json<CreateCustom>,
) -> impl IntoResponse {
let collection = state.db.collection::<QuestTaskDocument>("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 quests_collection = state.db.collection::<QuestDocument>("quests");

Expand All @@ -41,11 +37,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(),
Expand Down
18 changes: 6 additions & 12 deletions src/endpoints/admin/domain/create_domain.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
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},
};
use axum_auto_routes::route;
use mongodb::bson::doc;
use mongodb::options::FindOneOptions;
use serde::Deserialize;
use serde_json::json;
use std::sync::Arc;
Expand All @@ -33,16 +33,10 @@ 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 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(),
Expand Down
22 changes: 8 additions & 14 deletions src/endpoints/admin/nft_uri/create_uri.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
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},
};
use axum_auto_routes::route;
use mongodb::bson::doc;
use mongodb::options::FindOneOptions;
use serde::Deserialize;
use serde_json::json;
use std::sync::Arc;
Expand All @@ -29,29 +29,23 @@ pub async fn handler(
) -> impl IntoResponse {
let collection = state.db.collection::<NFTUri>("nft_uri");
let quests_collection = state.db.collection::<QuestDocument>("quests");
let insert_collection = state.db.collection::<QuestTaskDocument>("tasks");

let res = verify_quest_auth(sub, &quests_collection, &(body.quest_id as i64)).await;
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 last_doc = &collection.find_one(last_id_filter, options).await.unwrap();
let state_last_id = state.last_task_id.lock().await;

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(&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,
};

Expand Down
21 changes: 8 additions & 13 deletions src/endpoints/admin/quest/create_quest.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
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},
};
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;
Expand All @@ -35,16 +35,11 @@ pub async fn handler(
Json(body): Json<CreateQuestQuery>,
) -> impl IntoResponse {
let collection = state.db.collection::<QuestInsertDocument>("quests");
// 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 insert_collection = state.db.collection::<QuestTaskDocument>("tasks");

let state_last_id = state.last_task_id.lock().await;

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(&insert_collection, state_last_id.clone()).await;

let nft_reward = doc! {
"img": body.img_card.clone().to_string(),
Expand Down
Loading

0 comments on commit 53d651e

Please sign in to comment.