From 4db3d94ee898fbaa6f26109d8c2fa960c3c2f5bb Mon Sep 17 00:00:00 2001 From: Tau Date: Sat, 30 Nov 2019 14:13:49 -0500 Subject: [PATCH] idz: Un-hardcode result limit in repo layer Minor design nit but I should probably take my own code review advice. --- src/idz/handler/loadTopTen.ts | 2 +- src/idz/repo.ts | 5 +++-- src/idz/sql/timeAttack.ts | 7 ++++--- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/idz/handler/loadTopTen.ts b/src/idz/handler/loadTopTen.ts index 0c43e92..c5d1c65 100644 --- a/src/idz/handler/loadTopTen.ts +++ b/src/idz/handler/loadTopTen.ts @@ -18,7 +18,7 @@ export async function loadTopTen( } const { routeNo, minTimestamp } = selector; - const src = await w.timeAttack().loadTopTen(routeNo, minTimestamp); + const src = await w.timeAttack().loadTop(routeNo, minTimestamp, 10); if (src.length === 0) { continue; diff --git a/src/idz/repo.ts b/src/idz/repo.ts index c39834f..7bb5b0c 100644 --- a/src/idz/repo.ts +++ b/src/idz/repo.ts @@ -116,9 +116,10 @@ export interface TopTenResult { } export interface TimeAttackRepository { - loadTopTen( + loadTop( routeNo: Model.RouteNo, - minTimestamp: Date + minTimestamp: Date, + limit: number ): Promise; loadAll(profileId: Id): Promise; diff --git a/src/idz/sql/timeAttack.ts b/src/idz/sql/timeAttack.ts index 975310d..0916cc9 100644 --- a/src/idz/sql/timeAttack.ts +++ b/src/idz/sql/timeAttack.ts @@ -24,9 +24,10 @@ function _extractRow(row: Row): TimeAttackScore { export class SqlTimeAttackRepository implements TimeAttackRepository { constructor(private readonly _txn: Transaction) {} - async loadTopTen( + async loadTop( routeNo: RouteNo, - minTimestamp: Date + minTimestamp: Date, + limit: number ): Promise { // We're not using an ORM here so this join-heavy SQL is unfortunately // going to be a boilerplated mess. @@ -55,7 +56,7 @@ export class SqlTimeAttackRepository implements TimeAttackRepository { .where("ta.route_no", routeNo) .where(sql.gt("ta.timestamp", minTimestamp)) .orderBy(["ta.total_time asc", "ta.timestamp asc"]) - .limit(10); + .limit(limit); const rows = await this._txn.fetchRows(loadSql);