generated from nhcarrigan/template
feat: initial prototype works
I can log in and create a book! Woo!
This commit is contained in:
@@ -5,22 +5,16 @@
|
||||
*/
|
||||
|
||||
import { FastifyPluginAsync } from "fastify";
|
||||
import { PrismaClient } from "../../../generated/prisma";
|
||||
import { Game, GameStatus } from "@library/shared-types";
|
||||
import { Game, CreateGameDto, UpdateGameDto } from "@library/shared-types";
|
||||
import { GameService } from "../../services/game.service";
|
||||
import { adminGuard } from "../../middleware/admin-guard";
|
||||
|
||||
const gamesRoutes: FastifyPluginAsync = async (app) => {
|
||||
const prisma = new PrismaClient();
|
||||
const gameService = new GameService();
|
||||
|
||||
// Get all games (public route)
|
||||
app.get<{ Reply: Game[] }>("/", async () => {
|
||||
const games = await prisma.game.findMany({
|
||||
orderBy: { updatedAt: "desc" },
|
||||
});
|
||||
return games.map((game) => ({
|
||||
...game,
|
||||
status: game.status.toLowerCase() as GameStatus,
|
||||
}));
|
||||
return gameService.getAllGames();
|
||||
});
|
||||
|
||||
// Get single game (public route)
|
||||
@@ -28,64 +22,34 @@ const gamesRoutes: FastifyPluginAsync = async (app) => {
|
||||
"/:id",
|
||||
async (request) => {
|
||||
const { id } = request.params;
|
||||
const game = await prisma.game.findUnique({
|
||||
where: { id },
|
||||
});
|
||||
if (!game) return null;
|
||||
return {
|
||||
...game,
|
||||
status: game.status.toLowerCase() as GameStatus,
|
||||
};
|
||||
return gameService.getGameById(id);
|
||||
}
|
||||
);
|
||||
|
||||
// Create game (protected admin route)
|
||||
app.post<{ Body: Omit<Game, "id" | "createdAt" | "updatedAt">; Reply: Game }>(
|
||||
app.post<{ Body: CreateGameDto; Reply: Game }>(
|
||||
"/",
|
||||
{
|
||||
preValidation: [app.authenticate, adminGuard],
|
||||
},
|
||||
async (request, reply) => {
|
||||
|
||||
const game = await prisma.game.create({
|
||||
data: {
|
||||
...request.body,
|
||||
status: request.body.status.toUpperCase() as any,
|
||||
},
|
||||
});
|
||||
return {
|
||||
...game,
|
||||
status: game.status.toLowerCase() as GameStatus,
|
||||
};
|
||||
async (request) => {
|
||||
return gameService.createGame(request.body);
|
||||
}
|
||||
);
|
||||
|
||||
// Update game (protected admin route)
|
||||
app.put<{
|
||||
Params: { id: string };
|
||||
Body: Partial<Omit<Game, "id" | "createdAt" | "updatedAt">>;
|
||||
Body: UpdateGameDto;
|
||||
Reply: Game | null;
|
||||
}>(
|
||||
"/:id",
|
||||
{
|
||||
preValidation: [app.authenticate, adminGuard],
|
||||
},
|
||||
async (request, reply) => {
|
||||
|
||||
async (request) => {
|
||||
const { id } = request.params;
|
||||
const updateData = { ...request.body };
|
||||
if (updateData.status) {
|
||||
updateData.status = updateData.status.toUpperCase() as any;
|
||||
}
|
||||
|
||||
const game = await prisma.game.update({
|
||||
where: { id },
|
||||
data: updateData,
|
||||
});
|
||||
return {
|
||||
...game,
|
||||
status: game.status.toLowerCase() as GameStatus,
|
||||
};
|
||||
return gameService.updateGame(id, request.body);
|
||||
}
|
||||
);
|
||||
|
||||
@@ -95,12 +59,9 @@ const gamesRoutes: FastifyPluginAsync = async (app) => {
|
||||
{
|
||||
preValidation: [app.authenticate, adminGuard],
|
||||
},
|
||||
async (request, reply) => {
|
||||
|
||||
async (request) => {
|
||||
const { id } = request.params;
|
||||
await prisma.game.delete({
|
||||
where: { id },
|
||||
});
|
||||
await gameService.deleteGame(id);
|
||||
return { success: true };
|
||||
}
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user