From c6a67bd647cfa648e8121111d0cd84eeeb76df66 Mon Sep 17 00:00:00 2001 From: Tau Date: Sun, 26 May 2019 20:06:15 -0400 Subject: [PATCH] idz: Add TeamRepository --- src/idz/db/index.ts | 5 +++ src/idz/db/team.ts | 92 +++++++++++++++++++++++++++++++++++++++++++++ src/idz/repo.ts | 21 +++++++++++ 3 files changed, 118 insertions(+) create mode 100644 src/idz/db/team.ts diff --git a/src/idz/db/index.ts b/src/idz/db/index.ts index 1fc27b3..5ff97f6 100644 --- a/src/idz/db/index.ts +++ b/src/idz/db/index.ts @@ -8,6 +8,7 @@ import { SqlMissionsRepository } from "./missions"; import { SqlProfileRepository } from "./profile"; import { SqlSettingsRepository } from "./settings"; import { SqlStoryRepository } from "./story"; +import { SqlTeamRepository } from "./team"; import { SqlTicketsRepository } from "./tickets"; import { SqlTimeAttackRepository } from "./timeAttack"; import { SqlTitlesRepository } from "./titles"; @@ -51,6 +52,10 @@ class TransactionImpl implements Repo.Transaction { return new SqlStoryRepository(this._conn); } + teams(): Repo.TeamRepository { + return new SqlTeamRepository(this._conn); + } + tickets(): Repo.FacetRepository { return new SqlTicketsRepository(this._conn); } diff --git a/src/idz/db/team.ts b/src/idz/db/team.ts new file mode 100644 index 0000000..9671265 --- /dev/null +++ b/src/idz/db/team.ts @@ -0,0 +1,92 @@ +import { ClientBase } from "pg"; +import * as sql from "sql-bricks"; + +import { ExtId } from "../model/base"; +import { Team } from "../model/team"; +import { TeamSpec, TeamRepository } from "../repo"; +import { Id, generateExtId, generateId } from "../../db"; + +export class SqlTeamRepository implements TeamRepository { + constructor(private readonly _conn: ClientBase) {} + + async find(extId: ExtId): Promise> { + const findSql = sql + .select("t.id") + .from("idz.team t") + .where("t.ext_id", extId) + .toParams(); + + const { rows } = await this._conn.query(findSql); + const row = rows[0]; + + if (row === undefined) { + throw new Error(`Team not found for ExtID ${extId}`); + } + + return row.id; + } + + async load(id: Id): Promise { + const loadSql = sql + .select("t.*") + .from("idz.team t") + .where("t.id", id) + .toParams(); + + const { rows } = await this._conn.query(loadSql); + const row = rows[0]; + + if (row == undefined) { + throw new Error("Team not found"); + } + + return { + extId: row.ext_id, + name: row.name, + nameBg: row.name_bg, + nameFx: row.name_fx, + registerTime: new Date(row.register_time), + }; + } + + async save(id: Id, team: Team): Promise { + const saveSql = sql + .update("idz.team", { + name_bg: team.nameBg, + name_fx: team.nameFx, + }) + .where("id", id) + .toParams(); + + await this._conn.query(saveSql); + } + + async create(team: TeamSpec): Promise<[Id, ExtId]> { + const id = generateId() as Id; + const extId = generateExtId() as ExtId; + + const createSql = sql + .insert("idz.team", { + id: id, + ext_id: extId, + name: team.name, + name_bg: team.nameBg, + name_fx: team.nameFx, + register_time: team.registerTime, + }) + .toParams(); + + await this._conn.query(createSql); + + return [id, extId]; + } + + async delete(id: Id): Promise { + const deleteSql = sql + .delete("idz.team") + .where("id", id) + .toParams(); + + await this._conn.query(deleteSql); + } +} diff --git a/src/idz/repo.ts b/src/idz/repo.ts index f6c0f0b..0bf5307 100644 --- a/src/idz/repo.ts +++ b/src/idz/repo.ts @@ -1,7 +1,14 @@ +import { Subtract } from "utility-types"; + import * as Model from "./model"; import { AimeId } from "../model"; import { Id } from "../db"; +export type TeamSpec = Subtract< + Model.Team, + { extId: Model.ExtId } +>; + export interface CarRepository { countCars(profileId: Id): Promise; @@ -50,6 +57,18 @@ export interface ProfileRepository { create(profile: Model.Profile): Promise>; } +export interface TeamRepository { + find(extId: Model.ExtId): Promise>; + + load(id: Id): Promise; + + save(id: Id, team: Model.Team): Promise; + + create(team: TeamSpec): Promise<[Id, Model.ExtId]>; + + delete(id: Id): Promise; +} + // TODO extend and factorize export interface TopTenResult { driverName: string; @@ -89,6 +108,8 @@ export interface Repositories { // Also not a facet. w/e one step at a time. story(): FacetRepository; + teams(): TeamRepository; + tickets(): FacetRepository; timeAttack(): TimeAttackRepository;