-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
41 lines (36 loc) · 922 Bytes
/
index.js
File metadata and controls
41 lines (36 loc) · 922 Bytes
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
import db from "../../../../../db";
export default async function handler(req, res) {
if (req.method === "GET") {
try {
const { rows } = await db.query("select * from users");
res.status(201).json({
success: true,
results: rows.length,
users: rows,
});
} catch (error) {
res.status(500).json({
success: false,
error,
});
}
}
if (req.method === "POST") {
try {
const { fullName, email, role } = req.body;
const results = await db.query(
"INSERT INTO users (full_name, email, role, assigned_project, created_on) VALUES ($1, $2, $3, 0, NOW()) returning *",
[fullName, email, role]
);
res.status(201).json({
success: true,
user: results.rows[0],
});
} catch (error) {
res.status(500).json({
success: true,
error,
});
}
}
}