Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
250 lines (194 loc) · 5.01 KB
/
Copy pathserver.js
File metadata and controls
250 lines (194 loc) · 5.01 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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
const express = require('express');
const { pool, initializeDatabase } = require('./db');
const app = express();
const PORT = process.env.PORT || 3000;
app.use(express.json());
app.use(express.static('public'));
/* =========================================================
GET ALL ISSUES
========================================================= */
app.get('/api/issues', async (req, res) => {
try {
const result = await pool.query(
'SELECT * FROM issues ORDER BY id DESC'
);
res.json(result.rows);
} catch (error) {
console.error('GET /api/issues error:', error);
res.status(500).json({
error: 'Failed to fetch issues'
});
}
});
/* =========================================================
CREATE ISSUE
========================================================= */
app.post('/api/issues', async (req, res) => {
try {
const { title, description, category } = req.body;
if (!title || title.trim().length < 5) {
return res.status(400).json({
error: 'Title must be at least 5 characters'
});
}
if (!category || category.trim() === '') {
return res.status(400).json({
error: 'Category is required'
});
}
const result = await pool.query(
`
INSERT INTO issues
(title, description, category)
VALUES
($1, $2, $3)
RETURNING *
`,
[
title.trim(),
description || '',
category.trim()
]
);
res.status(201).json(result.rows[0]);
} catch (error) {
console.error('POST /api/issues error:', error);
res.status(500).json({
error: 'Failed to create issue'
});
}
});
/* =========================================================
UPDATE ISSUE
========================================================= */
app.put('/api/issues/:id', async (req, res) => {
try {
const id = parseInt(req.params.id, 10);
if (Number.isNaN(id)) {
return res.status(400).json({
error: 'Invalid issue ID'
});
}
// Check that the issue exists
const existingResult = await pool.query(
'SELECT * FROM issues WHERE id = $1',
[id]
);
if (existingResult.rows.length === 0) {
return res.status(404).json({
error: 'Issue not found'
});
}
const existing = existingResult.rows[0];
const {
title,
description,
category,
status
} = req.body;
// Validate title
if (
title !== undefined &&
(!title || title.trim() === '')
) {
return res.status(400).json({
error: 'Title cannot be empty'
});
}
// Accept both UI labels and database values
const statusMap = {
open: 'open',
'Open': 'open',
'in-progress': 'in-progress',
'In Progress': 'in-progress',
resolved: 'resolved',
'Resolved': 'resolved'
};
let normalizedStatus = existing.status;
if (status !== undefined) {
normalizedStatus = statusMap[status];
if (!normalizedStatus) {
return res.status(400).json({
error: 'Invalid status value'
});
}
}
const result = await pool.query(
`
UPDATE issues
SET
title = $1,
description = $2,
category = $3,
status = $4
WHERE id = $5
RETURNING *
`,
[
title !== undefined
? title.trim()
: existing.title,
description !== undefined
? description
: existing.description,
category !== undefined
? category
: existing.category,
normalizedStatus,
id
]
);
res.json(result.rows[0]);
} catch (error) {
console.error('PUT /api/issues/:id error:', error);
res.status(500).json({
error: 'Failed to update issue'
});
}
});
/* =========================================================
DELETE ISSUE
========================================================= */
app.delete('/api/issues/:id', async (req, res) => {
try {
const id = parseInt(req.params.id, 10);
if (Number.isNaN(id)) {
return res.status(400).json({
error: 'Invalid issue ID'
});
}
const result = await pool.query(
'DELETE FROM issues WHERE id = $1 RETURNING *',
[id]
);
if (result.rows.length === 0) {
return res.status(404).json({
error: 'Issue not found'
});
}
res.status(204).send();
} catch (error) {
console.error('DELETE /api/issues/:id error:', error);
res.status(500).json({
error: 'Failed to delete issue'
});
}
});
/* =========================================================
START SERVER
========================================================= */
async function startServer() {
try {
await initializeDatabase();
app.listen(PORT, '0.0.0.0', () => {
console.log(`Server running on port ${PORT}`);
});
} catch (error) {
console.error(
'Failed to initialize database:',
error
);
process.exit(1);
}
}
startServer();