Skip to content

Commit 34b9aaf

Browse files
committed
feat(proposals): add delete route and clean up logging
1 parent 7b85f36 commit 34b9aaf

2 files changed

Lines changed: 23 additions & 4 deletions

File tree

routes/proposals.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ router.get("/", async (req, res) => {
1616
.json({message: 'Error fetching proposals: ' + formatQueryErrorResponse(e)})
1717
}
1818

19+
console.log(e)
1920
return res.status(500).json({message: 'Server Error'})
2021
}
2122
});
@@ -29,9 +30,9 @@ router.post("/", async (req, res) => {
2930

3031
try{
3132
const proposal = await ProposalsService.store(data);
32-
console.log("Created Proposal", JSON.stringify(proposal));
3333
return res.status(201).json({ success: true, proposal: JSON.stringify(proposal)});
3434
}catch(e){
35+
console.log(e)
3536
return res.status(500).json({message: 'Server Error'})
3637
}
3738
});
@@ -42,6 +43,7 @@ router.get("/:id", async (req, res) => {
4243
const proposal = await ProposalsService.show(id);
4344
return res.status(200).json(proposal);
4445
}catch(e){
46+
console.log(e)
4547
return res.status(500).json({message: 'Server Error'})
4648
}
4749
});
@@ -58,13 +60,20 @@ router.put("/:id", async (req, res) => {
5860
const result = await ProposalsService.update(id, validationResult.data);
5961
res.status(200).json(result);
6062
}catch(e){
63+
console.log(e)
6164
return res.status(500).json({message: 'Server Error'})
6265
}
6366
});
6467

65-
router.delete("/:id", (req, res) => {
68+
router.delete("/:id", async (req, res) => {
6669
const { id } = req.params;
67-
res.status(204).json(); // No Content
70+
try {
71+
const result = await ProposalsService.destroy(id);
72+
return res.status(200).json({count: result.rowCount, rows:result.rows});
73+
}catch(e){
74+
console.log(e)
75+
return res.status(500).json({message: 'Server Error'})
76+
}
6877
});
6978

7079
export default router;

services/proposals.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { getPool } from '../database';
2-
import { sql } from 'slonik';
2+
import { sql} from 'slonik';
33
import {update as slonikUpdate} from 'slonik-utilities';
44
import { Proposal, PendingProposal, ProposalUpdate } from '../types/proposal';
55

@@ -61,9 +61,19 @@ async function update(id: string, data: ProposalUpdate) {
6161
});
6262
}
6363

64+
async function destroy(id: string) {
65+
const pool = await getPool();
66+
return await pool.connect(async (connection) => {
67+
return await connection.query(sql.unsafe`
68+
DELETE FROM proposals WHERE id = ${id} RETURNING id, title;
69+
`)
70+
});
71+
}
72+
6473
export default {
6574
index,
6675
store,
6776
show,
6877
update,
78+
destroy,
6979
}

0 commit comments

Comments
 (0)