forked from stevensblueprint/backend_challenge_spring_2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
197 lines (183 loc) · 5.39 KB
/
index.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
const express = require("express");
const { Pool } = require("pg");
const bodyParser = require("body-parser");
const { v4: uuidv4 } = require("uuid");
const app = express();
app.use(bodyParser.json());
const pool = new Pool({
user: "your_db_user",
host: "localhost",
database: "volunteer_db",
password: "your_db_password",
port: 5432,
});
// GET /api/volunteers - List Volunteers
app.get("/api/volunteers", async (req, res) => {
try {
const result = await pool.query("SELECT * FROM volunteers");
res.status(200).json(result.rows);
} catch (err) {
res.status(500).send("Server Error");
}
});
// POST /api/volunteers - Create a New Volunteer
app.post("/api/volunteers", async (req, res) => {
const {
first_name,
last_name,
email,
phone_number,
date_of_birth,
address,
skills,
availability,
background_check,
} = req.body;
const volunteer_id = uuidv4();
const date_joined = new Date();
try {
const result = await pool.query(
"INSERT INTO volunteers (volunteer_id, first_name, last_name, email, phone_number, date_of_birth, address, skills, availability, date_joined, background_check) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) RETURNING *",
[
volunteer_id,
first_name,
last_name,
email,
phone_number,
date_of_birth,
address,
skills,
availability,
date_joined,
background_check,
]
);
res.status(201).json(result.rows[0]);
} catch (err) {
res.status(500).send("Server Error");
}
});
// GET /api/volunteers/:volunteerId - Get Volunteer Details
app.get("/api/volunteers/:volunteerId", async (req, res) => {
const { volunteerId } = req.params;
try {
const result = await pool.query(
"SELECT * FROM volunteers WHERE volunteer_id = $1",
[volunteerId]
);
if (result.rows.length === 0) {
return res.status(404).send("Volunteer not found");
}
res.status(200).json(result.rows[0]);
} catch (err) {
res.status(500).send("Server Error");
}
});
// PUT /api/volunteers/:volunteerId - Update Volunteer Information
app.put("/api/volunteers/:volunteerId", async (req, res) => {
const { volunteerId } = req.params;
const {
first_name,
last_name,
email,
phone_number,
date_of_birth,
address,
skills,
availability,
background_check,
} = req.body;
try {
const result = await pool.query(
"UPDATE volunteers SET first_name = $1, last_name = $2, email = $3, phone_number = $4, date_of_birth = $5, address = $6, skills = $7, availability = $8, background_check = $9 WHERE volunteer_id = $10 RETURNING *",
[
first_name,
last_name,
email,
phone_number,
date_of_birth,
address,
skills,
availability,
background_check,
volunteerId,
]
);
if (result.rows.length === 0) {
return res.status(404).send("Volunteer not found");
}
res.status(200).json(result.rows[0]);
} catch (err) {
res.status(500).send("Server Error");
}
});
// DELETE /api/volunteers/:volunteerId - Delete a Volunteer
app.delete("/api/volunteers/:volunteerId", async (req, res) => {
const { volunteerId } = req.params;
try {
const result = await pool.query(
"DELETE FROM volunteers WHERE volunteer_id = $1 RETURNING *",
[volunteerId]
);
if (result.rows.length === 0) {
return res.status(404).send("Volunteer not found");
}
res.status(200).send("Volunteer deleted");
} catch (err) {
res.status(500).send("Server Error");
}
});
// Additional Routes
// GET /api/volunteers/:volunteerId/skills
app.get("/api/volunteers/:volunteerId/skills", async (req, res) => {
const { volunteerId } = req.params;
try {
const result = await pool.query(
"SELECT skills FROM volunteers WHERE volunteer_id = $1",
[volunteerId]
);
if (result.rows.length === 0) {
return res.status(404).send("Volunteer not found");
}
res.status(200).json(result.rows[0].skills);
} catch (err) {
res.status(500).send("Server Error");
}
});
// POST /api/volunteers/:volunteerId/skills - Add new skills
app.post("/api/volunteers/:volunteerId/skills", async (req, res) => {
const { volunteerId } = req.params;
const { newSkills } = req.body; // Expecting an array of new skills
try {
const result = await pool.query(
"UPDATE volunteers SET skills = array_cat(skills, $1) WHERE volunteer_id = $2 RETURNING *",
[newSkills, volunteerId]
);
if (result.rows.length === 0) {
return res.status(404).send("Volunteer not found");
}
res.status(200).json(result.rows[0].skills);
} catch (err) {
res.status(500).send("Server Error");
}
});
// DELETE /api/volunteers/:volunteerId/skills/:skillId - Remove a skill
app.delete("/api/volunteers/:volunteerId/skills/:skillId", async (req, res) => {
const { volunteerId, skillId } = req.params;
try {
const result = await pool.query(
"UPDATE volunteers SET skills = array_remove(skills, $1) WHERE volunteer_id = $2 RETURNING *",
[skillId, volunteerId]
);
if (result.rows.length === 0) {
return res.status(404).send("Volunteer not found");
}
res.status(200).json(result.rows[0].skills);
} catch (err) {
res.status(500).send("Server Error");
}
});
// Start the server
app.listen(3000, () => {
console.log("Server is running on port 3000");
});