Skip to content

Commit 11604af

Browse files
committed
List recent matches endpoint
1 parent 0220f36 commit 11604af

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/database/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::{str::FromStr, time::Duration};
22
use std::collections::HashSet;
33

44
use mars_api_rs_macro::IdentifiableDocument;
5+
use mongodb::options::FindOptions;
56
use mongodb::{options::{ClientOptions, FindOneOptions, UpdateOptions}, Client, Collection, bson::{doc, oid::ObjectId}, Cursor, results::DeleteResult};
67
use models::tag::Tag;
78
use rand::Rng;
@@ -202,6 +203,12 @@ impl Database {
202203
where R: CollectionOwner<R> + Serialize + IdentifiableDocument + DeserializeOwned + Unpin + Send + Sync {
203204
R::get_collection(&self).find_one(doc! { "nameLower": name.to_lowercase() }, None).await.unwrap_or(None)
204205
}
206+
207+
pub async fn get_recent_matches(&self, limit: i64) -> Vec<Match> {
208+
let opts = FindOptions::builder().sort(doc! { "loadedAt": -1 }).limit(limit).build();
209+
let cursor = self.matches.find(doc! {}, Some(opts)).await.ok();
210+
Self::consume_cursor_into_owning_vec_option(cursor).await
211+
}
205212
}
206213

207214
const DB_NAME: &'static str = "mars-api";

src/http/match/mod.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ pub async fn matches(
1515
Ok(JsonResponder::ok(cached_match))
1616
}
1717

18+
#[get("/?<limit>")]
19+
pub async fn recent_matches(
20+
state: &State<MarsAPIState>,
21+
limit: Option<i64>,
22+
) -> Result<JsonResponder<Vec<Match>>, ApiErrorResponder> {
23+
let limit = limit.unwrap_or(5).min(10);
24+
let matches = state.database.get_recent_matches(limit).await;
25+
Ok(JsonResponder::ok(matches))
26+
}
27+
1828
pub fn mount(rocket_build: Rocket<Build>) -> Rocket<Build> {
19-
rocket_build.mount("/mc/matches", routes![matches])
29+
rocket_build.mount("/mc/matches", routes![matches, recent_matches])
2030
}

0 commit comments

Comments
 (0)