Skip to content
Snippets Groups Projects
Verified Commit 993a842c authored by Fredrik Fonn Hansen's avatar Fredrik Fonn Hansen :8ball:
Browse files

Add prettier

parent cdf6e6d3
No related branches found
No related tags found
1 merge request!3Resolve "Create a users endpoint"
# Ignore artifacts:
build
coverage
fontawesome
dist
keys
node_modules
{
"trailingComma": "es5",
"tabWidth": 2,
"semi": true,
"singleQuote": true,
"printWidth": 90
}
...@@ -18,5 +18,10 @@ The key is login details to a Firebase service account, needed to access the dat ...@@ -18,5 +18,10 @@ The key is login details to a Firebase service account, needed to access the dat
Run the following commands in your terminal: Run the following commands in your terminal:
- yarn - `yarn` last ned avhengigheter
- yarn start - `yarn start` start serveren
## Nyttige kommandoer
Formatere koden: `yarn prettier`
Kjøre tester: `yarn test`
...@@ -5,7 +5,8 @@ ...@@ -5,7 +5,8 @@
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1", "test": "echo \"Error: no test specified\" && exit 1",
"start": "tsc && node dist/src/index.js" "start": "tsc && node dist/src/index.js",
"prettier": "prettier --write \"**/*.{js,ts,tsx,jsx,css,md,scss,html,json}\""
}, },
"author": "", "author": "",
"license": "ISC", "license": "ISC",
...@@ -15,6 +16,7 @@ ...@@ -15,6 +16,7 @@
"typescript": "^4.9.5" "typescript": "^4.9.5"
}, },
"dependencies": { "dependencies": {
"firebase-admin": "^11.5.0" "firebase-admin": "^11.5.0",
"prettier": "^2.8.4"
} }
} }
...@@ -27,60 +27,61 @@ const admin = require('firebase-admin'); ...@@ -27,60 +27,61 @@ const admin = require('firebase-admin');
const serviceAccount = path.join(__dirname, '../..', 'keys', 'fb-key.json'); const serviceAccount = path.join(__dirname, '../..', 'keys', 'fb-key.json');
admin.initializeApp({ admin.initializeApp({
credential: admin.credential.cert(serviceAccount), credential: admin.credential.cert(serviceAccount),
}); });
// returns all the user-id's // returns all the user-id's
app.get('/user', async (req, res) => { app.get('/user', async (req, res) => {
const usersRef = admin.firestore().collection('users'); const usersRef = admin.firestore().collection('users');
const querySnapshot = await usersRef.get(); const querySnapshot = await usersRef.get();
if (querySnapshot.empty) { if (querySnapshot.empty) {
res.status(204).send('No users found'); res.status(204).send('No users found');
} else { } else {
const userids = querySnapshot.docs.map((doc: { id: any; }) => doc.id); const userids = querySnapshot.docs.map((doc: { id: any }) => doc.id);
res.status(200).send(userids); res.status(200).send(userids);
} }
}); });
// returns data of a specific user // returns data of a specific user
app.get('/user/:idOrUsername', async (req, res) => { app.get('/user/:idOrUsername', async (req, res) => {
const usersRef = admin.firestore().collection('users'); const usersRef = admin.firestore().collection('users');
const snapshot = await usersRef.get(); const snapshot = await usersRef.get();
const searchParam = req.params.idOrUsername; const searchParam = req.params.idOrUsername;
const user = snapshot.docs.find((doc: { id: string; }) => doc.id === searchParam) const user =
|| (await Promise.all(snapshot.docs.map(async (doc: { id: string; }) => { snapshot.docs.find((doc: { id: string }) => doc.id === searchParam) ||
const userSnapshot = await usersRef.doc(doc.id).get(); (
return userSnapshot.data().username === searchParam ? userSnapshot : null; await Promise.all(
}))).find(Boolean); snapshot.docs.map(async (doc: { id: string }) => {
const userSnapshot = await usersRef.doc(doc.id).get();
if (user) { return userSnapshot.data().username === searchParam ? userSnapshot : null;
res.status(200).send(user.data()); })
} else { )
res.status(404).send('User not found'); ).find(Boolean);
}
}); if (user) {
res.status(200).send(user.data());
} else {
res.status(404).send('User not found');
}
});
// create new user // create new user
app.post('/user/create/:username', async (req, res) => { app.post('/user/create/:username', async (req, res) => {
const usersRef = admin.firestore().collection('users'); const usersRef = admin.firestore().collection('users');
const querySnapshot = await usersRef.where('username', '==', req.params.username).get(); const querySnapshot = await usersRef.where('username', '==', req.params.username).get();
if (!querySnapshot.empty) { if (!querySnapshot.empty) {
res.status(409).send('User already exists'); res.status(409).send('User already exists');
} else { } else {
const newUserRef = await usersRef.add({ const newUserRef = await usersRef.add({
username: req.params.username, username: req.params.username,
highscore: 0, highscore: 0,
games: 0, games: 0,
wins: 0, wins: 0,
losses: 0, losses: 0,
}); });
res.status(201).send(newUserRef.id); res.status(201).send(newUserRef.id);
} }
}); });
\ No newline at end of file
{ {
"compilerOptions": { "compilerOptions": {
"target": "es5", "target": "es5",
"module": "commonjs", "module": "commonjs",
"esModuleInterop": true, "esModuleInterop": true,
"outDir": "dist", "outDir": "dist",
"strict": true, "strict": true,
"sourceMap": true "sourceMap": true
}, },
"include": [ "include": ["src/**/*", "types/**/*", "keys/**/*"]
"src/**/*", }
"types/**/*",
"keys/**/*"
]
}
\ No newline at end of file
type User = { type User = {
id: string; id: string;
username: string; username: string;
wins: number; wins: number;
losses: number; losses: number;
highscore: number; highscore: number;
}; };
\ No newline at end of file
...@@ -1356,6 +1356,11 @@ prelude-ls@~1.1.2: ...@@ -1356,6 +1356,11 @@ prelude-ls@~1.1.2:
resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.1.2.tgz#21932a549f5e52ffd9a827f570e04be62a97da54" resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.1.2.tgz#21932a549f5e52ffd9a827f570e04be62a97da54"
integrity sha512-ESF23V4SKG6lVSGZgYNpbsiaAkdab6ZgOxe52p7+Kid3W3u3bxR4Vfd/o21dmN7jSt0IwgZ4v5MUd26FEtXE9w== integrity sha512-ESF23V4SKG6lVSGZgYNpbsiaAkdab6ZgOxe52p7+Kid3W3u3bxR4Vfd/o21dmN7jSt0IwgZ4v5MUd26FEtXE9w==
prettier@^2.8.4:
version "2.8.4"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.8.4.tgz#34dd2595629bfbb79d344ac4a91ff948694463c3"
integrity sha512-vIS4Rlc2FNh0BySk3Wkd6xmwxB0FpOndW5fisM5H8hsZSxU2VWVB5CWIkIjWvrHjIhxk2g3bfMKM87zNTrZddw==
proto3-json-serializer@^1.0.0: proto3-json-serializer@^1.0.0:
version "1.1.0" version "1.1.0"
resolved "https://registry.yarnpkg.com/proto3-json-serializer/-/proto3-json-serializer-1.1.0.tgz#52d9c73b24d25ff925639e1e5a01ac883460149f" resolved "https://registry.yarnpkg.com/proto3-json-serializer/-/proto3-json-serializer-1.1.0.tgz#52d9c73b24d25ff925639e1e5a01ac883460149f"
......
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