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

Resolve "Create a highscore endpoint"

parent 10695731
No related branches found
No related tags found
1 merge request!4Resolve "Create a highscore endpoint"
......@@ -19,3 +19,7 @@
> - 409 User already exists
> - 201 User created
### GET /highscores
> - 204 No highscores found
> - 200 List of users with highest score
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);
}
})
type User = {
export type User = {
id: string;
username: string;
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