Skip to content

Commit

Permalink
Merge pull request #6 from Alan-eMartin/feature/backend
Browse files Browse the repository at this point in the history
added patch, and delete routes to list router
  • Loading branch information
Alan-eMartin authored Nov 25, 2020
2 parents 27f0be9 + 80c4e4d commit 7eb0d26
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions server/routers/list-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,65 @@ router.get('/lists', auth, async (req, res) => {
}
})

// get list by id
router.get('/lists/:id', auth, async (req, res) => {
const _id = req.params.id

try {
const list = await List.findOne({ _id, owner: req.user._id })

if (!list) {
res.status(404).send()
}
res.send(list)
} catch (err) {
res.status(500).send()
}
})

// Update Lists
router.patch('/lists/:id', auth, async (req, res) => {
// check that the fields being changed are all, or one of the following
const updates = Object.keys(req.body)
const allowedUpdates = ["title", "position"]
const isValidOperation = updates.every(update => {
return allowedUpdates.includes(update)
})

if (!isValidOperation) {
return res.status(400).send({ error: 'The field you are trying to update are invalid' })
}

try {
const list = await List.findOne({ _id: req.params.id, owner: req.user._id })

if (!list) {
return res.status(404).send()
}
updates.forEach(update => list[update] = req.body[update])
await list.save()
res.send(list)
} catch (err) {
res.status(500).send(err)
}
})

// Delete List
router.delete('/lists/:id', auth, async (req, res) => {
const _id = req.params.id

try {
const list = await List.findOneAndDelete({ _id, owner: req.user._id })

if (!list) {
res.status(404).send()
}
res.send(list)
} catch (err) {
res.status(500).send()
}
})



module.exports = router

0 comments on commit 7eb0d26

Please sign in to comment.