-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
1,274 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
{ | ||
"presets": [ | ||
"@babel/preset-env" | ||
], | ||
"plugins": [ | ||
"@babel/plugin-transform-runtime" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
// import jwtDecode from 'jwt-decode'; | ||
// import jwt from 'jsonwebtoken'; | ||
import model from '../models'; | ||
import Response from '../helpers/Response'; | ||
|
||
const { profiles } = model; | ||
|
||
/** | ||
* @description - This class is all about users | ||
* @param {object} req | ||
* @param {object} res | ||
* @returns {class} Users | ||
*/ | ||
class ProfilesController { | ||
/** | ||
* @description - This method takes care of creating a users profile after registration | ||
* @param {object} req | ||
* @param {object} res | ||
* @returns {object} Created Users Profile | ||
*/ | ||
static async createProfile(req, res) { | ||
try { | ||
const usersProfile = await profiles.create({ | ||
username: req.body.username, | ||
biodata: req.body.biodata, | ||
image: req.body.image, | ||
address: req.body.address, | ||
dateofbirth: req.body.dateofbirth | ||
}); | ||
if (usersProfile) { | ||
Response.created(res, { | ||
message: 'Users profile created succesfully', | ||
profile: usersProfile | ||
}); | ||
} | ||
} catch (error) { | ||
Response.internalServerError(res, { | ||
message: 'users profile not created succesfully, please try again', | ||
error: { | ||
body: [ | ||
`Internal server error => ${error}` | ||
] | ||
} | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* @description - This method takes care of a user viewing his or other people's profile | ||
* @param {object} req | ||
* @param {object} res | ||
* @returns {object} Users Profile | ||
*/ | ||
static async viewProfile(req, res) { | ||
try { | ||
const usersProfile = await profiles.findOne({ | ||
where: { | ||
username: req.params.username | ||
} | ||
}); | ||
if (usersProfile === null) { | ||
Response.notfound(res, { | ||
message: 'Users profile not found', | ||
profile: usersProfile | ||
}); | ||
} else { | ||
Response.success(res, { | ||
message: 'Users profile returned succesfully', | ||
profile: usersProfile | ||
}); | ||
} | ||
} catch (error) { | ||
Response.internalServerError(res, { | ||
message: 'users profile not returned succesfully, please try again', | ||
error: { | ||
body: [ | ||
`Internal server error => ${error}` | ||
] | ||
} | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* @description - This method takes care of updating a user's profile | ||
* @param {object} req | ||
* @param {object} res | ||
* @returns {object} Updated Users Profile | ||
*/ | ||
static async updateProfile(req, res) { | ||
// To be used when users have registered and provided token to login | ||
// const token = req.headers['x-access-token']; | ||
|
||
if (req.body.userToken !== 'token') { | ||
Response.unauthorized(res, { | ||
errors: { | ||
body: [ | ||
'You cannot edit another persons profile' | ||
] | ||
} | ||
}); | ||
} | ||
|
||
try { | ||
const usersProfile = await profiles.findOne({ | ||
where: { | ||
username: req.params.username | ||
} | ||
}); | ||
if (usersProfile) { | ||
try { | ||
const updatedUsersProfile = await usersProfile.update({ | ||
biodata: req.body.biodata || usersProfile.biodata, | ||
image: req.body.image || usersProfile.image, | ||
address: req.body.address || usersProfile.address, | ||
dateofbirth: req.body.dateofbirth || usersProfile.dateofbirth | ||
}); | ||
if (updatedUsersProfile) { | ||
Response.success(res, { | ||
message: 'Users profile updated successfully', | ||
profile: updatedUsersProfile | ||
}); | ||
} | ||
} catch (error) { | ||
Response.internalServerError(res, { | ||
message: 'users profile not returned succesfully, please try again', | ||
error: { | ||
body: [ | ||
`Internal server error => ${error}` | ||
] | ||
} | ||
}); | ||
} | ||
} | ||
} catch (error) { | ||
Response.internalServerError(res, { | ||
message: 'users profile not returned succesfully, please try again', | ||
error: { | ||
body: [ | ||
`Internal server error => ${error}` | ||
] | ||
} | ||
}); | ||
} | ||
} | ||
} | ||
|
||
export default ProfilesController; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import Response from '../helpers/Response'; | ||
|
||
// This function checks for user id being an integer on users profile creation | ||
const checkUsersId = (req, res, next) => { | ||
const { | ||
userId | ||
} = req.params; | ||
|
||
if (!Number.isInteger(+userId)) { | ||
res.status(400).json({ | ||
errors: { | ||
body: [ | ||
'User Id must be an integer' | ||
] | ||
} | ||
}); | ||
} | ||
return 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 === '') { | ||
Response.badrequest(res, { | ||
message: 'User input(s) field must me not be empty', | ||
error: { | ||
body: [ | ||
'Invalid input' | ||
] | ||
} | ||
}); | ||
} | ||
return next(); | ||
}; | ||
|
||
// Export all middlewares here | ||
export { | ||
checkUsersId, validProfileInput | ||
}; |
Oops, something went wrong.