Skip to content

Commit

Permalink
Merge pull request #124 from starknet-id/feat/expired-quests
Browse files Browse the repository at this point in the history
fixing get_quests route res format
  • Loading branch information
Th0rgal authored Nov 7, 2023
2 parents 26b7970 + eb848c9 commit b79bcde
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 119 deletions.
72 changes: 41 additions & 31 deletions src/endpoints/get_quest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use axum::{
http::StatusCode,
response::{IntoResponse, Json},
};
use chrono::Utc;
use mongodb::bson;
use futures::StreamExt;
use mongodb::bson::doc;
use mongodb::bson::from_document;
use serde::Deserialize;
use std::sync::Arc;

Expand All @@ -23,40 +23,50 @@ pub async fn handler(
Query(query): Query<GetQuestsQuery>,
) -> impl IntoResponse {
let collection = state.db.collection::<QuestDocument>("quests");
let current_timestamp = bson::DateTime::from_millis(Utc::now().timestamp_millis());
let filter = doc! {
"$and": [
{
let pipeline = [
doc! {
"$match": {
"disabled": false,
"hidden": false,
"id": query.id
},
{
"$or": [
{
"expiry": {
"$exists": false
}
},
{
"expiry": {
"$gt": current_timestamp
}
},
doc! {
"$addFields": {
"expired": {
"$cond": [
{
"$and": [
{ "$gte": ["$expiry", 0] },
{ "$lt": ["$expiry", "$$NOW"] },
]
},
true,
false
]
}
}
},
];

match collection.aggregate(pipeline, None).await {
Ok(mut cursor) => {
while let Some(result) = cursor.next().await {
match result {
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);
}
return (StatusCode::OK, Json(quest)).into_response();
}
}
]
},
{
"disabled": false
}
]
};
match collection.find_one(filter, None).await {
Ok(Some(mut quest)) => {
if let Some(expiry) = &quest.expiry {
let timestamp = expiry.timestamp_millis().to_string();
quest.expiry_timestamp = Some(timestamp);
_ => continue,
}
}
(StatusCode::OK, Json(quest)).into_response()
get_error("Quest not found".to_string())
}
Ok(None) => get_error("Quest not found".to_string()),
Err(_) => get_error("Error querying quest".to_string()),
}
}
81 changes: 41 additions & 40 deletions src/endpoints/get_quests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ use axum::{
http::StatusCode,
response::{IntoResponse, Json},
};
use chrono::Utc;
use futures::StreamExt;
use futures::TryStreamExt;
use mongodb::bson;
use mongodb::bson::doc;
use mongodb::bson::from_document;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
Expand All @@ -23,47 +21,51 @@ pub struct NFTItem {
}

pub async fn handler(State(state): State<Arc<AppState>>) -> impl IntoResponse {
let collection = state.db.collection::<QuestDocument>("quests");
let current_timestamp = bson::DateTime::from_millis(Utc::now().timestamp_millis());
let filter = doc! {
"$and": [
{
"$or": [
{
"expiry": {
"$exists": false
}
},
{
"expiry": {
"$gt": current_timestamp
}
let pipeline = vec![
doc! {
"$match": {
"disabled": false,
"hidden": false,
}
},
doc! {
"$addFields": {
"expired": {
"$cond": [
{
"$and": [
{ "$gte": ["$expiry", 0] },
{ "$lt": ["$expiry", "$$NOW"] },
]
},
true,
false
]
}
]
}
},
{
"hidden": false
}
]
};
];
let collection = state.db.collection::<QuestDocument>("quests");

match collection.find(Some(filter), None).await {
Ok(cursor) => {
let quests_temp: Vec<QuestDocument> = cursor
.map(|result| {
result.map(|mut quest: QuestDocument| {
if let Some(expiry) = &quest.expiry {
let timestamp = expiry.timestamp_millis().to_string();
quest.expiry_timestamp = Some(timestamp);
match collection.aggregate(pipeline, None).await {
Ok(mut cursor) => {
let mut quests: Vec<QuestDocument> = Vec::new();
while let Some(result) = cursor.next().await {
match result {
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);
}
quests.push(quest);
}
quest
})
})
.try_collect()
.await
.unwrap_or_else(|_| vec![]);
}
_ => continue,
}
}
let mut res: HashMap<String, Vec<QuestDocument>> = HashMap::new();
for quest in quests_temp {
for quest in quests {
let category = quest.category.clone();
if res.contains_key(&category) {
let quests = res.get_mut(&category).unwrap();
Expand All @@ -72,7 +74,6 @@ pub async fn handler(State(state): State<Arc<AppState>>) -> impl IntoResponse {
res.insert(category, vec![quest]);
}
}

if res.is_empty() {
get_error("No quests found".to_string())
} else {
Expand Down
85 changes: 37 additions & 48 deletions src/endpoints/get_trending_quests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ use axum::{
http::StatusCode,
response::{IntoResponse, Json},
};
use chrono::Utc;
use futures::StreamExt;
use futures::TryStreamExt;
use mongodb::bson;
use mongodb::bson::doc;
use mongodb::bson::from_document;
use serde::{Deserialize, Serialize};
use std::sync::Arc;

Expand All @@ -22,55 +20,46 @@ pub struct NFTItem {
}

pub async fn handler(State(state): State<Arc<AppState>>) -> impl IntoResponse {
let collection = state.db.collection::<QuestDocument>("quests");
let current_timestamp = bson::DateTime::from_millis(Utc::now().timestamp_millis());
let filter = doc! {
"$and": [
{
"$or": [
{
"expiry": {
"$exists": false
}
},
{
"expiry": {
"$gt": current_timestamp
}
}
]
},
{
"disabled": false
let pipeline = vec![
doc! {
"$match": {
"disabled": false,
"hidden": false,
"is_trending": true,
}
},
{
"hidden": false
doc! {
"$addFields": {
"expired": {
"$cond": [
{
"$and": [
{ "$gte": ["$expiry", 0] },
{ "$lt": ["$expiry", "$$NOW"] },
]
},
true,
false
]
}
}
},
{
"is_trending": true
}
]
};
match collection.find(Some(filter), None).await {
Ok(cursor) => {
let quests: Vec<QuestDocument> = cursor
.map(|result| {
result.map(|mut quest: QuestDocument| {
if let Some(expiry) = &quest.expiry {
let timestamp = expiry.timestamp_millis().to_string();
quest.expiry_timestamp = Some(timestamp);
];
let collection = state.db.collection::<QuestDocument>("quests");
match collection.aggregate(pipeline, None).await {
Ok(mut cursor) => {
let mut quests: Vec<QuestDocument> = Vec::new();
while let Some(result) = cursor.next().await {
match result {
Ok(document) => {
if let Ok(quest) = from_document::<QuestDocument>(document) {
quests.push(quest);
}
quest
})
})
.try_collect()
.await
.unwrap_or_else(|_| vec![]);
if quests.is_empty() {
get_error("No quests found".to_string())
} else {
(StatusCode::OK, Json(quests)).into_response()
}
_ => continue,
}
}
(StatusCode::OK, Json(quests)).into_response()
}
Err(_) => get_error("Error querying quests".to_string()),
}
Expand Down
1 change: 1 addition & 0 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub_struct!(Debug, Serialize, Deserialize; QuestDocument {
expiry: Option<bson::DateTime>,
expiry_timestamp: Option<String>,
mandatory_domain: Option<String>,
expired: Option<bool>,
});

pub_struct!(Deserialize; CompletedTasks {
Expand Down

0 comments on commit b79bcde

Please sign in to comment.