-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
chelcie de almeida
authored and
chelcie de almeida
committed
Jun 16, 2021
1 parent
748cb18
commit 50a4e82
Showing
9 changed files
with
304 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
const { User, Thought } = require('../models'); | ||
|
||
const thoughtController = { | ||
// get all thoughts | ||
getAllThoughts(req, res) { | ||
Thought.find({}) | ||
.populate({ | ||
path: 'user', | ||
select: '-__v' | ||
}) | ||
.select('-__v') | ||
.then(dbThoughtData => res.json(dbThoughtData)) | ||
.catch(err => { | ||
console.log(err); | ||
res.status(400).json(err); | ||
}); | ||
}, | ||
|
||
// get one thought by id | ||
getThoughtById({ params }, res) { | ||
Thought.findOne({ _id: params.id }) | ||
.populate({ | ||
path: 'user', | ||
select: '-__v' | ||
}) | ||
.select('-__v') | ||
.then(dbThoughtData => { | ||
// If no Thought is found, send 404 | ||
if (!dbThoughtData) { | ||
res.status(404).json({ message: 'No Thought found with this id!' }); | ||
return; | ||
} | ||
res.json(dbThoughtData); | ||
}) | ||
.catch(err => { | ||
console.log(err); | ||
res.status(400).json(err); | ||
}); | ||
}, | ||
|
||
// createThought | ||
createThought({ body }, res) { | ||
Thought.create(body) | ||
.then(({ _id}) => { | ||
return User.findOneAndUpdate( | ||
{ username: body.username }, | ||
{ $push: { thoughts: _id }}, | ||
{ new: true} | ||
); | ||
}) | ||
.then(dbUserData => { | ||
if (!dbUserData) { | ||
res.status(404).json({ message: 'No User found with this username!'}); | ||
return; | ||
} | ||
res.json(dbUserData); | ||
}) | ||
.catch(err => res.json(err)); | ||
}, | ||
|
||
// update thought by id | ||
updateThought({ params, body }, res) { | ||
Thought.findOneAndUpdate({ _id: params.id }, body, { new: true, runValidators: true }) | ||
.then(dbThoughtData => { | ||
if (!dbThoughtData) { | ||
res.status(404).json({ message: 'No thought found with this id!' }); | ||
return; | ||
} | ||
res.json(dbThoughtData); | ||
}) | ||
.catch(err => res.status(400).json(err)); | ||
}, | ||
|
||
// delete thought | ||
deleteThought({ params }, res) { | ||
Thought.findOneAndDelete({ _id: params.id }) | ||
.then(dbThoughtData => { | ||
if (!dbThoughtData) { | ||
res.status(404).json({ message: 'No thought found with this id!' }); | ||
return; | ||
} | ||
res.json(dbThoughtData); | ||
}) | ||
.catch(err => res.status(400).json(err)); | ||
}, | ||
|
||
// create reaction in a single thought | ||
createReaction({ params, body }, res) { | ||
Thought.findOneAndUpdate( | ||
{ _id: params.thoughtId }, | ||
{ $push: { reactions: body }}, | ||
{ new: true, runValidators: true } | ||
) | ||
.then(dbThoughtData => { | ||
if (!dbThoughtData) { | ||
res.status(404).json({ message: 'No thought found with this id!'}); | ||
return; | ||
} | ||
res.json(dbThoughtData); | ||
}) | ||
.catch(err => res.json(err)); | ||
}, | ||
|
||
// delete reaction | ||
|
||
deleteReaction({ params }, res) { | ||
Thought.findOneAndDelete( | ||
{ _id: params.thoughtId }, | ||
{ $pull: { reactions: { reactionId: params.reactionId } } }, | ||
{ new: true } | ||
) | ||
.then(dbThoughtData => res.json(dbThoughtData)) | ||
.catch(err => res.json(err)); | ||
} | ||
|
||
} | ||
|
||
module.exports = thoughtController; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
const { User } = require('../models'); | ||
|
||
const userController = { | ||
// get all users | ||
getAllUser(req, res) { | ||
User.find({}) | ||
.select('-__v') | ||
.then(dbUserData => res.json(dbUserData)) | ||
.catch(err => { | ||
console.log(err); | ||
res.status(400).json(err); | ||
}); | ||
}, | ||
|
||
// get one User by id | ||
getUserById({ params }, res) { | ||
User.findOne({ _id: params.id }) | ||
.populate({ | ||
path: 'thoughts', | ||
select: '-__v' | ||
}) | ||
.populate({ | ||
path: 'friends', | ||
select: '-__v' | ||
}) | ||
.select('-__v') | ||
.then(dbUserData => { | ||
// If no User is found, send 404 | ||
if (!dbUserData) { | ||
res.status(404).json({ message: 'No User found with this id!' }); | ||
return; | ||
} | ||
res.json(dbUserData); | ||
}) | ||
.catch(err => { | ||
console.log(err); | ||
res.status(400).json(err); | ||
}); | ||
}, | ||
|
||
// createUser | ||
createUser({ body }, res) { | ||
User.create(body) | ||
.then(dbUserData => res.json(dbUserData)) | ||
.catch(err => res.status(400).json(err)); | ||
}, | ||
|
||
// update User by id | ||
updateUser({ params, body }, res) { | ||
User.findOneAndUpdate({ _id: params.id }, body, { new: true, runValidators: true }) | ||
.then(dbUserData => { | ||
if (!dbUserData) { | ||
res.status(404).json({ message: 'No User found with this id!' }); | ||
return; | ||
} | ||
res.json(dbUserData); | ||
}) | ||
.catch(err => res.status(400).json(err)); | ||
}, | ||
|
||
// delete User | ||
deleteUser({ params }, res) { | ||
User.findOneAndDelete({ _id: params.id }) | ||
.then(dbUserData => { | ||
if (!dbUserData) { | ||
res.status(404).json({ message: 'No User found with this id!' }); | ||
return; | ||
} | ||
res.json(dbUserData); | ||
}) | ||
.catch(err => res.status(400).json(err)); | ||
}, | ||
|
||
addFriend({ params }, res) { | ||
User.findOneAndUpdate( | ||
{_id: params.userId}, | ||
{ $push: { friends: params.friendId} }, | ||
{ new: true, runValidators: true} | ||
) | ||
.then(dbUserData => { | ||
if (!dbUserData) { | ||
res.status(404).json({ message: 'No user found with this id!'}); | ||
return; | ||
} | ||
res.json(dbUserData); | ||
}) | ||
.catch(err => res.json(err)); | ||
}, | ||
|
||
deleteFriend({ params }, res) { | ||
User.findOneAndDelete( | ||
{_id: params.userId}, | ||
{ $pull: { friends: params.friendId} }, | ||
{ new: true, runValidators: true} | ||
) | ||
.then(dbUserData => { | ||
if (!dbUserData) { | ||
res.status(404).json({ message: 'No user found with this id!'}); | ||
return; | ||
} | ||
res.json(dbUserData); | ||
}) | ||
.catch(err => res.json(err)); | ||
} | ||
|
||
|
||
|
||
} | ||
|
||
module.exports = userController; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
const router = require('express').Router(); | ||
const userRoutes = require('./user-routes'); | ||
const thoughtRoutes = require('./thought-routes') | ||
|
||
// add prefix of `/users` to routes created in `user-routes.js` | ||
router.use('/users', userRoutes); | ||
|
||
// add prefix of `/thoughts` to routes created in `thought-routes.js` | ||
router.use('/thoughts', thoughtRoutes) | ||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
const router = require('express').Router(); | ||
|
||
const { | ||
getAllThoughts, | ||
getThoughtById, | ||
createThought, | ||
updateThought, | ||
deleteThought, | ||
createReaction, | ||
deleteReaction | ||
} = require('../../controllers/thought-controller') | ||
|
||
router.route('/').get(getAllThoughts).post(createThought) | ||
|
||
router.route('/:id').get(getThoughtById).put(updateThought).delete(deleteThought) | ||
|
||
router.route('/:thoughtId/reactions').post(createReaction) | ||
|
||
router.route('/:thoughtId/reactionId').delete(deleteReaction) | ||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
const router = require('express').Router(); | ||
|
||
const { | ||
getAllUser, | ||
getUserById, | ||
createUser, | ||
updateUser, | ||
deleteUser, | ||
addFriend, | ||
deleteFriend | ||
} = require('../../controllers/user-controller') | ||
|
||
router.route('/').get(getAllUser).post(createUser) | ||
|
||
router.route('/:id').get(getUserById).put(updateUser).delete(deleteUser) | ||
|
||
router.route('/:userId/friends/:friendId').post(addFriend).delete(deleteFriend) | ||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
const router = require('express').Router(); | ||
// Import all of the API routes from /api/index.js (no need for index.js though since it's implied) | ||
const apiRoutes = require('./api'); | ||
|
||
|
||
// add prefix of `/api` to all of the api routes imported from the `api` directory | ||
router.use('/api', apiRoutes); | ||
|
||
|
||
router.use((req, res) => { | ||
res.status(404).send('<h1>😝 404 Error!</h1>'); | ||
}); | ||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters