-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.js
More file actions
executable file
·53 lines (47 loc) · 1.52 KB
/
api.js
File metadata and controls
executable file
·53 lines (47 loc) · 1.52 KB
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
const Pool = require('pg').Pool;
const pool = new Pool({
user: 'moviefan',
host: 'localhost',
database: 'movies',
password: 'password',
port: 5432
});
const getAllHorrors = async (request, response) => {
pool.query('SELECT * FROM horrors ORDER BY rating ASC', (error, results) => {
response.status(200).json(results.rows);
});
};
const getHorrorById = (request, response) => {
const id = parseInt(request.params.id);
pool.query('SELECT * FROM horrors WHERE id = $1', [id], (error, results) => {
response.status(200).json(results.rows);
});
};
const addHorror = async (request, response) => {
const { name, rating } = request.body;
pool.query('INSERT INTO horrors (name, rating) VALUES ($1, $2)', [name, rating], (error, results) => {
response.status(201).send(`Horror added successfuly.`);
});
};
const updateHorror = (request, response) => {
const id = parseInt(request.params.id);
const { name, rating } = request.body;
pool.query(
'UPDATE horrors SET name = $1, rating = $2 WHERE id = $3', [name, rating, id], (error, results) => {
response.status(200).send(`Horror with id ${id} modified.`);
}
);
};
const deleteHorror = (request, response) => {
const id = parseInt(request.params.id);
pool.query('DELETE FROM horrors WHERE id = $1', [id], (error, results) => {
response.status(200).send(`Horror with id ${id} deleted.`);
});
};
module.exports = {
getAllHorrors,
getHorrorById,
addHorror,
updateHorror,
deleteHorror
};