Skip to content

Commit

Permalink
prepared routes
Browse files Browse the repository at this point in the history
  • Loading branch information
chelcie de almeida authored and chelcie de almeida committed Jun 16, 2021
1 parent 748cb18 commit 50a4e82
Show file tree
Hide file tree
Showing 9 changed files with 304 additions and 6 deletions.
118 changes: 118 additions & 0 deletions controllers/thought-controller.js
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;
110 changes: 110 additions & 0 deletions controllers/user-controller.js
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;
4 changes: 2 additions & 2 deletions models/Thought.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ ThoughtSchema.virtual('reactionCount').get(function() {
return this.reactions.length;
});

// create the Pizza model using the PizzaSchema
// create the Thought model using the thoughtSchema
const Thought = model('Thought', ThoughtSchema);

// export the Pizza model
// export the thought model
module.exports = Thought;
11 changes: 8 additions & 3 deletions models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ const UserSchema = new Schema(
ref: 'Thought'
}
],
friends: [this] //need to revisit this
friends: [
{
type: Schema.Types.ObjectId,
ref: 'User'
}
]
},
{
toJSON: {
Expand All @@ -35,8 +40,8 @@ UserSchema.virtual('friendCount').get(function() {
return this.friends.length;
});

// create the Pizza model using the PizzaSchema
// create the User model using the userSchema
const User = model('User', UserSchema);

// export the Pizza model
// export the User model
module.exports = User;
11 changes: 11 additions & 0 deletions routes/api/index.js
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;
21 changes: 21 additions & 0 deletions routes/api/thought-routes.js
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;
19 changes: 19 additions & 0 deletions routes/api/user-routes.js
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;
14 changes: 14 additions & 0 deletions routes/index.js
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;
2 changes: 1 addition & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.static('public'));

// app.use(require('./routes'));
app.use(require('./routes'));

mongoose.connect(process.env.MONGODB_URI || 'mongodb://localhost/social', {
useFindAndModify: false,
Expand Down

0 comments on commit 50a4e82

Please sign in to comment.