aboutsummaryrefslogtreecommitdiff
path: root/planetwars-server/src/db/matches.rs
diff options
context:
space:
mode:
authorIlion Beyst <ilion.beyst@gmail.com>2022-08-08 18:31:11 +0200
committerIlion Beyst <ilion.beyst@gmail.com>2022-08-08 18:31:11 +0200
commitcf52ab6f7f4f96d552b2c6c828fc40704460046f (patch)
tree4207929dfc4401979036252ee89744e780c393e4 /planetwars-server/src/db/matches.rs
parentdb7980504f75371965fe887f2e459c8554ac09d0 (diff)
downloadplanetwars.dev-cf52ab6f7f4f96d552b2c6c828fc40704460046f.tar.xz
planetwars.dev-cf52ab6f7f4f96d552b2c6c828fc40704460046f.zip
implement before and after filters for matches
Diffstat (limited to 'planetwars-server/src/db/matches.rs')
-rw-r--r--planetwars-server/src/db/matches.rs35
1 files changed, 30 insertions, 5 deletions
diff --git a/planetwars-server/src/db/matches.rs b/planetwars-server/src/db/matches.rs
index b806dd3..22cb564 100644
--- a/planetwars-server/src/db/matches.rs
+++ b/planetwars-server/src/db/matches.rs
@@ -130,13 +130,34 @@ pub fn list_matches(amount: i64, conn: &PgConnection) -> QueryResult<Vec<FullMat
})
}
-pub fn list_public_matches(amount: i64, conn: &PgConnection) -> QueryResult<Vec<FullMatchData>> {
+pub fn list_public_matches(
+ amount: i64,
+ before: Option<NaiveDateTime>,
+ after: Option<NaiveDateTime>,
+ conn: &PgConnection,
+) -> QueryResult<Vec<FullMatchData>> {
conn.transaction(|| {
- let matches = matches::table
+ // TODO: how can this common logic be abstracted?
+ // TODO: this is not nice. Replace this with proper cursor logic.
+ let mut query = matches::table
.filter(matches::is_public.eq(true))
- .order_by(matches::created_at.desc())
- .limit(amount)
- .get_results::<MatchBase>(conn)?;
+ .into_boxed();
+
+ query = match (before, after) {
+ (None, None) => query.order_by(matches::created_at.desc()),
+ (Some(before), None) => query
+ .filter(matches::created_at.lt(before))
+ .order_by(matches::created_at.desc()),
+ (None, Some(after)) => query
+ .filter(matches::created_at.gt(after))
+ .order_by(matches::created_at.asc()),
+ (Some(before), Some(after)) => query
+ .filter(matches::created_at.lt(before))
+ .filter(matches::created_at.gt(after))
+ .order_by(matches::created_at.desc()),
+ };
+
+ let matches = query.limit(amount).get_results::<MatchBase>(conn)?;
fetch_full_match_data(matches, conn)
})
@@ -145,11 +166,15 @@ pub fn list_public_matches(amount: i64, conn: &PgConnection) -> QueryResult<Vec<
pub fn list_bot_matches(
bot_id: i32,
amount: i64,
+ before: Option<NaiveDateTime>,
+ after: Option<NaiveDateTime>,
conn: &PgConnection,
) -> QueryResult<Vec<FullMatchData>> {
conn.transaction(|| {
let matches = matches::table
.filter(matches::is_public.eq(true))
+ .filter(matches::created_at.nullable().lt(before))
+ .filter(matches::created_at.nullable().gt(after))
.order_by(matches::created_at.desc())
.inner_join(match_players::table)
.inner_join(