diff options
author | Ilion Beyst <ilion.beyst@gmail.com> | 2022-07-30 19:49:08 +0200 |
---|---|---|
committer | Ilion Beyst <ilion.beyst@gmail.com> | 2022-07-30 19:49:08 +0200 |
commit | e8dbb019337287040891231701b7e50593c37187 (patch) | |
tree | 13729b8d11389ff9aaa2d356cd0d784ca94517b3 /planetwars-server/src/db | |
parent | ee5c67c092acc57a6ef2b8f7934ba827df43add6 (diff) | |
download | planetwars.dev-e8dbb019337287040891231701b7e50593c37187.tar.xz planetwars.dev-e8dbb019337287040891231701b7e50593c37187.zip |
list only public matches in API
Diffstat (limited to 'planetwars-server/src/db')
-rw-r--r-- | planetwars-server/src/db/matches.rs | 64 |
1 files changed, 42 insertions, 22 deletions
diff --git a/planetwars-server/src/db/matches.rs b/planetwars-server/src/db/matches.rs index be2a8be..c884a63 100644 --- a/planetwars-server/src/db/matches.rs +++ b/planetwars-server/src/db/matches.rs @@ -89,6 +89,36 @@ pub struct MatchData { pub match_players: Vec<MatchPlayer>, } +/// Add player information to MatchBase instances +fn fetch_full_match_data( + matches: Vec<MatchBase>, + conn: &PgConnection, +) -> QueryResult<Vec<FullMatchData>> { + let match_players = MatchPlayer::belonging_to(&matches) + .left_join( + bot_versions::table.on(match_players::bot_version_id.eq(bot_versions::id.nullable())), + ) + .left_join(bots::table.on(bot_versions::bot_id.eq(bots::id.nullable()))) + .order_by(( + match_players::match_id.asc(), + match_players::player_id.asc(), + )) + .load::<FullMatchPlayerData>(conn)? + .grouped_by(&matches); + + let res = matches + .into_iter() + .zip(match_players.into_iter()) + .map(|(base, players)| FullMatchData { + base, + match_players: players.into_iter().collect(), + }) + .collect(); + + Ok(res) +} + +// TODO: this method should disappear pub fn list_matches(amount: i64, conn: &PgConnection) -> QueryResult<Vec<FullMatchData>> { conn.transaction(|| { let matches = matches::table @@ -96,29 +126,19 @@ pub fn list_matches(amount: i64, conn: &PgConnection) -> QueryResult<Vec<FullMat .limit(amount) .get_results::<MatchBase>(conn)?; - let match_players = MatchPlayer::belonging_to(&matches) - .left_join( - bot_versions::table - .on(match_players::bot_version_id.eq(bot_versions::id.nullable())), - ) - .left_join(bots::table.on(bot_versions::bot_id.eq(bots::id.nullable()))) - .order_by(( - match_players::match_id.asc(), - match_players::player_id.asc(), - )) - .load::<FullMatchPlayerData>(conn)? - .grouped_by(&matches); - - let res = matches - .into_iter() - .zip(match_players.into_iter()) - .map(|(base, players)| FullMatchData { - base, - match_players: players.into_iter().collect(), - }) - .collect(); + fetch_full_match_data(matches, conn) + }) +} - Ok(res) +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) }) } |