-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
83 lines (57 loc) · 2.27 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const {checkAndchange} = require('./assets/classes/functions')
const mysql = require('promise-mysql')
const bodyParser = require('body-parser')
const express = require('express')
const swaggerUi = require('swagger-ui-express')
const swaggerDocument = require('./assets/swagger.json')
const morgan = require('morgan')
const config = require('./assets/classes/config')
mysql.createConnection({
host: config.db.host,
database: config.db.database,
user: config.db.user,
password: config.db.password
}).then((db)=> {
console.log('Connected.')
const app = express()
let MembersRouter = express.Router()
let Members = require('./assets/classes/members-class')(db, config)
console.log(Members)
app.use(morgan('dev'))
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }))
// SWAGGERS http://localhost:8080/api/v1/api-docs/#/
app.use(config.rootAPI + 'api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
MembersRouter.route('/:id')
// Récupère un membre avec son ID
.get(async (req, res) => {
let member = await Members.getById(req.params.id)
res.json(checkAndchange(member))
})
// Modifie un membre avec ID
.put(async(req, res) => {
let updateMember = await Members.update(req.params.id, req.body.name)
res.json(checkAndchange(updateMember))
})
// Supprime un membre avec ID
.delete(async (req, res) => {
let deleteMember = await Members.delete(req.params.id)
res.json(checkAndchange(deleteMember))
})
MembersRouter.route('/')
// Récupère tous les membres
.get(async (req, res) => {
let allMembers = await Members.getAll(req.query.max)
res.json(checkAndchange((allMembers)))
})
// Ajoute un membre avec son nom
.post(async (req, res) => {
let addMember = await Members.add(req.body.name)
res.json(checkAndchange(addMember))
})
app.use(config.rootAPI+'members', MembersRouter)
app.listen(config.port, () => console.log('Started on port '+config.port))
}).catch((err)=>{
console.log('Error During database connection')
console.log(err.message)
})