Skip to content
Snippets Groups Projects
Commit 7dd78213 authored by Snorre Skjellestad Kristiansen's avatar Snorre Skjellestad Kristiansen
Browse files

Merge branch '4-create-a-highscore-endpoint' into 'main'

Resolve "Create a highscore endpoint"

Closes #4

See merge request !4
parents 10695731 1ceaa7a5
No related branches found
No related tags found
1 merge request!4Resolve "Create a highscore endpoint"
Pipeline #203711 passed
...@@ -19,3 +19,7 @@ ...@@ -19,3 +19,7 @@
> - 409 User already exists > - 409 User already exists
> - 201 User created > - 201 User created
### GET /highscores
> - 204 No highscores found
> - 200 List of users with highest score
import express from 'express'; import express from 'express';
import path from 'path'; import path from 'path';
import { User } from '../types/User';
const app = express(); const app = express();
const port = 3000; const port = 3000;
...@@ -85,3 +86,18 @@ app.post('/user/create/:username', async (req, res) => { ...@@ -85,3 +86,18 @@ app.post('/user/create/:username', async (req, res) => {
res.status(201).send(newUserRef.id); 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);
}
})
type User = { export type User = {
id: string; id: string;
username: string; username: string;
wins: number; wins: number;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment