Skip to content

Commit 6cf1619

Browse files
author
rinaldo stevenazzi
committed
WIP - connected mongodb, and start CRUD operation testing
1 parent e67ca10 commit 6cf1619

File tree

4 files changed

+44
-5
lines changed

4 files changed

+44
-5
lines changed

controllers/Global.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ class Global {
1616
this.mongoDB.getDB()
1717
.then(result => {
1818
this.db = result
19-
console.log('Global Controller - initDB initialisation', this.db)
2019
this.users.init(this.db)
2120
resolve(null)
2221
})
@@ -26,6 +25,19 @@ class Global {
2625
})
2726
})
2827
}
28+
29+
getHigherScore () {
30+
return new Promise((resolve, reject) => {
31+
this.users.checkUserScore(123)
32+
.then((users) => {
33+
console.log('Global Controller - getHigherScore', users)
34+
resolve(users)
35+
}).catch(e => {
36+
console.log('Global Controller - getHigherScore failed !!!')
37+
reject(e)
38+
})
39+
})
40+
}
2941
}
3042

3143
module.exports = Global

index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ const app = express()
77
const server = http.createServer(app)
88
const PORT = process.env.API_PORT
99

10-
console.log('process env', process.env)
11-
1210
const globalController = new Global()
1311
globalController.initDB()
1412
.then(() => console.log('SERVER - initDB - DONE'))
@@ -19,6 +17,13 @@ app.get('/', async (req, res) => {
1917
res.send('<h1>Hello world</h1>')
2018
});
2119

20+
app.get('/getHighersScore', async (req, res) => {
21+
globalController.getHigherScore().then(() => {
22+
res.send('<h1>this is the higher score</h1>')
23+
})
24+
25+
});
26+
2227

2328
server.listen(PORT, () => {
2429
console.log(`Listening on ${ PORT }`)

services/MongoDB.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ class MongoDB {
1111
return new Promise((resolve, reject) => {
1212
client.connect()
1313
.then(() => {
14-
console.log('MongoDB Class - getDB')
1514
resolve(client.db("speed-puzzle-db"))
1615
})
1716
.catch(e => {

services/Users.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@ class Users {
44
this.collection = null
55
}
66

7+
getCollectionScore(arr){
8+
return arr.map(a => a.score);
9+
}
10+
711
init (collection) {
12+
console.log('Users Class - init')
813
this.collection = collection.collection("users")
9-
console.log('Users Class - init', this.collection)
1014
}
1115

1216
addUser (user) {
@@ -36,6 +40,25 @@ class Users {
3640
})
3741
})
3842
}
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+
}
3962
}
4063

4164
module.exports = Users

0 commit comments

Comments
 (0)