Skip to content

Commit

Permalink
[Ft 166240825] update users profile
Browse files Browse the repository at this point in the history
  • Loading branch information
alainburindi committed Jul 2, 2019
1 parent c532aa0 commit 86a3fae
Show file tree
Hide file tree
Showing 19 changed files with 953 additions and 46 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"expect": "^24.8.0",
"express": "^4.16.4",
"express-session": "^1.16.2",
"joi": "^14.3.1",
"jsonwebtoken": "^8.5.1",
"lodash": "^4.17.11",
"morgan": "^1.9.1",
Expand Down
1 change: 0 additions & 1 deletion src/api/controllers/articleController.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ class ArticleController {
/**
* allow an author to update his/her article
*
* @author Frank Mutabazi
* @static
* @param {object} req the request
* @param {object} res the response to be sent
Expand Down
79 changes: 79 additions & 0 deletions src/api/controllers/profileController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import cloudinary from 'cloudinary';

import models from '../models/index';
import sendProfile from '../../helpers/results/send.profile';
import status from '../../helpers/constants/status.codes';
import errorMessages from '../../helpers/constants/error.messages';

const { Article, User } = models;

/**
* containing all user's
* profile model controller(update, getUserProfile, read)
* @export
* @class ProfileController
*/
export default class ProfileController {
/**
*
* update current user's profile
* @static
* @param {*} req The request Object
* @param {*} res The response Object
* @memberof ProfileController
* @return {Object} The response Object
*/
static async update(req, res) {
let profileImage = 'noimage.jpg';
const {
country, firstName, lastName, address, gender, phoneNumber, bio
} = req.body;
// upload the image to cloudinary
if (req.file) {
const image = await cloudinary.v2.uploader.upload(req.file.path);
profileImage = image.secure_url;
}
const updatedProfile = await User.update({
country, firstName, lastName, bio, address, gender, profileImage, phoneNumber
}, {
where: {
id: req.user.id
}
});
return sendProfile(res, status.OK, 'updated correctly', updatedProfile);
}

/**
*
* get another user's profile
* @static
* @param {*} req The request Object
* @param {*} res The response Object
* @memberof ProfileController
* @return {Object} The response Object
*/
static async read(req, res) {
const { username } = req.params;
const { limit, offset } = req.query;
// check if the user exists and send his profile
try {
const defaultPaginationLimit = 10;
const paginationLimit = limit || defaultPaginationLimit;
const user = await User.findOne({
where: {
username
},
attributes: ['username', 'email', 'profileImage', 'bio', 'firstName', 'lastName', 'address', 'phoneNumber', 'gender', 'country', 'facebook', 'twitter'],
include: [{
model: Article,
offset,
limit: paginationLimit,
}],
});
if (user) return sendProfile(res, status.OK, undefined, user);
return sendProfile(res, status.NOT_FOUND, errorMessages.noUser);
} catch (err) {
return res.status(status.SERVER_ERROR).json(err);
}
}
}
4 changes: 2 additions & 2 deletions src/api/models/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const Article = (sequelize, DataTypes) => {
allowNull: false,
unique: true,
primaryKey: true,
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4
type: DataTypes.INTEGER,
defaultValue: DataTypes.INTEGER
},
title: {
type: DataTypes.STRING,
Expand Down
6 changes: 5 additions & 1 deletion src/api/models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ const models = {
User: sequelize.import('./user'),
Article: sequelize.import('./article'),
};

Object.keys(models).forEach((key) => {
if ('associate' in models[key]) {
models[key].associate(models);
}
});
export { sequelize };

export default models;
3 changes: 3 additions & 0 deletions src/api/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ export default (sequelize, DataTypes) => {
}
},
{});
User.associate = (models) => {
User.hasMany(models.Article, { foreignKey: 'userId' });

};
User.findByEmail = (email) => {
const queryResult = User.findOne({
where: { email }
Expand Down
6 changes: 5 additions & 1 deletion src/api/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ import express from 'express';
import authRouter from './authRouter';
import resetRouter from './password.reset';
import articleRouter from './articleRouter';
import profileRouter from './profileRouter';
import authenticate from '../../middlewares/authenticate';


const router = express();

router.use('/users', authRouter);
router.use('/password-reset', resetRouter);
router.use('/articles', articleRouter);
router.use('/users', authRouter);
router.use('/profiles', authenticate, profileRouter);

export default router;
14 changes: 14 additions & 0 deletions src/api/routes/profileRouter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Router } from 'express';
import profileController from '../controllers/profileController';
import upload from '../../middlewares/upload';
import validateInputs from '../../middlewares/validations/body.inputs';


const profileRouter = new Router();
const fields = ['country', 'lastName', 'firstName', 'address', 'gender', 'phoneNumber', 'bio', 'profileImage', 'facebook', 'twitter'];

profileRouter.patch('/', upload.single('profileImage'), validateInputs(true, 'profile', fields),
profileController.update);
profileRouter.get('/:username', profileController.read);

export default profileRouter;
Loading

0 comments on commit 86a3fae

Please sign in to comment.