-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
129 lines (110 loc) · 3.89 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
const express = require('express');
const bodyParser = require('body-parser');
const mysql = require('mysql');
const app = express();
const port = process.env.PORT || 5000
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.listen(port, () => { console.log(`Server listening on http://localhost:${port}`) });
const pool = mysql.createPool({
connectionLimit: 10,
host: 'localhost',
port: 3306,
user: 'root',
password: '',
database: 'node-sql'
})
// Get all User
app.get('/api/users', (req, res) => {
pool.getConnection((err, connection) => {
try {
connection.query('SELECT * FROM users', (err, rows) => {
res.send(rows)
connection.release()
})
} catch (error) {
res.status(500).send({ message: error.message });
}
})
})
//Get User by ID
app.get('/api/user/:id', (req, res) => {
pool.getConnection((err, connection) => {
try {
connection.query('SELECT * FROM users WHERE id = ?', [req.params.id], (err, rows) => {
if (rows.length === 0) {
res.status(404);
res.send({ message: `User not found` })
} else {
res.send(rows)
connection.release()
}
})
} catch (error) {
res.status(500).send({ message: error.message });
}
})
})
// delete User By ID
app.delete('/api/user/delete/:id', (req, res) => {
pool.getConnection((err, connection) => {
try {
connection.query('SELECT * FROM users WHERE id = ?', [req.params.id], (err, rows) => {
if (rows.length === 0) {
res.status(404);
res.send({ message: `user not found` })
} else {
connection.query('DELETE FROM users WHERE id = ?', [req.params.id], (err, rows) => {
// res.send(rows)
res.send({ message: `user deleted successfully` })
connection.release()
})
}
})
} catch (error) {
res.status(500).send({ message: error.message });
}
})
})
//Add User
app.post('/api/user/add', (req, res) => {
pool.getConnection((err, connection) => {
try {
const pera = req.body;
connection.query('SELECT * FROM users WHERE email = ?', [pera.email], (err, rows) => {
if (rows.length !== 0) {
res.status(400);
res.send({ message: `User Already in Database` })
} else {
connection.query('INSERT INTO users SET ?', pera, (err, rows) => {
res.status(200).send({ message: 'User added successfully', User: pera })
connection.release()
})
}
})
} catch (error) {
res.status(500).send({ message: error.message });
}
})
})
// Update User
app.put('/api/user/update', (req, res) => {
pool.getConnection((err, connection) => {
try {
const pera = req.body;
connection.query('SELECT * FROM users WHERE email = ?', [pera.email], (err, rows) => {
if (rows.length === 0) {
res.status(400);
res.send({ message: `user not found` })
} else {
connection.query('UPDATE users SET ? WHERE users.email = ? ', [pera, pera.email], (err, rows) => {
res.status(200).send({ message: 'User Update successfully', User: pera })
connection.release()
})
}
})
} catch (error) {
res.status(500).send({ message: error.message });
}
})
})