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
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`
......@@ -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"
}
}
......@@ -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);
}
});
{
"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/**/*"]
}
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;
};
......@@ -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"
......
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