Skip to content

Commit

Permalink
Feature(profile): User profile
Browse files Browse the repository at this point in the history
-getting user profile
-editing user profile

[Delivers #164489929]
  • Loading branch information
P.C. Ndayisenga authored and Ruganda Mubarak committed Apr 12, 2019
1 parent 299dec4 commit d5106f0
Show file tree
Hide file tree
Showing 7 changed files with 424 additions and 241 deletions.
63 changes: 63 additions & 0 deletions controllers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import dotenv from 'dotenv';
import models from '../models/index';
import sendEmail from '../helpers/sendEmail/callMailer';
import generateToken from '../helpers/token';
import Validate from '../helpers/validateUser';

dotenv.config();
const User = models.user;
Expand Down Expand Up @@ -193,6 +194,68 @@ class UserController {
res.status(500).json({ error: error.message });
});
}

/**
* Get User Profile.
* @param {Object} req The request object.
* @param {Object} res The response object.
* @returns {Object} The response object.
*/
getProfile(req, res) {
if (!Number.isInteger(Number(req.params.id))) {
return res.status(400).json({ status: 400, error: 'The User ID must be an integer' });
}
const id = parseInt(req.params.id, 10);
return User.findOne({ where: { id, isActivated: true } })
.then((user) => {
if (!user) {
res.status(404).json({ status: 404, error: 'User not found' });
} else {
const profile = {
username: user.username,
email: user.email,
bio: user.bio,
image: user.image
};

res.status(200).json({ status: 200, profile });
}
}).catch((error) => {
res.status(500).json({ error: error.message });
});
}

/**
* Update User Profile.
* @param {Object} req The request object.
* @param {Object} res The response object.
* @returns {Object} The response object.
*/
async updateProfile(req, res) {
try {
const { id } = req.user;
if (Validate.isEmpty(req.body.username)) {
return res.status(400).json({ status: 400, error: 'Please provide a username' });
}
const updatedUser = await User.update(
{
username: req.body.username,
bio: req.body.bio,
image: req.file ? req.file.url : null
},
{ where: { id }, returning: true, plain: true }
);
const newProfile = {
username: updatedUser[1].username,
email: updatedUser[1].email,
image: updatedUser[1].image,
bio: updatedUser[1].bio
};
res.status(200).json({ status: 200, profile: newProfile });
} catch (error) {
res.status(500).json({ error: error.message });
}
}
}

export default new UserController();
4 changes: 4 additions & 0 deletions routes/api/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import User from '../../controllers/user';
import check from '../../middlewares/user';
import VerifyLink from '../../controllers/email/verifyLink';
import validateUser from '../../helpers/validate';
import multer from '../../middlewares/multerConfiguration';


const router = express.Router();
Expand All @@ -25,4 +26,7 @@ router.put('/password', User.resetPassword);

router.post('/login/google', passport.authenticate('googleToken', { session: false }), User.googleLogin);

router.get('/:id/profile', auth, User.getProfile);
router.put('/profile', auth, multer, User.updateProfile);

export default router;
File renamed without changes.
240 changes: 0 additions & 240 deletions test/1-user.js

This file was deleted.

Loading

0 comments on commit d5106f0

Please sign in to comment.