-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
48 lines (43 loc) · 1.22 KB
/
index.js
File metadata and controls
48 lines (43 loc) · 1.22 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
import db from "../../../../../db";
export default async function handler(req, res) {
if (req.method === "GET") {
try {
const { rows } = await db.query(`
SELECT
projects.*,
users.name AS user_name
FROM
projects
JOIN users ON projects.created_by::bigint = users.id
`);
res.status(201).json({
success: true,
results: rows.length,
projects: rows,
});
} catch (error) {
res.status(500).json({
success: false,
error,
});
}
}
if (req.method === "POST") {
try {
const { name, createdBy } = req.body;
const { rows } = await db.query(
"INSERT INTO projects (name, created_on, created_by) VALUES ($1, NOW(), $2) RETURNING *",
[name, createdBy]
);
res.status(201).json({
success: true,
user: rows[0],
});
} catch (error) {
res.status(500).json({
success: true,
error,
});
}
}
}