Skip to content

Commit

Permalink
#6: create api update/delete timeline
Browse files Browse the repository at this point in the history
  • Loading branch information
manhdg98 committed Mar 24, 2021
1 parent 78f1067 commit a64467a
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions curriculum-back/server/api/timeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,51 @@ router.route('/')
}
})

router.route('/')
.patch(async function (req, res) {
try {
const { id, time, title, content, image } = req.body;
let mapValidate = {
"time": time,
"title": title,
"content": content,
"image": image
}
let timeline;

if(id) {
timeline = await Timeline.findById(id);
for ( let [item, value] of Object.entries(mapValidate)) {
if(value === undefined || value === null) {
throw new Error(`Params ${item} invalid`);
}
}
timeline.time = time;
timeline.title = title;
timeline.content = content;
timeline.image = image;
}
await timeline.save()
res.send(201, timeline)
} catch (err) {
throw new Error(err)
}
})

router.route('/')
.delete(async function (req, res) {
try {
const { id } = req.body;

if(id) {
await Timeline.deleteOne({ _id: id });
}

res.send('Success')
} catch (err) {
throw new Error(err)
}
})


module.exports = router

0 comments on commit a64467a

Please sign in to comment.