From 993a842c80a090349bec6fd51e2248bcdd4ba611 Mon Sep 17 00:00:00 2001
From: fredrfha <fredrfha@stud.ntnu.no>
Date: Mon, 6 Mar 2023 19:48:19 +0100
Subject: [PATCH] Add prettier

---
 backend/.prettierignore  |  7 +++
 backend/.prettierrc.json |  7 +++
 backend/README.md        |  9 +++-
 backend/package.json     |  6 ++-
 backend/src/index.ts     | 93 ++++++++++++++++++++--------------------
 backend/tsconfig.json    | 25 +++++------
 backend/types/User.ts    | 12 +++---
 backend/yarn.lock        |  5 +++
 8 files changed, 93 insertions(+), 71 deletions(-)
 create mode 100644 backend/.prettierignore
 create mode 100644 backend/.prettierrc.json

diff --git a/backend/.prettierignore b/backend/.prettierignore
new file mode 100644
index 0000000..86a0c7a
--- /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 0000000..17387b4
--- /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 caac898..875e43c 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 908f949..44e4437 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 6c4d5ad..5ce25f3 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 f1d424a..35719ad 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 0b1b40b..a75e654 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 aef880e..51abf6b 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"
-- 
GitLab