diff options
Diffstat (limited to 'backend')
| -rw-r--r-- | backend/schema.prisma | 2 | ||||
| -rw-r--r-- | backend/src/index.ts | 9 | ||||
| -rw-r--r-- | backend/src/routes/auth.ts (renamed from backend/src/routes/authRoutes.ts) | 0 | ||||
| -rw-r--r-- | backend/src/routes/game.ts (renamed from backend/src/routes/gameRoutes.ts) | 0 | ||||
| -rw-r--r-- | backend/src/routes/score.ts | 50 | ||||
| -rw-r--r-- | backend/src/routes/user.ts (renamed from backend/src/routes/userRoutes.ts) | 0 |
6 files changed, 57 insertions, 4 deletions
diff --git a/backend/schema.prisma b/backend/schema.prisma index 3b91f85..0657fdf 100644 --- a/backend/schema.prisma +++ b/backend/schema.prisma @@ -27,7 +27,7 @@ model Session { model Game { internalName String @id - formattedName String + formattedName String @unique description String scores Score[] } diff --git a/backend/src/index.ts b/backend/src/index.ts index c776ce1..4a63c41 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -6,9 +6,10 @@ import { requireAuth } from './middleware/requireAuth'; import { startSessionCleanup } from './utils/session'; // Routes -import * as authRoutes from './routes/authRoutes'; -import * as userRoutes from './routes/userRoutes'; -import * as gameRoutes from './routes/gameRoutes'; +import * as authRoutes from './routes/auth'; +import * as userRoutes from './routes/user'; +import * as gameRoutes from './routes/game'; +import * as scoreRoutes from './routes/score'; const app = express(); const port = 5000; @@ -52,6 +53,8 @@ app.get('/api/session', userRoutes.handleGetCurrentSession); app.get('/api/supportedGames', gameRoutes.handleGetSupportedGames); +app.post('/api/uploadScore', requireAuth, scoreRoutes.handleScoreUpload); + app.listen(port, () => { console.log(`Server listening on port ${port}`); }); diff --git a/backend/src/routes/authRoutes.ts b/backend/src/routes/auth.ts index 4c6c374..4c6c374 100644 --- a/backend/src/routes/authRoutes.ts +++ b/backend/src/routes/auth.ts diff --git a/backend/src/routes/gameRoutes.ts b/backend/src/routes/game.ts index 26e7171..26e7171 100644 --- a/backend/src/routes/gameRoutes.ts +++ b/backend/src/routes/game.ts diff --git a/backend/src/routes/score.ts b/backend/src/routes/score.ts new file mode 100644 index 0000000..03026af --- /dev/null +++ b/backend/src/routes/score.ts @@ -0,0 +1,50 @@ +import express from 'express'; +import { prisma } from '../config/db'; + +export const handleScoreUpload = async (req: express.Request, res: express.Response) => { + try { + const { meta, scores } = req.body; + const userId = req.session.userId; + if (!userId) { + return res.status(401).json({ error: 'Unauthorized. Please log in to upload scores.' }); + } + + // Basic universal validation + if (!meta || !meta.game || !meta.service || !scores) { + return res.status(400).json({ error: 'Invalid request format. Expected meta with game/service and scores array' }); + } + let game = await prisma.game.findUnique({ + where: { internalName: meta.game } + }); + if (!game) { + game = await prisma.game.findFirst({ + where: { formattedName: meta.game } + }); + } + if (!game) { + return res.status(400).json({ error: `Game '${meta.game}' is not supported. Ensure that you are using the case-sensitive version of either the internal name or formatted name` }); + } + const internalGameName = game.internalName; + const scoresArray = Array.isArray(scores) ? scores : [scores]; + + // Create score records + const createdScores = await prisma.score.createMany({ + data: scoresArray.map(scoreData => ({ + gameInternalName: internalGameName, + userId: userId, + data: scoreData + })) + }); + + res.status(200).json({ + message: 'Score upload received successfully', + game: meta.game, + service: meta.service, + scoreCount: createdScores.count + }); + + } catch (error) { + console.error('Score upload endpoint error:', error); + res.status(500).json({ error: 'Internal server error. Unable to process score upload' }); + } +} diff --git a/backend/src/routes/userRoutes.ts b/backend/src/routes/user.ts index a03ece0..a03ece0 100644 --- a/backend/src/routes/userRoutes.ts +++ b/backend/src/routes/user.ts |
