-
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.
- Loading branch information
Showing
18 changed files
with
983 additions
and
309 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(409).json({ success: false, error }); | ||
} | ||
} | ||
} |
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,22 @@ | ||
import jwt from 'jsonwebtoken'; | ||
|
||
export default (req, res, next) => { | ||
const { authorization } = req.headers; | ||
if (!authorization) { | ||
return res.status(401).json({ | ||
success: false, | ||
message: 'Unauthorized! You are required to be logged in to perform this operation.', | ||
}); | ||
} | ||
const token = authorization.split(' ')[1]; | ||
jwt.verify(token, process.env.JWT_SECRET, (err, response) => { | ||
if (err) { | ||
return res.status(401).json({ | ||
success: false, | ||
message: 'Your session has expired, please login again to continue', | ||
}); | ||
} | ||
req.user = response; | ||
return next(); | ||
}); | ||
}; |
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,12 @@ | ||
import { User } from '../models'; | ||
|
||
export default (req, res, next) => User.findById(req.user.id, { where: { id: req.user.id } }) | ||
.then(({ dataValues: { verified } }) => { | ||
if (!verified) { | ||
return res.status(403).json({ | ||
success: false, | ||
errors: 'User has not been verified' | ||
}); | ||
} | ||
return next(); | ||
}); |
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.