mirror of
https://github.com/djhackersdev/minime.git
synced 2026-04-17 06:46:02 -05:00
idz: Add TeamRepository
This commit is contained in:
parent
d1c3051a5e
commit
c6a67bd647
|
|
@ -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<Model.Tickets> {
|
||||
return new SqlTicketsRepository(this._conn);
|
||||
}
|
||||
|
|
|
|||
92
src/idz/db/team.ts
Normal file
92
src/idz/db/team.ts
Normal file
|
|
@ -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<Team>): Promise<Id<Team>> {
|
||||
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<Team>): Promise<Team> {
|
||||
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: Team): Promise<void> {
|
||||
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<Team>, ExtId<Team>]> {
|
||||
const id = generateId() as Id<Team>;
|
||||
const extId = generateExtId() as ExtId<Team>;
|
||||
|
||||
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<Team>): Promise<void> {
|
||||
const deleteSql = sql
|
||||
.delete("idz.team")
|
||||
.where("id", id)
|
||||
.toParams();
|
||||
|
||||
await this._conn.query(deleteSql);
|
||||
}
|
||||
}
|
||||
|
|
@ -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<Model.Team> }
|
||||
>;
|
||||
|
||||
export interface CarRepository {
|
||||
countCars(profileId: Id<Model.Profile>): Promise<number>;
|
||||
|
||||
|
|
@ -50,6 +57,18 @@ export interface ProfileRepository {
|
|||
create(profile: Model.Profile): Promise<Id<Model.Profile>>;
|
||||
}
|
||||
|
||||
export interface TeamRepository {
|
||||
find(extId: Model.ExtId<Model.Team>): Promise<Id<Model.Team>>;
|
||||
|
||||
load(id: Id<Model.Team>): Promise<Model.Team>;
|
||||
|
||||
save(id: Id<Model.Team>, team: Model.Team): Promise<void>;
|
||||
|
||||
create(team: TeamSpec): Promise<[Id<Model.Team>, Model.ExtId<Model.Team>]>;
|
||||
|
||||
delete(id: Id<Model.Team>): Promise<void>;
|
||||
}
|
||||
|
||||
// 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<Model.Story>;
|
||||
|
||||
teams(): TeamRepository;
|
||||
|
||||
tickets(): FacetRepository<Model.Tickets>;
|
||||
|
||||
timeAttack(): TimeAttackRepository;
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user