-
Notifications
You must be signed in to change notification settings - Fork 8
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
52 changed files
with
532 additions
and
163 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,4 @@ | ||
|
||
build/ | ||
server/migrations | ||
server/seeders | ||
server/test | ||
server/test/* |
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 |
---|---|---|
|
@@ -49,3 +49,8 @@ package-lock.json | |
|
||
# personal files | ||
swagger.1.yaml | ||
|
||
#code formatter | ||
.prettierrc | ||
|
||
.vscode |
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,60 @@ | ||
import uuidv4 from 'uuid/v4'; | ||
import db from '../models'; | ||
import response from '../helpers/response'; | ||
import TokenManager from '../helpers/TokenManager'; | ||
|
||
const id = uuidv4(); | ||
|
||
const { Article, Comment } = db; | ||
|
||
/** | ||
* @class UserController | ||
*/ | ||
class CommentController { | ||
/** | ||
* | ||
* @description Allow user to comment on an article | ||
* @static | ||
* @param {*} req Express Request object | ||
* @param {*} res Express Response object | ||
* @returns {object} Json response | ||
* @memberof CommentController | ||
*/ | ||
static async createComment(req, res) { | ||
try { | ||
const token = req.headers.authorization.split(' ')[1]; | ||
const { userId } = TokenManager.verify(token); | ||
const { articleId, content } = req.body; | ||
if (!content) { | ||
return response(res, 400, 'failure', 'Enter a comment', null, null); | ||
} | ||
await Article.findAndCountAll({ | ||
where: { | ||
id: articleId | ||
} | ||
}); | ||
await Comment.create({ | ||
id, | ||
content, | ||
userId, | ||
articleId | ||
}); | ||
const getComment = await Comment.findAll({ | ||
where: { | ||
articleId | ||
} | ||
}); | ||
const allComment = []; | ||
getComment.map(comment => allComment.push(comment.dataValues)); | ||
if (getComment) { | ||
return response(res, 201, 'success', 'Comment created', null, allComment); | ||
} | ||
} catch (error) { | ||
if (error.name === 'SequelizeDatabaseError') { | ||
return response(res, 500, 'failure', 'Article not found', null, null); | ||
} | ||
} | ||
} | ||
} | ||
|
||
export default CommentController; |
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
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,23 @@ | ||
/** | ||
* | ||
* @description Method to send response in a generic format. | ||
* @param {*} res Express Response object | ||
* @param {number} code HTTP response status code | ||
* @param {string} status 'success' || 'failure' | ||
* @param {string} message Message to user | ||
* @param {object} error (optional) Error object | ||
* @param {object} payload (optional) Payload data to return with the response | ||
* @returns {object} Json response | ||
*/ | ||
|
||
export default (res, code, status, message, error, payload) => { | ||
res.status(code).json({ | ||
status, | ||
data: { | ||
statusCode: code, | ||
message, | ||
error, | ||
payload | ||
} | ||
}); | ||
}; |
Empty file.
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,61 @@ | ||
import TokenManager from '../helpers/TokenManager'; | ||
/** | ||
* @class AuthMiddleware | ||
* @description class contains function for implementing Authentication middleware | ||
*/ | ||
class AuthMiddleware { | ||
/** | ||
* @static | ||
* @description a middleware function checking if a user is authenticated | ||
* @param {object} req HTTP request object | ||
* @param {object} res HTTP response object | ||
* @param {function} next next middleware function | ||
* @returns {object} returns error message if user is not authenticated | ||
*/ | ||
static checkIfUserIsAuthenticated(req, res, next) { | ||
try { | ||
const { authorization } = req.headers; | ||
if (!authorization) { | ||
return res.status(401).send({ | ||
success: false, | ||
status: 401, | ||
error: { | ||
message: 'You are not logged in, Token is needed!' | ||
} | ||
}); | ||
} | ||
|
||
const token = authorization.split(' ')[1]; | ||
const decoded = TokenManager.verify(token); | ||
|
||
if (decoded) { | ||
req.user = decoded; | ||
return next(); | ||
} | ||
} catch (error) { | ||
const { name } = error; | ||
if (name === 'TokenExpiredError' || name === 'JsonWebTokenError') { | ||
return res.status(401).send({ | ||
success: 'failure', | ||
data: { | ||
error: { | ||
message: 'Token is invalid, You need to log in again' | ||
} | ||
} | ||
}); | ||
} | ||
|
||
return res.status(500).send({ | ||
success: 'failure', | ||
data: { | ||
error: { | ||
message: 'An error occured on the server', | ||
error | ||
} | ||
} | ||
}); | ||
} | ||
} | ||
} | ||
|
||
export default AuthMiddleware; |
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
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
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
Oops, something went wrong.