-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueries.js
88 lines (76 loc) · 2.18 KB
/
queries.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
/**
* In a production environment, you would want to put your
* configuration details in a separate file with restrictive
* permissions that is not accessible from version control.
*/
// Creates a pool of connections to avoid opening a new client for each query
const Pool = require('pg').Pool;
const pool = new Pool({
user: 'user_admin',
host: 'localhost',
database: 'users_api',
password: 'password',
port: 5433,
});
// gets all users
const getUsers = (request, response) => {
pool.query('SELECT * FROM users ORDER BY id ASC', (error, results) => {
if (error) {
console.log('ERROR message ' + error)
throw error;
}
response.status(200).json(results.rows);
})
};
// gets single user by id
const getUserById = (request, response) => {
const id = parseInt(request.params.id);
pool.query('SELECT * FROM users WHERE id = $1', [id], (error, results) => {
if (error) {
throw error;
}
response.status(200).json(results.rows);
})
};
// creates a new user
const createUser = (request, response) => {
const { name, email } = request.body;
pool.query('INSERT INTO users (name, email) VALUES ($1, $2)', [name, email], (error, results) => {
if (error) {
throw error;
}
response.status(201).send(`User added with ID: ${result.insertId}`);
})
};
// updates a user by id
const updateUser = (request, response) => {
const id = parseInt(request.params.id)
const { name, email } = request.body
pool.query(
'UPDATE users SET name = $1, email = $2 WHERE id = $3',
[name, email, id],
(error, results) => {
if (error) {
throw error;
}
response.status(200).send(`User modified with ID: ${id}`);
}
)
};
// deletes user by id
const deleteUser = (request, response) => {
const id = parseInt(request.params.id);
pool.query('DELETE FROM users WHERE id = $1', [id], (error, results) => {
if (error) {
throw error;
}
response.status(200).send(`User deleted with ID: ${id}`)
});
}
module.exports = {
getUsers,
getUserById,
createUser,
updateUser,
deleteUser,
}