Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made code leaner by outsourcing routes logic to controllers and updated the entire project to use ES6+ syntax #104

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Outsourced logic inside routes/api/profiles.js to controllers in cont…
…rollers/profiles.js
Sayan3sarkar committed Jul 24, 2020
commit 0df362f65b60d81acc296f71a871c4d98c418195
39 changes: 39 additions & 0 deletions controllers/profiles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const mongoose = require('mongoose');
const User = mongoose.model('User');

exports.getUsernameController = (req, res, next) =>{
if(req.payload){
User.findById(req.payload.id).then(user =>{
if(!user){ return res.json({profile: req.profile.toProfileJSONFor(false)}); }

return res.json({profile: req.profile.toProfileJSONFor(user)});
});
} else {
return res.json({profile: req.profile.toProfileJSONFor(false)});
}
};


exports.getUsernameFollowController = (req, res, next) =>{
const profileId = req.profile._id;

User.findById(req.payload.id).then(user =>{
if (!user) { return res.sendStatus(401); }

return user.follow(profileId).then(() => {
return res.json({profile: req.profile.toProfileJSONFor(user)});
});
}).catch(next);
};

exports.deleteUsernameFollowController = (req, res, next) =>{
var profileId = req.profile._id;

User.findById(req.payload.id).then(user =>{
if (!user) { return res.sendStatus(401); }

return user.unfollow(profileId).then(() => {
return res.json({profile: req.profile.toProfileJSONFor(user)});
});
}).catch(next);
};
39 changes: 4 additions & 35 deletions routes/api/profiles.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const router = require('express').Router();
const mongoose = require('mongoose');
const User = mongoose.model('User');
const auth = require('../auth');
const profilesController = require('../../controllers/profiles');

// Preload user profile on routes with ':username'
router.param('username', (req, res, next, username) => {
@@ -14,40 +13,10 @@ router.param('username', (req, res, next, username) => {
}).catch(next);
});

router.get('/:username', auth.optional, (req, res, next) =>{
if(req.payload){
User.findById(req.payload.id).then(user =>{
if(!user){ return res.json({profile: req.profile.toProfileJSONFor(false)}); }
router.get('/:username', auth.optional, profilesController.getUsernameController);

return res.json({profile: req.profile.toProfileJSONFor(user)});
});
} else {
return res.json({profile: req.profile.toProfileJSONFor(false)});
}
});

router.post('/:username/follow', auth.required, (req, res, next) =>{
var profileId = req.profile._id;

User.findById(req.payload.id).then(user =>{
if (!user) { return res.sendStatus(401); }

return user.follow(profileId).then(() => {
return res.json({profile: req.profile.toProfileJSONFor(user)});
});
}).catch(next);
});

router.delete('/:username/follow', auth.required, (req, res, next) =>{
var profileId = req.profile._id;

User.findById(req.payload.id).then(user =>{
if (!user) { return res.sendStatus(401); }
router.post('/:username/follow', auth.required, profilesController.getUsernameFollowController);

return user.unfollow(profileId).then(() => {
return res.json({profile: req.profile.toProfileJSONFor(user)});
});
}).catch(next);
});
router.delete('/:username/follow', auth.required, profilesController.deleteUsernameFollowController);

module.exports = router;