-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
132 lines (111 loc) · 3.79 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
let express = require('express');
let app = express();
let bodyParser = require('body-parser');
let mysql = require('mysql');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// homepage route
app.get('/', (req, res) => {
return res.send({
error: false,
message: 'Welcome to RESTful CRUD API with NodeJS, Express, MYSQL',
written_by: 'Patiphan',
published_on: 'https://milerdev.dev'
})
})
// connection to mysql database
let dbCon = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'root999',
database: 'nodejs_api'
})
dbCon.connect();
// retrieve all books
app.get('/books', (req, res) => {
dbCon.query('SELECT * FROM books', (error, results, fields) => {
if (error) throw error;
let message = ""
if (results === undefined || results.length == 0) {
message = "Books table is empty";
} else {
message = "Successfully retrieved all books";
}
return res.send({ error: false, data: results, message: message});
})
})
// add a new book
app.post('/book', (req, res) => {
let name = req.body.name;
let author = req.body.author;
// validation
if (!name || !author) {
return res.status(400).send({ error: true, message: "Please provide book name and author."});
} else {
dbCon.query('INSERT INTO books (name, author) VALUES(?, ?)', [name, author], (error, results, fields) => {
if (error) throw error;
return res.send({ error: false, data: results, message: "Book successfully added"})
})
}
});
// retrieve book by id
app.get('/book/:id', (req, res) => {
let id = req.params.id;
if (!id) {
return res.status(400).send({ error: true, message: "Please provide book id"});
} else {
dbCon.query("SELECT * FROM books WHERE id = ?", id, (error, results, fields) => {
if (error) throw error;
let message = "";
if (results === undefined || results.length == 0) {
message = "Book not found";
} else {
message = "Successfully retrieved book data";
}
return res.send({ error: false, data: results[0], message: message })
})
}
})
// update book with id
app.put('/book', (req, res) => {
let id = req.body.id;
let name = req.body.name;
let author = req.body.author;
// validation
if (!id || !name || !author) {
return res.status(400).send({ error: true, message: 'Please provide book id, name and author'});
} else {
dbCon.query('UPDATE books SET name = ?, author = ? WHERE id = ?', [name, author, id], (error, results, fields) => {
if (error) throw error;
let message = "";
if (results.changedRows === 0) {
message = "Book not found or data are same";
} else {
message = "Book successfully updated";
}
return res.send({ error: false, data: results, message: message })
})
}
})
// delete book by id
app.delete('/book', (req, res) => {
let id = req.body.id;
if (!id) {
return res.status(400).send({ error: true, message: "Please provide book id"});
} else {
dbCon.query('DELETE FROM books WHERE id = ?', [id], (error, results, fields) => {
if (error) throw error;
let message = "";
if (results.affectedRows === 0) {
message = "Book not found";
} else {
message = "Book successfully deleted";
}
return res.send({ error: false, data: results, message: message })
})
}
})
app.listen(3000, () => {
console.log('Node App is running on port 3000');
})
module.exports = app;