import * as sql from "sql-bricks"; import { ClientBase } from "pg"; import { _findProfile } from "./_util"; import { ExtId } from "../model/base"; import { Profile } from "../model/profile"; import { Team } from "../model/team"; import { ProfileSpec, ProfileRepository } from "../repo"; import { generateExtId, generateId, Id } from "../../db"; import { AimeId } from "../../model"; function _extractRow(row: any): Profile { return { id: row.ext_id, teamId: 2 as ExtId, // TODO name: row.name, lv: row.lv, exp: row.exp, fame: row.fame, dpoint: row.dpoint, mileage: row.mileage, }; } export class SqlProfileRepository implements ProfileRepository { constructor(private readonly _conn: ClientBase) {} private async _tryLoadByAimeId( aimeId: AimeId ): Promise { const lookupSql = sql .select("p.*") .from("idz.profile p") .join("aime.player r", { "p.player_id": "r.id" }) .where("r.ext_id", aimeId) .toParams(); const { rows } = await this._conn.query(lookupSql); const row = rows[0]; if (row === undefined) { return undefined; } return _extractRow(row); } async discoverByAimeId(aimeId: AimeId): Promise { const result = await this._tryLoadByAimeId(aimeId); return result !== undefined; } async loadByAimeId(aimeId: AimeId): Promise { const result = await this._tryLoadByAimeId(aimeId); if (result === undefined) { throw new Error("Profile not found for Aime ID"); } return result; } async load(extId: ExtId): Promise { const loadSql = sql .select("p.*") .from("idz.profile p") .where("ext_id", extId) .toParams(); const { rows } = await this._conn.query(loadSql); return _extractRow(rows[0]); } async save(profile: Profile, timestamp: Date): Promise { const saveSql = sql .update("idz.profile", { lv: profile.lv, exp: profile.exp, fame: profile.fame, dpoint: profile.dpoint, mileage: profile.mileage, access_time: timestamp, }) .where("ext_id", profile.id) .toParams(); await this._conn.query(saveSql); } async create( aimeId: AimeId, profile: ProfileSpec, timestamp: Date ): Promise> { const findSql = sql .select("r.id") .from("aime.player r") .where("r.ext_id", aimeId) .toParams(); const { rows } = await this._conn.query(findSql); const row = rows[0]; if (row === undefined) { throw new Error("Aime ID not found"); } const id = generateId(); const extId = generateExtId() as ExtId; const playerId = row.id; const createSql = sql .insert("idz.profile", { id: id, player_id: playerId, ext_id: extId, name: profile.name, lv: profile.lv, exp: profile.exp, fame: profile.fame, dpoint: profile.dpoint, mileage: profile.mileage, register_time: timestamp, access_time: timestamp, }) .toParams(); await this._conn.query(createSql); return extId; } }