-
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
13 changed files
with
265 additions
and
24 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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import dotenv from 'dotenv'; | ||
|
||
dotenv.config(); | ||
|
||
const config = { | ||
secret: process.env.SECRET, | ||
jwtSecret: process.env.JWT_TOKEN_SECRET, | ||
cloudinary: { | ||
cloud_name: process.env.CLOUD_NAME, | ||
api_key: process.env.API_KEY, | ||
api_secret: process.env.API_SECRET, | ||
} | ||
}; | ||
|
||
export default config; |
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,29 @@ | ||
import { Article } from '../models'; | ||
/** | ||
* validate Request id parameter | ||
* @param {Object} req | ||
* @param {Object} res | ||
* | ||
* @param {Function} next | ||
* | ||
* @return {Object} json | ||
*/ | ||
const checkArticle = (req, res, next) => { | ||
const { id } = req.params; | ||
Article.find({ | ||
where: { | ||
id | ||
} | ||
}) | ||
.then((article) => { | ||
if (article) return next(); | ||
return res.status(404).json({ | ||
errors: { | ||
body: [ | ||
'Ooops! the article cannot be found.' | ||
] | ||
} | ||
}); | ||
}); | ||
}; | ||
export default checkArticle; |
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 Validate from 'validatorjs'; | ||
|
||
/** | ||
* validate Request id parameter | ||
* @param {Object} req | ||
* @param {Object} res | ||
* | ||
* @param {Function} next | ||
* | ||
* @return {Object} json | ||
*/ | ||
const idIsNumber = (req, res, next) => { | ||
const { | ||
id, | ||
} = req.params; | ||
|
||
const data = { | ||
id, | ||
}; | ||
const rules = { | ||
id: ['required', 'integer'], | ||
}; | ||
const validations = new Validate(data, rules, { | ||
'integer.id': 'The parameter :attribute must be an integer.', | ||
}); | ||
|
||
if (validations.passes()) { | ||
return next(); | ||
} | ||
|
||
return res.status(406).json({ | ||
success: 'false', | ||
errors: validations.errors.all(), | ||
}); | ||
}; | ||
export default idIsNumber; |
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 @@ | ||
module.exports = { | ||
up: (queryInterface, Sequelize) => queryInterface.createTable('Likes', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
articleId: { | ||
type: Sequelize.INTEGER, | ||
onDelete: 'CASCADE', | ||
references: { | ||
model: 'Articles', | ||
key: 'id', | ||
as: 'articleId', | ||
}, | ||
}, | ||
userId: { | ||
type: Sequelize.INTEGER, | ||
onDelete: 'CASCADE', | ||
references: { | ||
model: 'Users', | ||
key: 'id', | ||
as: 'userId', | ||
}, | ||
}, | ||
}), | ||
down: (queryInterface/* , Sequelize */) => { | ||
queryInterface.dropTable('Likes'); | ||
} | ||
}; |
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,16 @@ | ||
|
||
module.exports = (sequelize) => { | ||
const Like = sequelize.define('Like', {}, {}); | ||
|
||
Like.associate = (models) => { | ||
Like.belongsTo(models.Article, { | ||
foreignKey: 'articleId', | ||
as: 'likedArticle', | ||
}); | ||
Like.belongsTo(models.User, { | ||
foreignKey: 'userId', | ||
as: 'user', | ||
}); | ||
}; | ||
return Like; | ||
}; |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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.