Skip to content

Commit

Permalink
[Feature #162171020]: refactor profile codebase
Browse files Browse the repository at this point in the history
- Add helper function Response
- Update ProfileController
- run test, test passes, commit, push to Github
  • Loading branch information
azupatrick0 committed Dec 3, 2018
1 parent 07a7ac5 commit 12f77ac
Show file tree
Hide file tree
Showing 6 changed files with 216 additions and 142 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// import jwtDecode from 'jwt-decode';
// import jwt from 'jsonwebtoken';
import model from '../models';
import { profileStatusResponseOk, profileStatusResponseError } from '../middlewares/UsersMiddleware';
import Response from '../helpers/Response';

const { profiles } = model;

Expand All @@ -11,7 +11,7 @@ const { profiles } = model;
* @param {object} res
* @returns {class} Users
*/
class UsersController {
class ProfilesController {
/**
* @description - This method takes care of creating a users profile after registration
* @param {object} req
Expand All @@ -28,13 +28,19 @@ class UsersController {
dateofbirth: req.body.dateofbirth
});
if (usersProfile) {
profileStatusResponseOk(res, 201, 'Users profile created succesfully', usersProfile);
Response.created(res, {
message: 'Users profile created succesfully',
profile: usersProfile
});
}
} catch (error) {
profileStatusResponseError(res, 500, 'users profile not created succesfully, please try again', {
body: [
`Internal server error => ${error}`
]
Response.internalServerError(res, {
message: 'users profile not created succesfully, please try again',
error: {
body: [
`Internal server error => ${error}`
]
}
});
}
}
Expand All @@ -53,15 +59,24 @@ class UsersController {
}
});
if (usersProfile === null) {
profileStatusResponseOk(res, 404, 'Users profile not found', usersProfile);
Response.notfound(res, {
message: 'Users profile not found',
profile: usersProfile
});
} else {
profileStatusResponseOk(res, 200, 'Users profile returned succesfully', usersProfile);
Response.success(res, {
message: 'Users profile returned succesfully',
profile: usersProfile
});
}
} catch (error) {
profileStatusResponseError(res, 500, 'users profile not returned succesfully, please try again', {
body: [
`Internal server error => ${error}`
]
Response.internalServerError(res, {
message: 'users profile not returned succesfully, please try again',
error: {
body: [
`Internal server error => ${error}`
]
}
});
}
}
Expand All @@ -77,10 +92,12 @@ class UsersController {
// const token = req.headers['x-access-token'];

if (req.body.userToken !== 'token') {
profileStatusResponseError(res, 401, '', {
body: [
'You cannot edit another persons profile'
]
Response.unauthorized(res, {
errors: {
body: [
'You cannot edit another persons profile'
]
}
});
}

Expand All @@ -99,24 +116,33 @@ class UsersController {
dateofbirth: req.body.dateofbirth || usersProfile.dateofbirth
});
if (updatedUsersProfile) {
profileStatusResponseOk(res, 200, 'Users profile updated succesfully', updatedUsersProfile);
Response.success(res, {
message: 'Users profile updated successfully',
profile: updatedUsersProfile
});
}
} catch (error) {
profileStatusResponseError(res, 500, 'users profile not returned succesfully, please try again', {
body: [
`Internal server error => ${error}`
]
Response.internalServerError(res, {
message: 'users profile not returned succesfully, please try again',
error: {
body: [
`Internal server error => ${error}`
]
}
});
}
}
} catch (error) {
profileStatusResponseError(res, 500, 'users profile not returned succesfully, please try again', {
body: [
`Internal server error => ${error}`
]
Response.internalServerError(res, {
message: 'users profile not returned succesfully, please try again',
error: {
body: [
`Internal server error => ${error}`
]
}
});
}
}
}

export default UsersController;
export default ProfilesController;
77 changes: 77 additions & 0 deletions helpers/Response.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* @description - This class is all about server response
* @returns {class} Response
*/
class Response {
/**
* @description - success response
* @param {object} res
* @param {object} data
* @returns {object} Success
*/
static success(res, data) {
return res.status(200).json(data);
}

/**
* @description - Not found response
* @param {object} res
* @param {object} data
* @returns {object} Not found
*/
static notfound(res, data) {
return res.status(404).json(data);
}

/**
* @description - Internal server error response
* @param {object} res
* @param {object} data
* @returns {object} Error
*/
static internalServerError(res, data) {
return res.status(500).json(data);
}

/**
* @description - bad request
* @param {object} res
* @param {object} data
* @returns {object} Error
*/
static badrequest(res, data) {
return res.status(400).json(data);
}

/**
* @description - created response
* @param {object} res
* @param {object} data
* @returns {object} Created
*/
static created(res, data) {
return res.status(201).json(data);
}

/**
* @description - Unauthorized credentials
* @param {object} res
* @param {object} data
* @returns {object} Unauthorized
*/
static unauthorized(res, data) {
return res.status(401).json(data);
}

/**
* @description - forbidden credentials
* @param {object} res
* @param {object} data
* @returns {object} forbidden
*/
static forbidden(res, data) {
return res.status(403).json(data);
}
}

export default Response;
Original file line number Diff line number Diff line change
@@ -1,14 +1,4 @@
// This function return error on users profile
const profileStatusResponseError = (res, statusCode, message, obj) => res.status(statusCode).json({
message,
errors: obj
});

// This function return ok on users profile
const profileStatusResponseOk = (res, statusCode, message, obj) => res.status(statusCode).json({
message,
profile: obj
});
import Response from '../helpers/Response';

// This function checks for user id being an integer on users profile creation
const checkUsersId = (req, res, next) => {
Expand All @@ -31,12 +21,19 @@ const checkUsersId = (req, res, next) => {
// This fucntion checks for a user entering valid inputs users profile update
const validProfileInput = (req, res, next) => {
if (req.body.username === '' || req.body.biodata === '' || req.body.image === '' || req.body.address === '' || req.body.dateofbirth === '') {
profileStatusResponseError(res, 400, 'User input(s) field must me not be empty', 'Invalid input');
Response.badrequest(res, {
message: 'User input(s) field must me not be empty',
error: {
body: [
'Invalid input'
]
}
});
}
return next();
};

// Export all middlewares here
export {
profileStatusResponseOk, profileStatusResponseError, checkUsersId, validProfileInput
checkUsersId, validProfileInput
};
8 changes: 4 additions & 4 deletions models/profiles.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export default (sequelize, DataTypes) => {
const profiles = sequelize.define('profiles', {
const Profiles = sequelize.define('profiles', {
username: {
type: DataTypes.STRING,
allowNull: false,
Expand All @@ -23,11 +23,11 @@ export default (sequelize, DataTypes) => {
},
}, {});

/* profiles.associate = (models) => {
profiles.belongsTo(models.Users, {
/* Profiles.associate = (models) => {
Profiles.belongsTo(models.Users, {
foreignKey: 'userId',
onDelete: 'CASCADE',
});
}; */
return profiles;
return Profiles;
};
4 changes: 2 additions & 2 deletions routes/Users.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import express from 'express';
import Users from '../controllers/UsersController';
import { checkUsersId, validProfileInput } from '../middlewares/UsersMiddleware';
import Users from '../controllers/ProfilesController';
import { checkUsersId, validProfileInput } from '../middlewares/ProfilesMiddleware';

const router = express.Router();

Expand Down
Loading

0 comments on commit 12f77ac

Please sign in to comment.