-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.js
More file actions
74 lines (65 loc) · 2.45 KB
/
Copy pathdb.js
File metadata and controls
74 lines (65 loc) · 2.45 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
const spicedPg = require("spiced-pg");
const db = spicedPg(
// left hand side for heroku || right hand side for localhost //
process.env.DATABASE_URL || "postgres:jharding@localhost/images"
);
module.exports.getImages = function () {
const query = `SELECT * FROM images
ORDER BY id DESC
LIMIT 15;`;
return db.query(query);
};
module.exports.addImage = function (url, username, title, description) {
const query = `INSERT INTO images (url, username, title, description) VALUES ($1, $2, $3, $4)
RETURNING id`;
const params = [url, username, title, description];
return db.query(query, params);
};
module.exports.getImageInfo = function (id) {
const query = `SELECT *
FROM images
WHERE id = $1;`;
const params = [id];
return db.query(query, params);
};
module.exports.addComment = function (name, comment, id) {
const query = `INSERT INTO comments (comment, username, image_id) VALUES ($1, $2, $3)
RETURNING comment, username, created_at;`;
const params = [comment, name, id];
return db.query(query, params);
};
module.exports.getComments = function (id) {
const query = `SELECT id, username, comment, created_at
FROM comments
WHERE image_id = ${id}
ORDER BY id DESC;`;
return db.query(query);
};
module.exports.getMoreImages = function (id) {
const query = `SELECT url, title, id, (
SELECT id FROM images
ORDER BY id ASC
LIMIT 1
) AS "lowestId" FROM images
WHERE id < $1
ORDER BY id DESC
LIMIT 9;`;
const params = [id];
return db.query(query, params);
};
module.exports.getThread = function (commentId) {
const query = `SELECT username, reply, (
SELECT username FROM comments
WHERE id = $1) AS "commenter"
FROM replies
WHERE comment_id = $1;`;
const params = [commentId];
return db.query(query, params);
};
module.exports.addReply = function (reply, username, commentId) {
const query = `INSERT INTO replies (reply, username, comment_id)
VALUES ($1, $2, $3)
returning reply, username, comment_id;`;
const params = [reply, username, commentId];
return db.query(query, params);
};