diff --git a/backend/.prettierignore b/backend/.prettierignore new file mode 100644 index 0000000000000000000000000000000000000000..86a0c7a5266eaece7b7a52a0d3aa3775cf87402b --- /dev/null +++ b/backend/.prettierignore @@ -0,0 +1,7 @@ +# Ignore artifacts: +build +coverage +fontawesome +dist +keys +node_modules diff --git a/backend/.prettierrc.json b/backend/.prettierrc.json new file mode 100644 index 0000000000000000000000000000000000000000..17387b472693be127885468cf440879117ab7dfe --- /dev/null +++ b/backend/.prettierrc.json @@ -0,0 +1,7 @@ +{ + "trailingComma": "es5", + "tabWidth": 2, + "semi": true, + "singleQuote": true, + "printWidth": 90 +} diff --git a/backend/README.md b/backend/README.md index caac89853070c4a38772c5846a82f415a52f049d..875e43cc639073dd67ca4ad448e91c9545f1b61e 100644 --- a/backend/README.md +++ b/backend/README.md @@ -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: -- yarn -- yarn start +- `yarn` last ned avhengigheter +- `yarn start` start serveren + +## Nyttige kommandoer + +Formatere koden: `yarn prettier` +Kjøre tester: `yarn test` diff --git a/backend/package.json b/backend/package.json index 908f949db88e21b9bc5e10d1174fab2b01f18ae6..44e44374c5cfa5de2bfa98159216e18438afad88 100644 --- a/backend/package.json +++ b/backend/package.json @@ -5,7 +5,8 @@ "main": "index.js", "scripts": { "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": "", "license": "ISC", @@ -15,6 +16,7 @@ "typescript": "^4.9.5" }, "dependencies": { - "firebase-admin": "^11.5.0" + "firebase-admin": "^11.5.0", + "prettier": "^2.8.4" } } diff --git a/backend/src/index.ts b/backend/src/index.ts index 6c4d5ad6ace18e081528c1d691ef9529893e8621..5ce25f3f3b2245bc1f905b46957246b9f0e98b1d 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -27,60 +27,61 @@ const admin = require('firebase-admin'); const serviceAccount = path.join(__dirname, '../..', 'keys', 'fb-key.json'); admin.initializeApp({ - credential: admin.credential.cert(serviceAccount), + credential: admin.credential.cert(serviceAccount), }); - // returns all the user-id's app.get('/user', async (req, res) => { - const usersRef = admin.firestore().collection('users'); - const querySnapshot = await usersRef.get(); - - if (querySnapshot.empty) { - res.status(204).send('No users found'); - } else { - const userids = querySnapshot.docs.map((doc: { id: any; }) => doc.id); - res.status(200).send(userids); - } + const usersRef = admin.firestore().collection('users'); + const querySnapshot = await usersRef.get(); + + if (querySnapshot.empty) { + res.status(204).send('No users found'); + } else { + const userids = querySnapshot.docs.map((doc: { id: any }) => doc.id); + res.status(200).send(userids); + } }); - // returns data of a specific user app.get('/user/:idOrUsername', async (req, res) => { - const usersRef = admin.firestore().collection('users'); - const snapshot = await usersRef.get(); - const searchParam = req.params.idOrUsername; - - const user = snapshot.docs.find((doc: { id: string; }) => doc.id === searchParam) - || (await Promise.all(snapshot.docs.map(async (doc: { id: string; }) => { - const userSnapshot = await usersRef.doc(doc.id).get(); - return userSnapshot.data().username === searchParam ? userSnapshot : null; - }))).find(Boolean); - - if (user) { - res.status(200).send(user.data()); - } else { - res.status(404).send('User not found'); - } - }); - + const usersRef = admin.firestore().collection('users'); + const snapshot = await usersRef.get(); + const searchParam = req.params.idOrUsername; + + const user = + snapshot.docs.find((doc: { id: string }) => doc.id === searchParam) || + ( + await Promise.all( + snapshot.docs.map(async (doc: { id: string }) => { + const userSnapshot = await usersRef.doc(doc.id).get(); + return userSnapshot.data().username === searchParam ? userSnapshot : null; + }) + ) + ).find(Boolean); + + if (user) { + res.status(200).send(user.data()); + } else { + res.status(404).send('User not found'); + } +}); // create new user app.post('/user/create/:username', async (req, res) => { - const usersRef = admin.firestore().collection('users'); - const querySnapshot = await usersRef.where('username', '==', req.params.username).get(); - - if (!querySnapshot.empty) { - res.status(409).send('User already exists'); - } else { - const newUserRef = await usersRef.add({ - username: req.params.username, - highscore: 0, - games: 0, - wins: 0, - losses: 0, - }); - res.status(201).send(newUserRef.id); - } - }); - \ No newline at end of file + const usersRef = admin.firestore().collection('users'); + const querySnapshot = await usersRef.where('username', '==', req.params.username).get(); + + if (!querySnapshot.empty) { + res.status(409).send('User already exists'); + } else { + const newUserRef = await usersRef.add({ + username: req.params.username, + highscore: 0, + games: 0, + wins: 0, + losses: 0, + }); + res.status(201).send(newUserRef.id); + } +}); diff --git a/backend/tsconfig.json b/backend/tsconfig.json index f1d424a1da2aa1d8f3ff9930a45bba9d9c3fc610..35719adfd90c5ab3ae8540b3f177e144d4f34348 100644 --- a/backend/tsconfig.json +++ b/backend/tsconfig.json @@ -1,16 +1,11 @@ { - "compilerOptions": { - "target": "es5", - "module": "commonjs", - "esModuleInterop": true, - "outDir": "dist", - "strict": true, - "sourceMap": true - }, - "include": [ - "src/**/*", - "types/**/*", - "keys/**/*" - ] - } - \ No newline at end of file + "compilerOptions": { + "target": "es5", + "module": "commonjs", + "esModuleInterop": true, + "outDir": "dist", + "strict": true, + "sourceMap": true + }, + "include": ["src/**/*", "types/**/*", "keys/**/*"] +} diff --git a/backend/types/User.ts b/backend/types/User.ts index 0b1b40be45afdee8c3e91edd7f0dcdb04f52dfc0..a75e654b824c08eac220eb365f59296a7b9d80c3 100644 --- a/backend/types/User.ts +++ b/backend/types/User.ts @@ -1,7 +1,7 @@ type User = { - id: string; - username: string; - wins: number; - losses: number; - highscore: number; -}; \ No newline at end of file + id: string; + username: string; + wins: number; + losses: number; + highscore: number; +}; diff --git a/backend/yarn.lock b/backend/yarn.lock index aef880e3bb4a19f3e43b9b56998a76144eea6968..51abf6b00c5542500f214f43a81c729ec91bfed9 100644 --- a/backend/yarn.lock +++ b/backend/yarn.lock @@ -1356,6 +1356,11 @@ prelude-ls@~1.1.2: resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.1.2.tgz#21932a549f5e52ffd9a827f570e04be62a97da54" 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: version "1.1.0" resolved "https://registry.yarnpkg.com/proto3-json-serializer/-/proto3-json-serializer-1.1.0.tgz#52d9c73b24d25ff925639e1e5a01ac883460149f"