Skip to content

Commit

Permalink
feat: add updated db changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ayushtom committed Apr 18, 2024
1 parent 32d8bd8 commit 25bc1ed
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 36 deletions.
24 changes: 19 additions & 5 deletions src/endpoints/get_quest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,16 @@ pub async fn handler(
Query(query): Query<GetQuestsQuery>,
) -> impl IntoResponse {
let collection = state.db.collection::<QuestDocument>("quests");
let current_time = chrono::Utc::now().timestamp_millis();

let pipeline = [
doc! {
"$match": {
"disabled": false,
"id": query.id
"id": query.id,
"start_time": doc! {
"$lte": current_time
},
}
},
doc! {
Expand All @@ -38,8 +43,18 @@ pub async fn handler(
"$cond": [
{
"$and": [
{ "$gte": ["$expiry", 0] },
{ "$lt": ["$expiry", "$$NOW"] },
doc! {
"$gte": [
"$expiry",
0
]
},
doc! {
"$lt": [
"$expiry",
"$$NOW"
]
}
]
},
true,
Expand All @@ -57,8 +72,7 @@ pub async fn handler(
Ok(document) => {
if let Ok(mut quest) = from_document::<QuestDocument>(document) {
if let Some(expiry) = &quest.expiry {
let timestamp = expiry.timestamp_millis().to_string();
quest.expiry_timestamp = Some(timestamp);
quest.expiry_timestamp = Some(expiry.to_string());
}
return (StatusCode::OK, Json(quest)).into_response();
}
Expand Down
25 changes: 3 additions & 22 deletions src/endpoints/get_quest_participants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use serde::{Deserialize, Serialize};
use std::sync::Arc;

#[derive(Debug, Serialize, Deserialize)]

pub struct GetQuestParticipantsQuery {
quest_id: u32,
}
Expand Down Expand Up @@ -48,30 +49,10 @@ pub async fn handler(
let tasks_count = tasks_ids.len();

let pipeline = vec![
doc! {
"$addFields": doc! {
"refactoredTimestamp": doc! {
"$toDate": "$timestamp"
}
}
},
doc! {
"$match": {
"$expr": doc! {
"$and": [
doc! {
"$in": [
"$task_id",
tasks_ids
]
},
doc! {
"$lt": [
"$refactoredTimestamp",
"$$expiry"
]
}
]
"task_id": {
"$in": tasks_ids
}
}
},
Expand Down
23 changes: 18 additions & 5 deletions src/endpoints/get_quests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@ pub struct NFTItem {

#[route(get, "/get_quests", crate::endpoints::get_quests)]
pub async fn handler(State(state): State<Arc<AppState>>) -> impl IntoResponse {
let current_time = chrono::Utc::now().timestamp_millis();

let pipeline = vec![
doc! {
"$match": {
"disabled": false,
"hidden": false,
"start_time": {
"$lte":current_time
}
}
},
doc! {
Expand All @@ -36,8 +40,18 @@ pub async fn handler(State(state): State<Arc<AppState>>) -> impl IntoResponse {
"$cond": [
{
"$and": [
{ "$gte": ["$expiry", 0] },
{ "$lt": ["$expiry", "$$NOW"] },
doc! {
"$gte": [
"$expiry",
0
]
},
doc! {
"$lt": [
"$expiry",
"$$NOW"
]
}
]
},
true,
Expand All @@ -57,8 +71,7 @@ pub async fn handler(State(state): State<Arc<AppState>>) -> impl IntoResponse {
Ok(document) => {
if let Ok(mut quest) = from_document::<QuestDocument>(document) {
if let Some(expiry) = &quest.expiry {
let timestamp = expiry.timestamp_millis().to_string();
quest.expiry_timestamp = Some(timestamp);
quest.expiry_timestamp = Some(expiry.to_string());
}
quests.push(quest);
}
Expand Down
6 changes: 5 additions & 1 deletion src/endpoints/get_trending_quests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,16 @@ pub async fn handler(
Some(addr) => addr.to_string(),
None => "".to_string(),
};
let current_time = chrono::Utc::now().timestamp_millis();

let mut pipeline = vec![
doc! {
"$match": {
"disabled": false,
"hidden": false,
"is_trending": true,
"start_time": doc! {
"$lte": current_time
}
}
},
doc! {
Expand Down
8 changes: 5 additions & 3 deletions src/models.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use mongodb::{bson, Database};
use mongodb::{Database};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use starknet::{
Expand Down Expand Up @@ -34,13 +34,15 @@ pub_struct!(Debug, Serialize, Deserialize; QuestDocument {
rewards_nfts: Vec<NFTItem>,
img_card: String,
title_card: String,
hidden: bool,
hidden: Option<bool>,
disabled: bool,
expiry: Option<bson::DateTime>,
expiry: Option<i64>,
expiry_timestamp: Option<String>,
mandatory_domain: Option<String>,
expired: Option<bool>,
experience: i64,
start_time: i64,

});

pub_struct!(Deserialize; CompletedTasks {
Expand Down

0 comments on commit 25bc1ed

Please sign in to comment.