add started & ended timestamps to game table

This commit is contained in:
Nathaniel Tampus
2023-04-05 21:50:34 +08:00
parent baebc04510
commit 3f851602ea
4 changed files with 27 additions and 12 deletions

View File

@@ -21,6 +21,8 @@ export const INIT_TABLES = /* sql */ `
white_id INT REFERENCES "user",
white_name VARCHAR(32),
black_id INT REFERENCES "user",
black_name VARCHAR(32)
black_name VARCHAR(32),
started_at TIMESTAMP NOT NULL,
ended_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
`;

View File

@@ -3,8 +3,6 @@ import type { Game, User } from "@chessu/types";
export const activeGames: Array<Game> = [];
// todo: join user and game relationship
export const save = async (game: Game) => {
try {
const white: User = {};
@@ -20,7 +18,7 @@ export const save = async (game: Game) => {
black.id = game.black?.id;
}
const res = await db.query(
`INSERT INTO "game"(winner, end_reason, pgn, white_id, white_name, black_id, black_name) VALUES($1, $2, $3, $4, $5, $6, $7) RETURNING *`,
`INSERT INTO "game"(winner, end_reason, pgn, white_id, white_name, black_id, black_name, started_at) VALUES($1, $2, $3, $4, $5, $6, $7, $8) RETURNING *`,
[
game.winner || null,
game.endReason || null,
@@ -28,7 +26,8 @@ export const save = async (game: Game) => {
white.id || null,
white.name || null,
black.id || null,
black.name || null
black.name || null,
new Date(game.startedAt as number)
]
);
if (black.id || white.id) {
@@ -65,7 +64,9 @@ export const save = async (game: Game) => {
black: {
id: res.rows[0].black_id || undefined,
name: res.rows[0].black_name || undefined
}
},
startedAt: res.rows[0].started_at.getTime(),
endedAt: res.rows[0].ended_at?.getTime() || undefined
} as Game;
} catch (err: unknown) {
console.log(err);
@@ -76,7 +77,7 @@ export const save = async (game: Game) => {
export const findById = async (id: number) => {
try {
const res = await db.query(
`SELECT game.id, game.winner, game.end_reason, game.pgn, white_user.id AS white_id, COALESCE(white_user.name, game.white_name) AS white_name, black_user.id AS black_id, COALESCE(black_user.name, game.black_name) AS black_name FROM game LEFT JOIN "user" white_user ON white_user.id = game.white_id LEFT JOIN "user" black_user ON black_user.id = game.black_id WHERE game.id=$1`,
`SELECT game.id, game.winner, game.end_reason, game.pgn, white_user.id AS white_id, COALESCE(white_user.name, game.white_name) AS white_name, black_user.id AS black_id, started_at, ended_at, COALESCE(black_user.name, game.black_name) AS black_name FROM game LEFT JOIN "user" white_user ON white_user.id = game.white_id LEFT JOIN "user" black_user ON black_user.id = game.black_id WHERE game.id=$1`,
[id]
);
if (res.rowCount) {
@@ -86,7 +87,9 @@ export const findById = async (id: number) => {
endReason: res.rows[0].end_reason,
pgn: res.rows[0].pgn,
white: { id: res.rows[0].white_id || undefined, name: res.rows[0].white_name },
black: { id: res.rows[0].black_id || undefined, name: res.rows[0].black_name }
black: { id: res.rows[0].black_id || undefined, name: res.rows[0].black_name },
startedAt: res.rows[0].started_at.getTime(),
endedAt: res.rows[0].ended_at?.getTime() || undefined
} as Game;
} else return null;
} catch (err: unknown) {
@@ -102,7 +105,7 @@ export const findByUserId = async (id: number, limit = 10) => {
try {
// TODO: pagination
const res = await db.query(
`SELECT game.id, game.winner, game.end_reason, game.pgn, white_user.id AS white_id, COALESCE(white_user.name, game.white_name) AS white_name, black_user.id AS black_id, COALESCE(black_user.name, game.black_name) AS black_name FROM game LEFT JOIN "user" white_user ON white_user.id = game.white_id LEFT JOIN "user" black_user ON black_user.id = game.black_id WHERE white_user.id=$1 OR black_user.id=$1 ORDER BY id DESC LIMIT $2`,
`SELECT game.id, game.winner, game.end_reason, game.pgn, white_user.id AS white_id, COALESCE(white_user.name, game.white_name) AS white_name, black_user.id AS black_id, started_at, ended_at, COALESCE(black_user.name, game.black_name) AS black_name FROM game LEFT JOIN "user" white_user ON white_user.id = game.white_id LEFT JOIN "user" black_user ON black_user.id = game.black_id WHERE white_user.id=$1 OR black_user.id=$1 ORDER BY id DESC LIMIT $2`,
[id, limit]
);
return res.rows.map((r) => {
@@ -112,7 +115,9 @@ export const findByUserId = async (id: number, limit = 10) => {
endReason: r.end_reason,
pgn: r.pgn,
white: { id: r.white_id || undefined, name: r.white_name },
black: { id: r.black_id || undefined, name: r.black_name }
black: { id: r.black_id || undefined, name: r.black_name },
startedAt: r.started_at.getTime(),
endedAt: r.ended_at?.getTime() || undefined
} as Game;
});
} catch (err: unknown) {
@@ -131,7 +136,9 @@ export const update = async (id: number, data: string) => {
endReason: res.rows[0].end_reason,
pgn: res.rows[0].pgn,
white: { id: res.rows[0].white_id, name: res.rows[0].white_name },
black: { id: res.rows[0].black_id, name: res.rows[0].black_name }
black: { id: res.rows[0].black_id, name: res.rows[0].black_name },
startedAt: res.rows[0].started_at.getTime(),
endedAt: res.rows[0].ended_at?.getTime() || undefined
} as Game;
} catch (err: unknown) {
console.log(err);
@@ -148,7 +155,9 @@ export const remove = async (id: number) => {
endReason: res.rows[0].end_reason,
pgn: res.rows[0].pgn,
white: { id: res.rows[0].white_id, name: res.rows[0].white_name },
black: { id: res.rows[0].black_id, name: res.rows[0].black_name }
black: { id: res.rows[0].black_id, name: res.rows[0].black_name },
startedAt: res.rows[0].started_at.getTime(),
endedAt: res.rows[0].ended_at?.getTime() || undefined
} as Game;
} catch (err: unknown) {
console.log(err);