-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapi.js
More file actions
94 lines (83 loc) · 2.55 KB
/
api.js
File metadata and controls
94 lines (83 loc) · 2.55 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
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
const { Pool, types } = require('pg');
types.setTypeParser(1700, parseFloat);
const pool = new Pool({
user: 'moviefan',
host: 'localhost',
database: 'movies',
password: 'password',
port: 5432
});
const inMemoryHorrors = [
{
name: 'The Hills Have Eyes',
rating: 7.8
},
{
name: 'The Night of Living Dead',
rating: 9.0
},
{
name: 'The Scream',
rating: 7.2
}
];
const getAllHorrors = async (request, response) => {
if (request.treatment == 'on') {
pool.query('SELECT * FROM horrors ORDER BY rating ASC', (error, results) => {
response.status(200).json(results.rows);
});
} else {
response.status(200).json(inMemoryHorrors);
}
};
const getHorrorById = (request, response) => {
const id = parseInt(request.params.id);
if (request.treatment == 'on') {
pool.query('SELECT * FROM horrors WHERE id = $1', [id], (error, results) => {
response.status(200).json(results.rows);
});
} else {
response.status(200).json(inMemoryHorrors[0]);
}
};
const addHorror = async (request, response) => {
const { name, rating } = request.body;
if (request.treatment == 'on') {
pool.query('INSERT INTO horrors (name, rating) VALUES ($1, $2)', [name, rating], (error, results) => {
response.status(201).send(`Horror added successfully.`);
});
} else {
inMemoryHorrors.push({ name, rating });
response.status(201).send(`Horror added successfully.`);
}
};
const updateHorror = (request, response) => {
const id = parseInt(request.params.id);
const { name, rating } = request.body;
if (request.treatment == 'on') {
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.`);
});
} else {
inMemoryHorrors[0] = { name, rating };
response.status(200).send(`Horror with id ${id} modified.`);
}
};
const deleteHorror = (request, response) => {
const id = parseInt(request.params.id);
if (request.treatment == 'on') {
pool.query('DELETE FROM horrors WHERE id = $1', [id], (error, results) => {
response.status(200).send(`Horror with id ${id} deleted.`);
});
} else {
inMemoryHorrors.shift();
response.status(200).send(`Horror with id ${id} deleted.`);
}
};
module.exports = {
getAllHorrors,
getHorrorById,
addHorror,
updateHorror,
deleteHorror
};