-
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.
- Write unit test - add route to like articles - add user can like articles controller [Delivers #159206057]
- Loading branch information
1 parent
6b9dd41
commit 56bfd5c
Showing
13 changed files
with
429 additions
and
36 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
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
Oops, something went wrong.