update game queries

This commit is contained in:
Nathaniel Tampus
2023-04-02 15:01:11 +08:00
parent c7f3abf29c
commit f18d6d1e8c

View File

@@ -1,5 +1,5 @@
import { db } from "../index.js"; import { db } from "../index.js";
import type { Game } from "@chessu/types"; import type { Game, User } from "@chessu/types";
export const activeGames: Array<Game> = []; export const activeGames: Array<Game> = [];
@@ -7,16 +7,28 @@ export const activeGames: Array<Game> = [];
export const create = async (game: Game) => { export const create = async (game: Game) => {
try { try {
const white: User = {};
const black: User = {};
if (typeof game.white?.id === "string") {
white.name = game.white?.name;
} else {
white.id = game.white?.id;
}
if (typeof game.black?.id === "string") {
black.name = game.black?.name;
} else {
black.id = game.black?.id;
}
const res = await db.query( 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) VALUES($1, $2, $3, $4, $5, $6, $7) RETURNING *`,
[ [
game.winner || null, game.winner || null,
game.endReason || null, game.endReason || null,
game.pgn, game.pgn,
game.white?.id || null, white.id || null,
game.white?.name || null, white.name || null,
game.black?.id || null, black.id || null,
game.black?.name || null black.name || null
] ]
); );
return { return {
@@ -35,15 +47,18 @@ export const create = async (game: Game) => {
export const findById = async (id: number) => { export const findById = async (id: number) => {
try { try {
const res = await db.query(`SELECT * FROM "game" WHERE id=$1`, [id]); 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`,
[id]
);
if (res.rowCount) { if (res.rowCount) {
return { return {
id: res.rows[0].id, id: res.rows[0].id,
winner: res.rows[0].winner, winner: res.rows[0].winner,
endReason: res.rows[0].end_reason, endReason: res.rows[0].end_reason,
pgn: res.rows[0].pgn, pgn: res.rows[0].pgn,
white: { id: res.rows[0].white_id, name: res.rows[0].white_name }, white: { id: res.rows[0].white_id || null, 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 || null, name: res.rows[0].black_name }
} as Game; } as Game;
} else return null; } else return null;
} catch (err: unknown) { } catch (err: unknown) {
@@ -58,7 +73,7 @@ export const findByUserId = async (id: number, limit = 10) => {
} }
try { try {
const res = await db.query( const res = await db.query(
`SELECT * FROM "game" WHERE white_id=$1 OR black_id=$1 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, 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 LIMIT $2`,
[id, limit] [id, limit]
); );
return res.rows.map((r) => { return res.rows.map((r) => {
@@ -67,8 +82,8 @@ export const findByUserId = async (id: number, limit = 10) => {
winner: r.winner, winner: r.winner,
endReason: r.end_reason, endReason: r.end_reason,
pgn: r.pgn, pgn: r.pgn,
white: { id: r.white_id, name: r.white_name }, white: { id: r.white_id || null, name: r.white_name },
black: { id: r.black_id, name: r.black_name } black: { id: r.black_id || null, name: r.black_name }
} as Game; } as Game;
}); });
} catch (err: unknown) { } catch (err: unknown) {