-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
45 lines (39 loc) · 1.01 KB
/
db.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
const Pool = require('pg').Pool
const pool = new Pool({
user: process.env.YF_DB_USER,
host: process.env.YF_DB_HOST,
database: 'api',
password: process.env.YF_DB_PASSWORD,
port: 5432
})
const getUsers = (request, response) => {
pool.query('SELECT * FROM users ORDER BY id ASC', (error, results) => {
if (error) {
throw error
}
response.status(200).json(results.rows)
})
}
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)
})
}
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: ${results.insertId}`)
})
}
module.exports = {
getUsers,
getUserById,
createUser
}