Skip to content

Commit d2cac2c

Browse files
author
rinaldo stevenazzi
committed
add /score post endpoint
1 parent 6cf1619 commit d2cac2c

File tree

5 files changed

+105
-33
lines changed

5 files changed

+105
-33
lines changed

constants/scores.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export const SCORES = Object.freeze({
2+
ADD_USER: 'addUser',
3+
REMOVE_USER: 'removeUser'
4+
})

controllers/Global.js

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
const MongoDB = require("../Services/MongoDB")
1+
const MongoDB = require("../services/MongoDB")
22
const Users = require("../services/Users");
3+
const Scores = require("../services/Scores");
34

45
class Global {
56
constructor(){
67
console.log('Global Class Constructor')
78
this.mongoDB = new MongoDB()
89
this.users = new Users()
10+
this.scores = new Scores()
911
this.db = {}
1012

1113
this.initDB = this.initDB.bind(this)
@@ -17,6 +19,7 @@ class Global {
1719
.then(result => {
1820
this.db = result
1921
this.users.init(this.db)
22+
this.scores.init(this.db)
2023
resolve(null)
2124
})
2225
.catch(e => {
@@ -26,16 +29,29 @@ class Global {
2629
})
2730
}
2831

29-
getHigherScore () {
32+
addScore (score = 123) {
3033
return new Promise((resolve, reject) => {
31-
this.users.checkUserScore(123)
32-
.then((users) => {
33-
console.log('Global Controller - getHigherScore', users)
34-
resolve(users)
34+
this.scores.checkScores(score)
35+
.then(() => {
36+
console.log('Global Controller - addScore')
37+
resolve()
3538
}).catch(e => {
36-
console.log('Global Controller - getHigherScore failed !!!')
37-
reject(e)
38-
})
39+
console.log('Global Controller - addScore failed !!!')
40+
reject()
41+
})
42+
})
43+
}
44+
45+
checkScore (score = 123) {
46+
return new Promise((resolve, reject) => {
47+
this.scores.checkScores(score)
48+
.then(() => {
49+
console.log('Global Controller - checkScore')
50+
resolve()
51+
}).catch(result => {
52+
console.log('Global Controller - checkScore failed !!!')
53+
reject(result)
54+
})
3955
})
4056
}
4157
}

index.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,20 @@ globalController.initDB()
1313
.catch((e) => console.log('SERVER - initDB - ERROR', e))
1414

1515
app.use(express.json())
16-
app.get('/', async (req, res) => {
16+
app.get('/', (req, res) => {
1717
res.send('<h1>Hello world</h1>')
1818
});
1919

20-
app.get('/getHighersScore', async (req, res) => {
21-
globalController.getHigherScore().then(() => {
22-
res.send('<h1>this is the higher score</h1>')
20+
app.post('/score', (req, res) => {
21+
const { score } = req.body
22+
console.log('globalController - /score', score)
23+
globalController.checkScore(score).then(() => {
24+
console.log('index - response 200')
25+
res.send()
26+
}).catch((result) => {
27+
console.log('index - response 406')
28+
res.status(406).send(result)
2329
})
24-
2530
});
2631

2732

services/Scores.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
class Scores {
2+
constructor(){
3+
console.log('Scores Class Constructor')
4+
this.collection = null
5+
}
6+
7+
getCollectionPropertyValue(arr, property){
8+
return arr.map(a => a[property]);
9+
}
10+
11+
init (collection) {
12+
console.log('Scores Class - init')
13+
this.collection = collection.collection("Users")
14+
}
15+
16+
17+
// TODO : create a Capped Collections with a fixed size to 20
18+
// https://www.mongodb.com/docs/manual/core/capped-collections/#behavior
19+
async checkScores (score) {
20+
return new Promise(async (resolve, reject) => {
21+
22+
23+
//
24+
//
25+
// this.collection.findOne({'name' : 'b'})
26+
// .then((result) => {
27+
// console.log('findOne name b', result)
28+
// })
29+
30+
// const collection = this.collection.find({}).sort({score:-1}).toArray()
31+
let collection
32+
let scores
33+
try {
34+
collection = await this.collection.find().toArray()
35+
scores = this.getCollectionPropertyValue(collection, 'score')
36+
37+
console.log('collection')
38+
console.log('length', collection.length)
39+
console.log('min', Math.min(...scores))
40+
console.log('max', Math.max(...scores))
41+
42+
43+
44+
if(score > Math.min(...scores)) {
45+
console.log('score is higher', score)
46+
resolve()
47+
}
48+
reject(collection)
49+
} catch (error) {
50+
console.log('checkScores - ERROR', error)
51+
reject()
52+
}
53+
54+
55+
// this.collection.find({}).sort({score:-1}).toArray()
56+
// .then((result) => {
57+
// console.log('length', result.length)
58+
// console.log('min', Math.min(...this.getCollectionScore(result)))
59+
// console.log('max', Math.max(...this.getCollectionScore(result)))
60+
// })
61+
62+
})
63+
}
64+
}
65+
66+
module.exports = Scores

services/Users.js

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,25 +40,6 @@ class Users {
4040
})
4141
})
4242
}
43-
44-
45-
// TODO : create a Capped Collections with a fixed size to 20
46-
// https://www.mongodb.com/docs/manual/core/capped-collections/#behavior
47-
checkUserScore (score) {
48-
return new Promise((resolve, reject) => {
49-
this.collection.findOne({'name' : 'b'})
50-
.then((result) => {
51-
console.log('findOne name b', result)
52-
})
53-
this.collection.find({}).sort({score:-1}).toArray()
54-
.then((result) => {
55-
console.log('length', result.length)
56-
console.log('min', Math.min(...this.getCollectionScore(result)))
57-
console.log('max', Math.max(...this.getCollectionScore(result)))
58-
})
59-
60-
})
61-
}
6243
}
6344

6445
module.exports = Users

0 commit comments

Comments
 (0)