Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyendacthienngan committed Dec 29, 2021
1 parent a80661d commit f99d685
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
23 changes: 22 additions & 1 deletion controllers/users.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ exports.createUser = async function (request, response) {
};

exports.login = async function (request, response) {
//TO-DO: Check role admin or user?
const account = await Account.findOne({email: request.body.email});
if (!account) return response.status(422).send('Email is not correct');
const checkPassword = await bcrypt.compare(request.body.password, account.password);
Expand Down Expand Up @@ -88,6 +87,28 @@ exports.getAllUsers = function (req, res) {
});
};

exports.getUserById = function (req, res) {
const userId = req.params.id
User.findById(userId).exec(function (err, user) {
if (err) {
res.send(400, err);
} else {
res.send(user);
}
});
};

exports.getUserIdByUsername = function (req, res) {
const username = req.params.username
User.find({username: username}).exec(function (err, user) {
if (err) {
res.send(400, err);
} else {
res.send(user);
}
});
};

exports.getAvatar = async function (req, res) {
const key = req.params.key;
try {
Expand Down
1 change: 0 additions & 1 deletion controllers/video-info.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ exports.getAllPublicVideoInfos = function (req,res) {

exports.getVideoInfoById = function (req, res) {
const id = req.params.id
console.log("id: ", id)
Video.findById(id)
.exec(function(err, result) {
if (!err)
Expand Down
4 changes: 4 additions & 0 deletions routes/users.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ const router = express.Router();
const user = require('../controllers/users.controller');
const verifyToken = require('./../middlewares/verifyToken.middleware');

// router.get('/:id', verifyToken, user.getUserById);

router.get('/:username', verifyToken, user.getUserIdByUsername);

router.get('/', verifyToken, user.getAllUsers);

router.post('/', user.createUser);
Expand Down

0 comments on commit f99d685

Please sign in to comment.