idz: Add SqlCoursePlaysRepository

This commit is contained in:
Tau
2019-11-06 17:28:30 -05:00
parent ceaeebc0b4
commit 651d826963
6 changed files with 62 additions and 8 deletions
+51
View File
@@ -0,0 +1,51 @@
import { Client } from "pg";
import * as sql from "sql-bricks-postgres";
import { _findProfile } from "./_util";
import { CourseNo, ExtId } from "../model/base";
import { Profile } from "../model/profile";
import { CoursePlaysRepository } from "../repo";
import { generateId } from "../../db";
export class SqlCoursePlaysRepository implements CoursePlaysRepository {
constructor(private readonly _conn: Client) {}
async loadAll(extId: ExtId<Profile>): Promise<Map<CourseNo, number>> {
const loadSql = sql
.select("cp.course_no", "cp.count")
.from("idz.course_plays cp")
.join("idz.profile p", { "cp.profile_id": "p.id" })
.where("p.ext_id", extId)
.toParams();
const { rows } = await this._conn.query(sql);
const result = new Map();
for (const row of rows) {
result.set(row.course_no, row.count);
}
return result;
}
async saveAll(
extId: ExtId<Profile>,
plays: Map<CourseNo, number>
): Promise<void> {
const profileId = await _findProfile(this._conn, extId);
for (const [k, v] of plays) {
const saveSql = sql
.insert("idz.course_plays", {
id: generateId(),
profile_id: profileId,
course_no: k,
count: v,
})
.onConflict("profile_id", "course_no")
.doUpdate("count");
await this._conn.query(saveSql);
}
}
}
+3 -3
View File
@@ -1,7 +1,7 @@
import { car } from "./_car";
import { mission } from "./_mission";
import { RequestCode } from "./_defs";
import { BackgroundCode, ExtId, TitleCode } from "../model/base";
import { BackgroundCode, CourseNo, ExtId, TitleCode } from "../model/base";
import { Profile } from "../model/profile";
import { SaveProfileRequest } from "../request/saveProfile";
import { bitmap } from "./_bitmap";
@@ -29,10 +29,10 @@ export function saveProfile(buf: Buffer): SaveProfileRequest {
storyRows.push(row);
}
const coursePlays = new Map<number, number>();
const coursePlays = new Map<CourseNo, number>();
for (let i = 0; i < 16; i++) {
coursePlays.set(i, buf.readUInt16LE(0x04c0 + 2 * i));
coursePlays.set(i as CourseNo, buf.readUInt16LE(0x04c0 + 2 * i));
}
const freeCar = {
+1
View File
@@ -2,6 +2,7 @@
// exist at run time, we just pretend they do.
export type BackgroundCode = number & { __brand: "backgroundCode" };
export type CourseNo = number & { __brand: "courseNo" };
export type ExtId<T> = number & { __extId: T };
export type RouteNo = number & { __brand: "routeNo" };
export type TitleCode = number & { __brand: "titleCode" };
+1 -1
View File
@@ -1,4 +1,4 @@
export { BackgroundCode, ExtId, TitleCode } from "./base";
export { BackgroundCode, CourseNo, ExtId, TitleCode } from "./base";
export { Car, CarSelector } from "./car";
export { Chara } from "./chara";
export { MissionState, MissionGrid } from "./mission";
+4 -2
View File
@@ -27,11 +27,13 @@ export interface CarRepository {
}
export interface CoursePlaysRepository {
loadAll(profileId: Model.ExtId<Model.Profile>): Promise<Map<number, number>>;
loadAll(
profileId: Model.ExtId<Model.Profile>
): Promise<Map<Model.CourseNo, number>>;
saveAll(
profileId: Model.ExtId<Model.Profile>,
counts: Map<number, number>
counts: Map<Model.CourseNo, number>
): Promise<void>;
}
+2 -2
View File
@@ -1,4 +1,4 @@
import { BackgroundCode, ExtId, TitleCode } from "../model/base";
import { BackgroundCode, CourseNo, ExtId, TitleCode } from "../model/base";
import { Car } from "../model/car";
import { MissionState } from "../model/mission";
import { Profile } from "../model/profile";
@@ -18,7 +18,7 @@ export interface SaveProfileRequest {
title: TitleCode;
titles: TitleCode[];
background: BackgroundCode;
coursePlays: Map<number, number>;
coursePlays: Map<CourseNo, number>;
missions: MissionState;
car: Car;
story: Story;