From 1ceaa7a5591fe0fc5fac9b94466f4f40e4fb0d4c Mon Sep 17 00:00:00 2001 From: Snorre Skjellestad Kristiansen <snorrekr@stud.ntnu.no> Date: Wed, 8 Mar 2023 17:40:10 +0100 Subject: [PATCH] Resolve "Create a highscore endpoint" --- backend/documentation.md | 4 ++++ backend/src/index.ts | 16 ++++++++++++++++ backend/types/User.ts | 2 +- 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/backend/documentation.md b/backend/documentation.md index ddeb43c..88a5ece 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 5ce25f3..fda0910 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 a75e654..7e20761 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; -- GitLab