full e2e behavior, nice
This commit is contained in:
@@ -1,6 +1,68 @@
|
||||
import { Hono } from "hono";
|
||||
import { prisma } from "./db/db";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { z } from "zod";
|
||||
|
||||
const api = new Hono().get("/ping", (c) => c.text("pong"));
|
||||
const api = new Hono()
|
||||
.get("/ping", (c) => c.text("pong"))
|
||||
|
||||
.get("/games", async (c) => {
|
||||
const games = await prisma.game.findMany();
|
||||
return c.json(games);
|
||||
})
|
||||
|
||||
.get(
|
||||
"/instances",
|
||||
zValidator("query", z.object({ game: z.string() })),
|
||||
async (c) => {
|
||||
const { game } = c.req.valid("query");
|
||||
const instances = await prisma.instance.findMany({
|
||||
where: {
|
||||
game: {
|
||||
name: game,
|
||||
},
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
},
|
||||
});
|
||||
return c.json(instances);
|
||||
}
|
||||
)
|
||||
|
||||
.get(
|
||||
"/gameState/:gameId",
|
||||
zValidator("param", z.object({ gameId: z.string() })),
|
||||
async (c) => {
|
||||
const { gameId } = c.req.valid("param");
|
||||
const instance = await prisma.instance.findUnique({
|
||||
where: {
|
||||
id: Number(gameId),
|
||||
},
|
||||
});
|
||||
return c.json(instance?.gameState);
|
||||
}
|
||||
)
|
||||
|
||||
.put(
|
||||
"/gameState/:gameId",
|
||||
zValidator("param", z.object({ gameId: z.string() }), (result) => {
|
||||
console.log(JSON.stringify(result, null, 2));
|
||||
}),
|
||||
zValidator("json", z.any()),
|
||||
async (c) => {
|
||||
const { gameId } = c.req.valid("param");
|
||||
const gameState = c.req.valid("json");
|
||||
|
||||
await prisma.instance.update({
|
||||
data: { gameState },
|
||||
where: {
|
||||
id: Number(gameId),
|
||||
},
|
||||
});
|
||||
|
||||
return c.text("", 200);
|
||||
}
|
||||
);
|
||||
export default api;
|
||||
export type ApiType = typeof api;
|
||||
|
||||
4
packages/server/src/db/Games.ts
Normal file
4
packages/server/src/db/Games.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
"use server";
|
||||
import { prisma } from "./db";
|
||||
|
||||
export const queryAll = async () => await prisma.game.findMany();
|
||||
25
packages/server/src/db/Instances.ts
Normal file
25
packages/server/src/db/Instances.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
"use server";
|
||||
|
||||
import { GameState, newDeck, shuffle } from "../types/cards";
|
||||
import { prisma } from "./db";
|
||||
|
||||
export const queryInstances = async (gameName: string) =>
|
||||
prisma.instance.findMany({ where: { game: { name: gameName } } });
|
||||
|
||||
export const createInstance = (gameName: string) =>
|
||||
prisma.instance.create({
|
||||
data: {
|
||||
gameState: { deck: shuffle(newDeck()), hand: [] } as GameState,
|
||||
game: { connect: { name: gameName } },
|
||||
},
|
||||
});
|
||||
|
||||
export const getGameState = (instanceId: number) =>
|
||||
prisma.instance
|
||||
.findUnique({ where: { id: instanceId } })
|
||||
.then((i) => i?.gameState as GameState | undefined);
|
||||
|
||||
export const updateGameState = async (
|
||||
instanceId: number,
|
||||
gameState: GameState
|
||||
) => prisma.instance.update({ where: { id: instanceId }, data: { gameState } });
|
||||
5
packages/server/src/db/db.ts
Normal file
5
packages/server/src/db/db.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
"use server";
|
||||
|
||||
import { PrismaClient } from "@prisma/client";
|
||||
|
||||
export const prisma = new PrismaClient();
|
||||
Reference in New Issue
Block a user