forked from Mohamed-Fdl/BookApi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·148 lines (124 loc) · 4.3 KB
/
server.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
const express = require('express')
const bodyParser = require('body-parser')
const session = require('express-session')
const app = express()
const Book = require('./models/book')
//mongo db connection
require('./connection')()
//jwt secret in env ,run into cli
//export TOKEN_SECRET = 'qqmvbuq368fbbg1'
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(bodyParser.json())
//session
app.set('trust proxy', 1) // trust first proxy
app.use(session({
secret: 'fdlencryption',
resave: true,
saveUninitialized: true,
cookie: { secure: true }
}))
//verify token througth headerAuth
const verifyToken = require('./middlewares/verifyToken')
//register controller
const registerController = require('./controllers/registerController')
//login controller
const loginController = require('./controllers/loginController')
//routes
app.get('/', function(req, res) {
res.send("<h1>Présentation de BookApi</h1>")
})
app.post('/register', function(req, res) {
registerController(req, res, (data) => {
res.json({
"message": data
})
})
})
app.post('/login', function(req, res) {
loginController(req, res, (data) => {
res.json({
"message": data
})
})
})
app.post('/new', verifyToken, function(req, res) { //i need name author parution year(year) and description
if (req.body.name === undefined || req.body.author === undefined || req.body.year === undefined || req.body.description === undefined) {
res.statusCode = 500
res.json({ "success": false, "message": "Send all informations:name,author,year,description" })
} else {
//recup datas
var name = req.body.name
var author = req.body.author
var year = parseInt(req.body.year)
var description = req.body.description
//create and store book
new Book({
name: name,
author: author,
year: year,
description: description
}).save().then(function() {
res.statusCode = 200
res.json({ "success": true, "message": "Informations sent successfully.Book have been" })
})
}
})
app.get('/book/:id', verifyToken, function(req, res) {
var id = req.params.id
Book.findById(id, function(err, book) {
if (err) console.log(err)
res.statusCode = 200
res.json({ "success": true, "data": book })
})
})
app.get('/book', verifyToken, function(req, res) {
(async function() {
const books = await Book.find({})
res.statusCode = 200
res.json({ "success": true, "data": books })
})()
})
app.put('/book/:id', verifyToken, function(req, res) {
var id = req.params.id
if (req.body.name === undefined || req.body.author === undefined || req.body.year === undefined || req.body.description === undefined) {
res.statusCode = 500
res.json({ "success": false, "message": "Send all informations:name,author,year,description" })
} else {
Book.findById(id, function(err, book) {
if (err) {
console.log(err)
res.statusCode = 500
res.json({ "success": false, "message": "No such book in database.Invalid ID" })
} else {
(async function() {
book.name = req.body.name
book.author = req.body.author
book.year = req.body.year
book.description = req.body.description
await book.save()
res.statusCode = 200
res.json({ "success": true, "message": "Book have been updated" })
})()
}
})
}
})
app.delete('/book/:id', verifyToken, function(req, res) {
var id = req.params.id
Book.findById(id, function(err, book) {
if (err) {
console.log(err)
res.statusCode = 500
res.json({ "success": false, "message": "No such book in database for deletion.Invalid ID" })
} else {
(async function() {
await book.deleteOne({ id: id })
res.statusCode = 200
res.json({ "success": true, "message": "Book have been updated" })
})()
}
})
})
app.listen(8080)