Permalink
Browse files

full crud on users and ideas

  • Loading branch information...
spencermerryman committed Nov 8, 2018
1 parent bacb46f commit 279135324d3a936fdd070beaba23047c548353e4
Showing with 33 additions and 1 deletion.
  1. +29 −0 controllers/ideasController.js
  2. +1 −1 controllers/userController.js
  3. +3 −0 routes/index.js
@@ -15,6 +15,35 @@ const ideasController = {
.then((idea) => {
res.send(idea)
})
},
delete: (req, res) => {
var ideaId = req.params.ideaId
Idea.findByIdAndDelete(ideaId)
.then(() => {
res.send(200)
})
},
update: (req, res) => {
var ideaId = req.params.ideaId
Idea.findByIdAndUpdate(ideaId, req.body, { new: true })
.then((updatedIdea) => {
updatedIdea.save()
res.send(updatedIdea)
})
},
create: (req, res) => {
var userId = req.params.userId
User.findById(userId)
.then((user) => {
console.log(user)
Idea.create(req.body)
.then((newIdea) => {
console.log(newIdea)
user.ideas.push(newIdea)
user.save()
res.send(newIdea)
})
})
}

}
@@ -9,7 +9,7 @@ const userController = {
})
},
show: (req, res) => {
User.findById(req.params.userId)
User.findById(req.params.userId).populate('ideas')
.then((user) => {
res.send(user)
})
@@ -11,6 +11,9 @@ router.delete('/api/users/:userId', userController.delete)

router.get('/api/users/:userId/ideas', ideasController.index)
router.get('/api/ideas/:ideaId', ideasController.show)
router.delete('/api/ideas/:ideaId', ideasController.delete)
router.patch('/api/ideas/:ideaId', ideasController.update)
router.post('/api/users/:userId/ideas', ideasController.create)

module.exports = router

0 comments on commit 2791353

Please sign in to comment.