diff --git a/src/idz/db/titles.ts b/src/idz/db/titles.ts new file mode 100644 index 0000000..fb1da77 --- /dev/null +++ b/src/idz/db/titles.ts @@ -0,0 +1,51 @@ +import { Client } from "pg"; +import * as sql from "sql-bricks"; + +import { _findProfile } from "./_util"; +import { TitleCode, ExtId } from "../model/base"; +import { Profile } from "../model/profile"; +import { FlagRepository } from "../repo"; +import { generateId } from "../../db"; + +export class SqlTitlesRepository implements FlagRepository { + constructor(private readonly _conn: Client) {} + + async loadAll(extId: ExtId): Promise> { + const loadSql = sql + .select("t.title_no") + .from("idz.title_unlock t") + .join("idz.profile p", { "t.profile_id": "p.id" }) + .where("p.ext_id", extId) + .toParams(); + + const { rows } = await this._conn.query(loadSql); + const result = new Set(); + + for (const row of rows) { + result.add(row.title_no); + } + + return result; + } + + async saveAll(extId: ExtId, flags: Set): Promise { + const profileId = await _findProfile(this._conn, extId); + const existing = await this.loadAll(extId); + + for (const flag of flags) { + if (existing.has(flag)) { + continue; + } + + const saveSql = sql + .insert("idz.title_unlock", { + id: generateId(), + profile_id: profileId, + title_no: flag, + }) + .toParams(); + + await this._conn.query(saveSql); + } + } +}