-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.js
More file actions
97 lines (90 loc) · 2.24 KB
/
db.js
File metadata and controls
97 lines (90 loc) · 2.24 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
95
96
97
var spicedPg = require("spiced-pg");
var db = spicedPg("postgres:postgres:postgres@localhost:5432/imageboard");
exports.getImages = async () => {
const query = `
SELECT *
FROM images
ORDER BY id DESC
LIMIT 4
`;
const { rows } = await db.query(query);
return rows;
};
exports.saveImage = async (url, username, title, description) => {
const query = `
INSERT INTO images (url, username, title, description)
VALUES ($1, $2, $3, $4)
RETURNING *
`;
const { rows } = await db.query(query, [
url,
username || null,
title || null,
description || null
]);
return rows;
};
exports.getMoreImages = async lastId => {
const query = `
SELECT *, (
SELECT MIN(id)
FROM images
) AS last_id
FROM images
WHERE id < $1
ORDER BY id DESC
LIMIT 4
`;
const { rows } = await db.query(query, [lastId]);
return rows;
};
exports.saveComment = async (comment, username, image_id) => {
const query = `
INSERT INTO comments (comment, username, image_id)
VALUES ($1, $2, $3)
RETURNING comment, username, created_at
`;
const { rows } = await db.query(query, [comment || null, username || null, image_id]);
return rows;
};
exports.getImage = async id => {
const query = `
SELECT *, (
SELECT id
FROM images
WHERE id > $1
ORDER BY id ASC
LIMIT 1
) AS next_id, (
SELECT id
FROM images
WHERE id < $1
ORDER BY id DESC
LIMIT 1
) AS prev_id
FROM images
WHERE id = $1
`;
const { rows } = await db.query(query, [id]);
return rows;
};
exports.getComments = async image_id => {
const query = `
SELECT *
FROM comments
WHERE image_id = $1
ORDER BY created_at
DESC
`;
const { rows } = await db.query(query, [image_id]);
return rows;
};
exports.getTags = async image_id => {
const query = `
SELECT *
FROM tags
WHERE image_id = $1
`;
const { rows } = await db.query(query, [image_id]);
return rows;
};