57 lines
1.0 KiB
TypeScript
57 lines
1.0 KiB
TypeScript
import { prisma } from "./db/db";
|
|
import { Elysia, t } from "elysia";
|
|
import { Prisma } from "@prisma/client";
|
|
import { simpleApi } from "./games/simple";
|
|
import { human } from "./human";
|
|
|
|
const api = new Elysia({ prefix: "/api" })
|
|
.post("/whoami", async ({ cookie: { token } }) => {
|
|
if (token.value == null) {
|
|
const newHuman = await prisma.human.create({
|
|
data: {},
|
|
});
|
|
token.value = newHuman.key;
|
|
}
|
|
return token.value;
|
|
})
|
|
.use(human)
|
|
.post(
|
|
"/setName",
|
|
({ body: { name }, humanKey }) =>
|
|
prisma.human.update({
|
|
where: {
|
|
key: humanKey,
|
|
},
|
|
data: {
|
|
name,
|
|
},
|
|
}),
|
|
{
|
|
body: t.Object({
|
|
name: t.String(),
|
|
}),
|
|
}
|
|
)
|
|
.get("/profile", ({ humanKey }) =>
|
|
prisma.human.findFirst({ where: { key: humanKey } })
|
|
)
|
|
.get("/games", () => prisma.game.findMany())
|
|
|
|
.get("/instances", ({ query: { game } }) =>
|
|
prisma.instance.findMany({
|
|
where: {
|
|
game: {
|
|
name: game,
|
|
},
|
|
},
|
|
select: {
|
|
id: true,
|
|
},
|
|
})
|
|
)
|
|
|
|
.use(simpleApi);
|
|
|
|
export default api;
|
|
export type Api = typeof api;
|