aboutsummaryrefslogtreecommitdiff
path: root/planetwars-server/src/routes/matches.rs
diff options
context:
space:
mode:
authorIlion Beyst <ilion.beyst@gmail.com>2022-08-02 20:12:34 +0200
committerIlion Beyst <ilion.beyst@gmail.com>2022-08-02 20:12:34 +0200
commit3c2f4977e478dc7c6b243cab540ac2de4d0e6c84 (patch)
tree40a568397c2ade3d04e0aaa703ef5974b6087055 /planetwars-server/src/routes/matches.rs
parentaafb7856455313844a13a75c5171a649f6573feb (diff)
downloadplanetwars.dev-3c2f4977e478dc7c6b243cab540ac2de4d0e6c84.tar.xz
planetwars.dev-3c2f4977e478dc7c6b243cab540ac2de4d0e6c84.zip
add parameters to recent_matches api endpoint
Diffstat (limited to 'planetwars-server/src/routes/matches.rs')
-rw-r--r--planetwars-server/src/routes/matches.rs28
1 files changed, 25 insertions, 3 deletions
diff --git a/planetwars-server/src/routes/matches.rs b/planetwars-server/src/routes/matches.rs
index 03f6653..fde8471 100644
--- a/planetwars-server/src/routes/matches.rs
+++ b/planetwars-server/src/routes/matches.rs
@@ -1,4 +1,8 @@
-use axum::{extract::Path, Extension, Json};
+use axum::{
+ extract::{Path, Query},
+ Extension, Json,
+};
+use chrono::NaiveDateTime;
use hyper::StatusCode;
use serde::{Deserialize, Serialize};
use std::{path::PathBuf, sync::Arc};
@@ -24,10 +28,28 @@ pub struct ApiMatchPlayer {
bot_name: Option<String>,
}
-pub async fn list_public_matches(
+#[derive(Serialize, Deserialize)]
+pub struct ListRecentMatchesParams {
+ count: Option<usize>,
+ // TODO: should timezone be specified here?
+ // TODO: implement these
+ before: Option<NaiveDateTime>,
+ after: Option<NaiveDateTime>,
+}
+
+const MAX_NUM_RETURNED_MATCHES: usize = 100;
+const DEFAULT_NUM_RETURNED_MATCHES: usize = 50;
+
+pub async fn list_recent_matches(
+ Query(params): Query<ListRecentMatchesParams>,
conn: DatabaseConnection,
) -> Result<Json<Vec<ApiMatch>>, StatusCode> {
- matches::list_public_matches(100, &conn)
+ let count = std::cmp::min(
+ params.count.unwrap_or(DEFAULT_NUM_RETURNED_MATCHES),
+ MAX_NUM_RETURNED_MATCHES,
+ );
+
+ matches::list_public_matches(count as i64, &conn)
.map_err(|_| StatusCode::BAD_REQUEST)
.map(|matches| Json(matches.into_iter().map(match_data_to_api).collect()))
}