From d0d4f579f53e4a9e19b825dd5a1bf84c7d9f89e4 Mon Sep 17 00:00:00 2001 From: Pinapelz Date: Fri, 29 Aug 2025 23:15:02 -0700 Subject: add backend admin api route to create new game --- backend/src/index.ts | 3 +++ backend/src/routes/admin.ts | 50 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 backend/src/routes/admin.ts diff --git a/backend/src/index.ts b/backend/src/index.ts index f51eaa2..01fc8d9 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -10,6 +10,7 @@ import * as authRoutes from './routes/auth'; import * as userRoutes from './routes/user'; import * as gameRoutes from './routes/game'; import * as scoreRoutes from './routes/score'; +import * as adminRoutes from './routes/admin'; const app = express(); const port = 5000; @@ -58,6 +59,8 @@ app.get('/api/scores', requireAuth, scoreRoutes.handleGetScores); app.delete('/api/scores', requireAuth, scoreRoutes.handleScoreDeletion); app.get('/api/scores/:chartId', requireAuth, scoreRoutes.handleGetScoresByChartId); +app.post('/api/admin/createGame', requireAuth, adminRoutes.handleCreateGame); + app.listen(port, () => { console.log(`Server listening on port ${port}`); }); diff --git a/backend/src/routes/admin.ts b/backend/src/routes/admin.ts new file mode 100644 index 0000000..1950715 --- /dev/null +++ b/backend/src/routes/admin.ts @@ -0,0 +1,50 @@ +import { prisma } from '../config/db'; +import express from 'express'; + +export const handleCreateGame = async (req: express.Request, res: express.Response) => { + try { + if (!req.session.userId) { + return res.status(401).json({ error: 'Authentication required' }); + } + + if (req.session.userId !== 1){ + return res.status(403).json({ error: 'Unauthorized. You are not the admin of this instance' }); + } + + const user = await prisma.user.findUnique({ + where: { id: req.session.userId }, + select: { id: true, username: true, email: true } + }); + + if (!user) { + req.session.destroy((err) => { + if (err) console.error('Session destroy error:', err); + }); + return res.status(401).json({ error: 'Invalid session' }); + } + const { gameInternalName, gameFormattedName, gameDescription } = req.body; + + if (!gameInternalName || !gameFormattedName || !gameDescription) { + return res.status(400).json({ error: 'All fields are required' }); + } + + const success = await prisma.game.create({ + data: { + internalName: gameInternalName, + formattedName: gameFormattedName, + description: gameDescription, + } + }); + + if (!success) { + console.log('Failed to create game:', success); + return res.status(500).json({ error: 'Failed to create game. Does it already exist?' }); + } + return res.status(200).json({ message: 'Game created successfully' }); + + + } catch (error) { + console.error('Game Creation error:', error); + res.status(500).json({ error: 'Internal server error' }); + } +} -- cgit v1.2.3