list only public matches in API

This commit is contained in:
Ilion Beyst 2022-07-30 19:49:08 +02:00
parent ee5c67c092
commit e8dbb01933
3 changed files with 46 additions and 24 deletions

View file

@ -89,17 +89,14 @@ pub struct MatchData {
pub match_players: Vec<MatchPlayer>, pub match_players: Vec<MatchPlayer>,
} }
pub fn list_matches(amount: i64, conn: &PgConnection) -> QueryResult<Vec<FullMatchData>> { /// Add player information to MatchBase instances
conn.transaction(|| { fn fetch_full_match_data(
let matches = matches::table matches: Vec<MatchBase>,
.order_by(matches::created_at.desc()) conn: &PgConnection,
.limit(amount) ) -> QueryResult<Vec<FullMatchData>> {
.get_results::<MatchBase>(conn)?;
let match_players = MatchPlayer::belonging_to(&matches) let match_players = MatchPlayer::belonging_to(&matches)
.left_join( .left_join(
bot_versions::table bot_versions::table.on(match_players::bot_version_id.eq(bot_versions::id.nullable())),
.on(match_players::bot_version_id.eq(bot_versions::id.nullable())),
) )
.left_join(bots::table.on(bot_versions::bot_id.eq(bots::id.nullable()))) .left_join(bots::table.on(bot_versions::bot_id.eq(bots::id.nullable())))
.order_by(( .order_by((
@ -119,6 +116,29 @@ pub fn list_matches(amount: i64, conn: &PgConnection) -> QueryResult<Vec<FullMat
.collect(); .collect();
Ok(res) Ok(res)
}
// TODO: this method should disappear
pub fn list_matches(amount: i64, conn: &PgConnection) -> QueryResult<Vec<FullMatchData>> {
conn.transaction(|| {
let matches = matches::table
.order_by(matches::created_at.desc())
.limit(amount)
.get_results::<MatchBase>(conn)?;
fetch_full_match_data(matches, conn)
})
}
pub fn list_public_matches(amount: i64, conn: &PgConnection) -> QueryResult<Vec<FullMatchData>> {
conn.transaction(|| {
let matches = matches::table
.filter(matches::is_public.eq(true))
.order_by(matches::created_at.desc())
.limit(amount)
.get_results::<MatchBase>(conn)?;
fetch_full_match_data(matches, conn)
}) })
} }

View file

@ -129,7 +129,7 @@ pub fn api() -> Router {
"/bots/:bot_name/upload", "/bots/:bot_name/upload",
post(routes::bots::upload_code_multipart), post(routes::bots::upload_code_multipart),
) )
.route("/matches", get(routes::matches::list_matches)) .route("/matches", get(routes::matches::list_public_matches))
.route("/matches/:match_id", get(routes::matches::get_match_data)) .route("/matches/:match_id", get(routes::matches::get_match_data))
.route( .route(
"/matches/:match_id/log", "/matches/:match_id/log",

View file

@ -23,8 +23,10 @@ pub struct ApiMatchPlayer {
bot_name: Option<String>, bot_name: Option<String>,
} }
pub async fn list_matches(conn: DatabaseConnection) -> Result<Json<Vec<ApiMatch>>, StatusCode> { pub async fn list_public_matches(
matches::list_matches(100, &conn) conn: DatabaseConnection,
) -> Result<Json<Vec<ApiMatch>>, StatusCode> {
matches::list_public_matches(100, &conn)
.map_err(|_| StatusCode::BAD_REQUEST) .map_err(|_| StatusCode::BAD_REQUEST)
.map(|matches| Json(matches.into_iter().map(match_data_to_api).collect())) .map(|matches| Json(matches.into_iter().map(match_data_to_api).collect()))
} }