diff --git a/backend/documentation.md b/backend/documentation.md index ddeb43c57e97dee30d47fbb904300e77f1f9c86c..88a5ece56cb0529c2b5ffe095158fac16d2d52c9 100644 --- a/backend/documentation.md +++ b/backend/documentation.md @@ -19,3 +19,7 @@ > - 409 User already exists > - 201 User created + +### GET /highscores +> - 204 No highscores found +> - 200 List of users with highest score diff --git a/backend/src/index.ts b/backend/src/index.ts index 5ce25f3f3b2245bc1f905b46957246b9f0e98b1d..fda09101fced91f35ff5486a9cf5eee1db6a3715 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -1,5 +1,6 @@ import express from 'express'; import path from 'path'; +import { User } from '../types/User'; const app = express(); const port = 3000; @@ -85,3 +86,18 @@ app.post('/user/create/:username', async (req, res) => { res.status(201).send(newUserRef.id); } }); + + +// returns users with top 10 highscore + +app.get('/highscores', async (req, res) => { + const usersRef = admin.firestore().collection('users'); + const querySnapshot = await usersRef.orderBy('highscore', 'desc').limit(10).get(); + + if (querySnapshot.empty) { + res.status(204).send('No highscores found'); + } else { + const users = querySnapshot.docs.map((doc: any) => doc.data() as User); + res.status(200).send(users); + } +}) diff --git a/backend/types/User.ts b/backend/types/User.ts index a75e654b824c08eac220eb365f59296a7b9d80c3..7e207611346141494921b93f80a1808f0bca4bf9 100644 --- a/backend/types/User.ts +++ b/backend/types/User.ts @@ -1,4 +1,4 @@ -type User = { +export type User = { id: string; username: string; wins: number;