-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
153 lines (142 loc) · 4.29 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
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
149
150
151
152
153
require('dotenv').config()
// import dependencies that you want to use
const express = require('express');
const app = express()
const bodyParser = require('body-parser');
const logger = require('morgan')
const mysql = require('mysql')
// use module
app.use(logger('dev'))
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json());
const port = process.env.PORT || 3000;
// initialize mysql connection
const connection = mysql.createConnection({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME
})
connection.connect(err => {
if(err) throw err;
})
app.listen(port, () => {
console.log('Server is running on port ' + port)
})
app.get('/', function(req, res){
connection.query('SELECT * FROM `laptop`', function(err, results, fields){
if(err) {
console.log(err)
} else {
res.status(200).json({
status: 200,
error: false,
message: 'Successfully get all data!',
data: results
})
}
})
})
app.get('/:id', function(req, res){
connection.query('SELECT * FROM `laptop` WHERE `id` = ?', req.params.id, function(err, results, fields){
if(err) {
console.log(err)
} else {
if(results.length > 0) {
res.status(200).json({
status: 200,
error: false,
message: 'Successfully get single data!',
data: results
})
} else {
res.status(400).json({
status: 400,
error: true,
message: 'No data found!',
data: results
})
}
}
})
})
app.post('/', (req, res) => {
// const name = req.body.name ? req.body.name : 'no data' || ternary operator
// const desc = req.body.desc ? req.body.desc : 'no data'
// const price = req.body.price ? req.body.price : 'no data'
const { name, description, price } = req.body
connection.query('INSERT INTO `laptop` (name, description, price) values (?, ?, ?)',
[name, description, price], function(err, results){
if(err) {
console.log(err)
res.status(400).json({
status: 400,
message: 'Error add new data!',
})
} else {
res.status(200).json({
status: 200,
error: false,
message: 'Successfully add new data!',
data: req.body
})
}
})
// res.status(200).json({
// status: 200,
// message: 'You have successfully add new data!',
// data: name
// })
})
app.put('/:id', function(req, res){
const { name, description, price } = req.body;
if(!name || !description || !price) {
res.status(300).json({
status: 300,
error: true,
message: 'name, description and price needed for update!',
})
} else {
connection.query('UPDATE `laptop` SET name = ?, description = ?, price = ?',
[ name, description, price],
function(err, results) {
if(err) {
console.log(err)
} else {
res.status(200).json({
status: 200,
error: false,
message: 'Successfully update data with id: ' + req.params.id,
data: req.body
})
}
})
}
})
app.delete('/:id', function(req, res){
connection.query('DELETE from `laptop` WHERE `id` = ?',
[ req.params.id ],
function(err, results) {
if(err) {
console.log(err)
} else {
if(results.affectedRows > 0 ){
res.status(200).json({
status: 200,
error: false,
message: 'Successfully delete data with id: ' + req.params.id,
})
} else {
res.status(400).json({
status: 400,
error: true,
message: 'Cannot delete data with id: ' + req.params.id,
})
}
}
})
})
app.get('*', function(req, res){
console.log('someone access 404')
res.send('404 Not Found!')
})