Skip to content

Commit 9c082d1

Browse files
committed
🐛 QUIZZ fix delete/:id and patch/:id
1 parent 306de05 commit 9c082d1

File tree

1 file changed

+14
-20
lines changed

1 file changed

+14
-20
lines changed

back/routes/quizz.js

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -86,40 +86,34 @@ router
8686

8787
.delete('/:id/delete',
8888
async (req, res) => {
89-
const result = await pool.query('DELETE FROM quizz WHERE id_quizz = $1', [req.params.id]);
90-
if (result.rowCount === 0) {
89+
const result = await pool.query('DELETE FROM quizz WHERE id_quizz = $1', [req.params.id], (err, res) => {
90+
if (err) return;
91+
else return res;
92+
});
93+
if (result === undefined) {
9194
return res.status(404)
9295
.send({
9396
error: 'Quizz can\'t be deleted because it doesn\'t exist'
9497
});
9598
}
96-
res.status(204).end();
99+
res.status(204).send(res);
97100
})
98101

99102
.patch('/:id',
100103
upload.single('file'), async (req, res) => {
101104

102-
let result = undefined;
103-
104-
if (req.body.path_file && req.file) {
105-
result = await pool.query('UPDATE quizz SET path_file=$1 WHERE id_quizz=$2', [req.body.path_file, req.params.id]);
106-
} else if(req.body.path_file && !req.file) {
107-
return res.status(500).send({error:'You cant modify the path_file without providing an image'});
105+
if (req.body.title !== '') {
106+
await pool.query('UPDATE quizz SET title = $1 WHERE id_quizz=$2', [req.body.title, req.params.id]);
108107
}
109108

110-
if (req.body.title) {
111-
result = await pool.query('UPDATE quizz SET title = $1 WHERE id_quizz=$2', [req.body.title, req.params.id]);
109+
if (req.body.path_file !== '') {
110+
await pool.query('UPDATE quizz SET path_file=$1 WHERE id_quizz=$2', [req.body.path_file, req.params.id]);
112111
}
113-
114-
if (req.body.difficulty) {
115-
result = await pool.query('UPDATE quizz SET difficulty=$1 WHERE id_quizz=$2', [req.body.difficulty, req.params.id]);
112+
113+
if (req.body.difficulty !== '') {
114+
await pool.query('UPDATE quizz SET difficulty=$1 WHERE id_quizz=$2', [req.body.difficulty, req.params.id]);
116115
}
117-
118-
if(result === undefined) {
119-
return res.status(500).send({error:'You didnt provide info for one or more field nor quizz doesnt exist'});
120-
}else if(result.rowCount === 0) {
121-
return res.status(404).send({error: 'Quizz not found and can\'t be patched'});
122-
}
116+
123117
res.status(204).end();
124118

125119
})

0 commit comments

Comments
 (0)