Skip to content

Commit d11653c

Browse files
author
rinaldo stevenazzi
committed
add logic to add, remove, user, checking score, etc.
1 parent ccd41a5 commit d11653c

File tree

8 files changed

+285
-40
lines changed

8 files changed

+285
-40
lines changed

controllers/Global.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const EVENTS = require("../constants/events");
66
class Global {
77
constructor(){
88
console.log('Global Class Constructor')
9+
910
this.mongoDB = new MongoDB()
1011
this.users = new Users()
1112
this.scores = new Scores()
@@ -16,7 +17,7 @@ class Global {
1617

1718
initDB () {
1819
return new Promise((resolve, reject) => {
19-
this.mongoDB.getDB()
20+
this.mongoDB.connect()
2021
.then(result => {
2122
this.db = result
2223
this.users.init(this.db)
@@ -30,15 +31,16 @@ class Global {
3031
})
3132
}
3233

33-
addUser (score, email, password) {
34+
addUser (user) {
35+
const { email } = user
3436
return new Promise(async (resolve, reject) => {
3537
const oldUser = await this.users.isUserAlreadyExist(email)
3638
if (oldUser) {
3739
resolve({message: EVENTS.USER_ALREADY_EXIST})
3840
}
3941

40-
this.users.addUser({score, email, password})
41-
.then((result) => {
42+
this.users.addUser(user)
43+
.then(() => {
4244
resolve({message: EVENTS.USER_CREATED})
4345
}).catch(e => {
4446
console.log('Global Controller - addUser failed !!!')

index.js

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,35 +19,35 @@ app.get('/', (req, res) => {
1919
});
2020

2121
app.post('/score', (req, res) => {
22-
const { score } = req.body
23-
console.log('globalController - /score', score)
24-
globalController.checkScore(score).then(() => {
25-
console.log('index - response 200')
26-
res.send()
27-
}).catch((result) => {
28-
console.log('index - response 406')
29-
res.status(406).send(result)
30-
})
31-
});
22+
const {score} = req.body
3223

33-
app.post('/adduser', (req, res) => {
34-
const { score, email, password } = req.body
35-
// console.log('globalController - /adduser score', score)
36-
// console.log('globalController - /adduser email', email)
37-
// console.log('globalController - /adduser password', password)
38-
globalController.addUser(score, email, password).then((result) => {
39-
console.log('index - adduser result', result)
40-
if (result.message === EVENTS.USER_ALREADY_EXIST) {
41-
res.status(409).send("User Already Exist.")
42-
}
24+
globalController.checkScore(score).then(() => {
4325
res.send()
4426
}).catch((result) => {
45-
console.log('index - response 406')
4627
res.status(406).send(result)
4728
})
29+
30+
});
31+
32+
app.post('/adduser', (req, res) => {
33+
const {username, score, email, password} = req.body
34+
35+
globalController.addUser({username, score, email, password})
36+
.then((result) => {
37+
console.log('index - adduser result', result)
38+
if (result.message === EVENTS.USER_ALREADY_EXIST) {
39+
res.status(409).send("User Already Exist.")
40+
}
41+
res.send()
42+
})
43+
.catch((result) => {
44+
console.log('index - response 406')
45+
res.status(406).send(result)
46+
})
47+
4848
});
4949

5050

5151
server.listen(PORT, () => {
52-
console.log(`Listening on ${ PORT }`)
52+
console.log(`Listening on ${PORT}`)
5353
})

package-lock.json

Lines changed: 209 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
"test": "echo \"Error: no test specified\" && exit 1"
99
},
1010
"dependencies": {
11+
"bcryptjs": "^2.4.3",
1112
"dotenv": "^16.0.0",
1213
"express": "^4.17.1",
14+
"jsonwebtoken": "^8.5.1",
1315
"mongodb": "^4.1.0",
1416
"socket.io": "^4.1.2"
1517
},

0 commit comments

Comments
 (0)