aboutsummaryrefslogtreecommitdiffstats
path: root/backend
diff options
context:
space:
mode:
authorPinapelz <yukais@pinapelz.com>2025-08-29 23:15:02 -0700
committerPinapelz <yukais@pinapelz.com>2025-08-29 23:15:02 -0700
commitd0d4f579f53e4a9e19b825dd5a1bf84c7d9f89e4 (patch)
tree3491ba5565a3692aa9b90f2caf0661bf46f50656 /backend
parent2383a8ba0ad99937396230333f10c59050ddf5c5 (diff)
add backend admin api route to create new game
Diffstat (limited to 'backend')
-rw-r--r--backend/src/index.ts3
-rw-r--r--backend/src/routes/admin.ts50
2 files changed, 53 insertions, 0 deletions
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' });
+ }
+}
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage