idz: Add idz_my_chara table

This commit is contained in:
BemaniWitch 2020-10-29 18:36:21 -04:00 committed by Tau
parent d19f1ac004
commit 63cd59e10e
7 changed files with 79 additions and 0 deletions

View File

@ -278,3 +278,12 @@ create table "idz_weekly_missions" (
"progress_right" integer not null,
"params_right" integer not null
);
create table "idz_my_chara" (
"id" integer primary key not null,
"profile_id" integer not null
references "idz_profile"("id")
on delete cascade,
"my_chara_no" integer not null,
constraint "idz_my_chara_uq" unique ("profile_id", "my_chara_no")
);

View File

@ -1,3 +1,12 @@
create table "idz_my_chara" (
"id" integer primary key not null,
"profile_id" integer not null
references "idz_profile"("id")
on delete cascade,
"my_chara_no" integer not null,
constraint "idz_my_chara_uq" unique ("profile_id", "my_chara_no")
);
create table "new_idz_settings" (
"id" integer primary key not null
references "idz_profile"("id")

View File

@ -4,6 +4,7 @@
export type BackgroundCode = number & { __brand: "backgroundCode" };
export type CourseNo = number & { __brand: "courseNo" };
export type ExtId<T> = number & { __extId: T };
export type MyCharaCode = number & { __brand: "myCharaCode" };
export type RouteNo = number & { __brand: "routeNo" };
export type StampCode = number & { __brand: "stampCode" };
export type TitleCode = number & { __brand: "titleCode" };

View File

@ -2,6 +2,7 @@ export {
BackgroundCode,
CourseNo,
ExtId,
MyCharaCode,
RouteNo,
StampCode,
TitleCode,

View File

@ -177,6 +177,8 @@ export interface Repositories {
// not really a facet tbh
missions(): FacetRepository<Model.MissionState>;
myChara(): FlagRepository<Model.MyCharaCode>;
profile(): ProfileRepository;
settings(): FacetRepository<Model.Settings>;

View File

@ -3,6 +3,7 @@ import { SqlCarRepository } from "./car";
import { SqlCharaRepository } from "./chara";
import { SqlCoursePlaysRepository } from "./coursePlays";
import { SqlMissionsRepository } from "./missions";
import { SqlMyCharaRepository } from "./myChara";
import { SqlProfileRepository } from "./profile";
import { SqlSettingsRepository } from "./settings";
import { SqlStampsRepository } from "./stamps";
@ -43,6 +44,10 @@ export class SqlRepositories implements Repo.Repositories {
return new SqlMissionsRepository(this._txn);
}
myChara(): Repo.FlagRepository<Model.MyCharaCode> {
return new SqlMyCharaRepository(this._txn);
}
profile(): Repo.ProfileRepository {
return new SqlProfileRepository(this._txn);
}

View File

@ -0,0 +1,52 @@
import sql from "sql-bricks-postgres";
import { MyCharaCode } from "../model/base";
import { Profile } from "../model/profile";
import { FlagRepository } from "../repo";
import { Id } from "../../../model";
import { Transaction } from "../../../sql";
export class SqlMyCharaRepository implements FlagRepository<MyCharaCode> {
constructor(private readonly _txn: Transaction) {}
async loadAll(id: Id<Profile>): Promise<Set<MyCharaCode>> {
const loadSql = sql
.select("mc.my_chara_no")
.from("idz_mychara_unlock mc")
.join("idz_profile p", { "mc.profile_id": "p.id" })
.where("p.id", id);
const rows = await this._txn.fetchRows(loadSql);
const result = new Set<MyCharaCode>();
for (const row of rows) {
result.add(parseInt(row.my_chara_no!) as MyCharaCode);
}
return result;
}
async saveAll(
profileId: Id<Profile>,
flags: Set<MyCharaCode>
): Promise<void> {
const existing = await this.loadAll(profileId);
for (const flag of flags) {
if (existing.has(flag)) {
continue;
}
const saveSql = sql
.insert("idz_mychara_unlock", {
id: this._txn.generateId(),
profile_id: profileId,
my_chara_no: flag,
})
.onConflict("profile_id", "my_chara_no")
.doNothing();
await this._txn.modify(saveSql);
}
}
}