diff --git a/src/idz/db/settings.ts b/src/idz/db/settings.ts new file mode 100644 index 0000000..3634e57 --- /dev/null +++ b/src/idz/db/settings.ts @@ -0,0 +1,49 @@ +import { Client } from "pg"; +import * as sql from "sql-bricks-postgres"; + +import { _findProfile } from "./_util"; +import { ExtId } from "../model/base"; +import { Settings } from "../model/settings"; +import { Profile } from "../model/profile"; +import { FacetRepository } from "../repo"; + +export class SqlSettingsRepository implements FacetRepository { + constructor(private readonly _conn: Client) {} + + async load(extId: ExtId): Promise { + const loadSql = sql + .select("s.*") + .from("idz.profile p") + .join("idz.settings s", { "p.id": "s.id" }) + .where("p.ext_id", extId) + .toParams(); + + const { rows } = await this._conn.query(loadSql); + const row = rows[0]; + + return { + music: row.music, + pack: row.pack, + paperCup: row.paper_cup, + gauges: row.gauges, + }; + } + + async save(extId: ExtId, settings: Settings): Promise { + const profileId = await _findProfile(this._conn, extId); + + const saveSql = sql + .insert("idz.settings", { + id: profileId, + music: settings.music, + pack: settings.pack, + paper_cup: settings.paperCup, + gauges: settings.gauges, + }) + .onConflict("id") + .doUpdate("music", "pack", "paper_cup", "gauges") + .toParams(); + + await this._conn.query(saveSql); + } +}