Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: parse expiry timestamp #63

Merged
merged 2 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 35 additions & 5 deletions src/endpoints/get_quest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use axum::{
http::StatusCode,
response::{IntoResponse, Json},
};
use chrono::Utc;
use mongodb::bson;
use mongodb::bson::doc;
use serde::Deserialize;
use std::sync::Arc;
Expand All @@ -21,11 +23,39 @@ pub async fn handler(
Query(query): Query<GetQuestsQuery>,
) -> impl IntoResponse {
let collection = state.db.collection::<QuestDocument>("quests");
match collection
.find_one(doc! {"id": query.id, "disabled" : false}, None)
.await
{
Ok(Some(quest)) => (StatusCode::OK, Json(quest)).into_response(),
let current_timestamp = bson::DateTime::from_millis(Utc::now().timestamp_millis());
let filter = doc! {
"$and": [
{
"id": query.id
},
{
"$or": [
{
"expiry": {
"$exists": false
}
},
{
"expiry": {
"$gt": current_timestamp
}
}
]
},
{
"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);
}
(StatusCode::OK, Json(quest)).into_response()
}
Ok(None) => get_error("Quest not found".to_string()),
Err(_) => get_error("Error querying quest".to_string()),
}
Expand Down
18 changes: 16 additions & 2 deletions src/endpoints/get_quests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ use axum::{
response::{IntoResponse, Json},
};
use chrono::Utc;
use futures::StreamExt;
use futures::TryStreamExt;
use mongodb::bson::{self, doc};
use mongodb::bson;
use mongodb::bson::doc;
use serde::{Deserialize, Serialize};
use std::sync::Arc;

Expand Down Expand Up @@ -48,7 +50,19 @@ pub async fn handler(State(state): State<Arc<AppState>>) -> impl IntoResponse {
};
match collection.find(Some(filter), None).await {
Ok(cursor) => {
let quests: Vec<QuestDocument> = cursor.try_collect().await.unwrap_or_else(|_| vec![]);
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);
}
quest
})
})
.try_collect()
.await
.unwrap_or_else(|_| vec![]);
if quests.is_empty() {
get_error("No quests found".to_string())
} else {
Expand Down
1 change: 1 addition & 0 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub_struct!(Debug, Serialize, Deserialize; QuestDocument {
hidden: bool,
disabled: bool,
expiry: Option<bson::DateTime>,
expiry_timestamp: Option<String>,
});

pub_struct!(Deserialize; CompletedTasks {
Expand Down