-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi-handler.js
126 lines (104 loc) · 3.34 KB
/
api-handler.js
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
const pg = require("pg");
const { Pool } = pg;
const dotenv = require("dotenv");
dotenv.config();
const pool = new Pool({
user: process.env.DB_USER,
host: process.env.DB_HOST,
database: process.env.DB_DATABASE,
password: process.env.DB_PASSWORD,
port: process.env.DB_PORT,
});
exports.getHealthCheck = async (req, res) => {
const check = {
message: "OK",
timestamp: Date.now(),
uptime: process.uptime(),
};
try {
const client = await pool.connect();
client.release();
res.status(200).json(check);
} catch (error) {
check.message = error;
res.status(503).json(check);
}
};
exports.getStudentById = async (req, res) => {
const { id } = req.params;
if (isNaN(id)) {
return res.status(400).send("Invalid Student ID");
}
const doesIdExists = await pool.query(
"SELECT EXISTS(SELECT 1 FROM students WHERE id = $1)",
[parseInt(id)]
);
doesIdExists.rows[0].exists
? pool
.query(`Select * from students where id=$1;`, [parseInt(id)])
.then((results) => res.status(200).send(results.rows[0]))
.catch((e) => console.log("Error getStudentById request =>", e))
: res.status(400).send("Student ID does not exist");
};
exports.getAllStudents = (req, res) => {
return pool
.query(`SELECT * FROM students`)
.then((result) => res.send(result.rows))
.catch((e) => console.log("error", e));
};
exports.addStudentEntry = (req, res) => {
const { name, age, department } = req.body;
let dbArgs = Object.keys(req.body);
let dbVals = [];
let query = "";
name && dbVals.push(name);
age && dbVals.push(age);
department && dbVals.push(department);
const updateStr = [...dbArgs].map((item, index) => item).join(",");
query = `insert into students(${updateStr}) values($1,$2,$3) returning *;`;
query
? pool
.query(query, dbVals)
.then((result) => res.send(result.rows[0]))
.catch((e) => console.log("error", e))
: res.status(400).send("Bad Request");
};
exports.updateStudentEntry = async (req, res) => {
const { name, age, department } = req.body;
const { id } = req.params;
if (isNaN(id)) {
return res.status(400).send("Invalid Student ID");
}
const doesIdExists = await pool.query(
"SELECT EXISTS(SELECT 1 FROM students WHERE id = $1)",
[parseInt(id)]
);
// eslint-disable-next-line no-extra-boolean-cast
Boolean(doesIdExists.rows[0].exists)
? pool
.query(
"update students set id=$1,name=$2,age=$3,department=$4 where id=$5;",
[parseInt(id), name, age, department, parseInt(id)]
)
.then(() =>
res.status(200).send(`Student Entry modified with id:${id}`)
)
.catch((e) => console.log("Error PUT request =>", e))
: res.status(400).send("Student ID does not exist");
};
exports.deleteStudentEntry = async (req, res) => {
const { id } = req.params;
if (isNaN(id)) {
return res.status(400).send("Invalid Student ID");
}
const doesIdExists = await pool.query(
"SELECT EXISTS(SELECT 1 FROM students WHERE id = $1)",
[parseInt(id)]
);
doesIdExists
? pool
.query("delete from students where id=$1", [parseInt(id)])
.then(() => res.status(200).send(`Deleted student entry with id:${id}`))
.catch((e) => console.log("Error DELETE request =>", e))
: res.status(400).send("Student ID does not exist");
};