-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(create_article): add functionality to create articles
- setup middlewares to validate article details and to authenticate user token - create database models for articles - setup routes for the endpoint to post an article - write test for testing the functionality to create an article - add documentation for this endpoint [Delievers #164139686]
- Loading branch information
Showing
19 changed files
with
1,058 additions
and
310 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 |
---|---|---|
|
@@ -43,3 +43,6 @@ mochawesome-report | |
# Ignore yarn generated files | ||
yarn.lock | ||
dist/ | ||
|
||
#coveralls code coverage reports | ||
.nyc_output/ |
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,30 @@ | ||
import { Article } from '../models'; | ||
|
||
/** | ||
* @class ArticleController | ||
* @override | ||
* @export | ||
*/ | ||
export default class ArticleController { | ||
/** | ||
* @description - Create a new article | ||
* @static | ||
* @param {Object} req - the request object | ||
* @param {Object} res - the response object | ||
* @memberof ArticleController | ||
* @returns {Object} class instance | ||
*/ | ||
static async createArticle(req, res) { | ||
const { title, description, body } = req.body; | ||
try { | ||
const result = await Article.create({ title, description, body }); | ||
return res.status(201).json({ | ||
success: true, | ||
message: 'New article created successfully', | ||
article: result | ||
}); | ||
} catch (error) { | ||
return res.status(500).json({ success: false, error: [error.message] }); | ||
} | ||
} | ||
} |
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,3 @@ | ||
import slug from 'slug'; | ||
|
||
export default validString => slug(validString, { lower: true }); |
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,36 @@ | ||
import jwt from 'jsonwebtoken'; | ||
|
||
const { JWT_SECRET } = process.env; | ||
/** | ||
* Authentication class | ||
*/ | ||
class Auth { | ||
/** | ||
* @description Middleware function to verify if user has a valid token | ||
* @param {object} req http request object | ||
* @param {object} res http response object | ||
* @param {Function} next next middleware function | ||
* @returns {undefined} | ||
*/ | ||
static verifyUser(req, res, next) { | ||
const token = req.headers['x-access-token'] || req.query.token || req.headers.authorization; | ||
if (!token) { | ||
return res.status(401).json({ | ||
success: false, | ||
message: 'Unauthorized! You are required to be logged in to perform this operation.', | ||
}); | ||
} | ||
jwt.verify(token, JWT_SECRET, (err, decoded) => { | ||
if (err) { | ||
return res.status(401).json({ | ||
success: false, | ||
message: 'Your session has expired, please login again to continue', | ||
}); | ||
} | ||
req.user = decoded; | ||
return next(); | ||
}); | ||
} | ||
} | ||
|
||
export default Auth; |
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 { User } from '../models'; | ||
|
||
export default (req, res, next) => { | ||
const { | ||
body, | ||
user, | ||
} = req; | ||
const options = {}; | ||
if (user) { | ||
options.fieldName = 'id'; | ||
options.fieldValue = user.id; | ||
} else if (body) { | ||
options.fieldName = 'email'; | ||
options.fieldValue = body.email; | ||
} | ||
|
||
const { fieldName, fieldValue } = options; | ||
User.findOne({ where: { [fieldName]: fieldValue } }) | ||
.then((foundUser) => { | ||
if (foundUser) { | ||
const { dataValues: { verified } } = foundUser; | ||
if (!verified) { | ||
return res.status(403).json({ | ||
success: false, | ||
errors: [ | ||
'User has not been verified.' | ||
] | ||
}); | ||
} | ||
return next(); | ||
} | ||
return res.status(404).json({ | ||
success: false, | ||
errors: [ | ||
'User not found.' | ||
] | ||
}); | ||
}); | ||
}; |
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,54 @@ | ||
|
||
module.exports = { | ||
up: (queryInterface, Sequelize) => queryInterface.createTable('Articles', { | ||
id: { | ||
unique: true, | ||
allowNull: false, | ||
primaryKey: true, | ||
type: Sequelize.UUID, | ||
defaultValue: Sequelize.UUIDV4 | ||
}, | ||
title: { | ||
type: Sequelize.STRING, | ||
allowNull: { | ||
args: true, | ||
msg: 'Article should have a title' | ||
} | ||
}, | ||
slug: { | ||
type: Sequelize.STRING, | ||
allowNull: false, | ||
unique: { | ||
args: true, | ||
msg: 'Article should have a unique slug' | ||
} | ||
}, | ||
description: { | ||
type: Sequelize.TEXT, | ||
allowNull: { | ||
args: true, | ||
msg: 'Article should have a description' | ||
} | ||
}, | ||
body: { | ||
type: Sequelize.TEXT, | ||
allowNull: { | ||
args: true, | ||
msg: 'Article should have a body' | ||
} | ||
}, | ||
taglist: { | ||
type: Sequelize.ARRAY(Sequelize.STRING), | ||
defaultValue: [] | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
} | ||
}), | ||
down: queryInterface => queryInterface.dropTable('Articles') | ||
}; |
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,65 @@ | ||
import shortId from 'shortid'; | ||
import generateSlug from '../helpers/slug'; | ||
|
||
module.exports = (sequelize, DataTypes) => { | ||
const Article = sequelize.define('Article', { | ||
id: { | ||
unique: true, | ||
allowNull: false, | ||
primaryKey: true, | ||
type: DataTypes.UUID, | ||
defaultValue: DataTypes.UUIDV4 | ||
}, | ||
title: { | ||
type: DataTypes.STRING, | ||
allowNull: { | ||
args: true, | ||
msg: 'Article should have a title' | ||
} | ||
}, | ||
slug: { | ||
type: DataTypes.STRING, | ||
allowNull: false, | ||
unique: { | ||
args: true, | ||
msg: 'Article should have a unique slug' | ||
} | ||
}, | ||
description: { | ||
type: DataTypes.TEXT, | ||
allowNull: { | ||
args: true, | ||
msg: 'Article should have a description' | ||
} | ||
}, | ||
body: { | ||
type: DataTypes.TEXT, | ||
allowNull: { | ||
args: true, | ||
msg: 'Article should have a body' | ||
} | ||
}, | ||
taglist: { | ||
type: DataTypes.ARRAY(DataTypes.STRING), | ||
defaultValue: [] | ||
} | ||
}, { | ||
classMethods: { | ||
associate(models) { | ||
Article.belongsTo(models.User, { | ||
foreignKey: 'userId', | ||
onDelete: 'CASCADE', | ||
}); | ||
} | ||
} | ||
}); | ||
|
||
Article.hook('beforeValidate', (article) => { | ||
article.slug = (`${generateSlug(article.title)}-${shortId.generate()}`).toLowerCase(); | ||
article.title = article.title.trim(); | ||
article.description = article.description.trim(); | ||
article.body = article.body.trim(); | ||
}); | ||
|
||
return Article; | ||
}; |
Oops, something went wrong.