-
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
26 changed files
with
1,144 additions
and
43 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,3 +19,6 @@ src/api/migrations | |
|
||
# Seeders | ||
src/api/seeders | ||
|
||
# Models | ||
src/api/models |
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,51 @@ | ||
import models from '../models/index'; | ||
import status from '../../helpers/constants/status.codes'; | ||
import sendError from '../../helpers/error.sender'; | ||
import errorMessages from '../../helpers/constants/error.messages'; | ||
// import errorMessages from '../../helpers/constants/error.messages'; | ||
|
||
const { Article } = models; | ||
|
||
/** | ||
* containing all controllers of the Articles tags | ||
* | ||
* @export | ||
* @class articles | ||
*/ | ||
export default class articleTags { | ||
/** | ||
* A controller to add a tag to an article | ||
* | ||
* @param {Object} req - the request object | ||
* @param {Object} res - the result object | ||
* @returns {Object} res | ||
*/ | ||
static async createArticleTag(req, res) { | ||
// Initialising variables | ||
const result = {}; | ||
const { slug } = req.params; | ||
const { tag } = req.body; | ||
const { tagList } = req.Existing; | ||
|
||
if (tagList.includes(tag)) { | ||
return sendError(status.RESET_CONTENT, res, 'tagList', errorMessages.existingTag); | ||
} | ||
// Updating the tags | ||
tagList.push(tag); | ||
|
||
// Updating the tags | ||
await Article.update({ | ||
tagList | ||
}, | ||
{ | ||
where: { | ||
slug | ||
} | ||
}); | ||
|
||
// sending the result back | ||
result.status = status.OK; | ||
result.message = 'Tag added successfully'; | ||
res.status(status.OK).json(result); | ||
} | ||
} |
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,51 @@ | ||
export default { | ||
up: (queryInterface, Sequelize) => queryInterface.createTable('Articles', { | ||
id: { | ||
allowNull: false, | ||
unique: true, | ||
primaryKey: true, | ||
type: Sequelize.UUID, | ||
defaultValue: Sequelize.UUIDV4 | ||
}, | ||
title: { | ||
type: Sequelize.STRING, | ||
allowNull: false | ||
}, | ||
slug: { | ||
type: Sequelize.STRING, | ||
allowNull: false, | ||
unique: true | ||
}, | ||
description: { | ||
type: Sequelize.STRING, | ||
allowNull: false | ||
}, | ||
body: { | ||
type: Sequelize.TEXT, | ||
allowNull: false | ||
}, | ||
tagList: { | ||
type: Sequelize.ARRAY(Sequelize.TEXT), | ||
allowNull: false | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
userId: { | ||
type: Sequelize.INTEGER, | ||
references: { | ||
model: 'Users', | ||
key: 'id' | ||
}, | ||
onDelete: 'CASCADE', | ||
onUpdate: 'CASCADE', | ||
allowNull: false | ||
} | ||
}), | ||
down: (queryInterface, Sequelize) => 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
export default { | ||
up: (queryInterface, Sequelize) => queryInterface | ||
.createTable('Ratings', { | ||
userId: { | ||
type: Sequelize.INTEGER, | ||
references: { | ||
model: 'Users', | ||
key: 'id' | ||
}, | ||
onDelete: 'CASCADE', | ||
onUpdate: 'CASCADE', | ||
allowNull: false, | ||
unique: false | ||
}, | ||
articleSlug: { | ||
type: Sequelize.STRING, | ||
references: { | ||
model: 'Articles', | ||
key: 'slug' | ||
}, | ||
onDelete: 'CASCADE', | ||
onUpdate: 'CASCADE', | ||
allowNull: false, | ||
unique: false | ||
}, | ||
rating: { | ||
type: Sequelize.INTEGER | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
} | ||
}) | ||
.then(() => queryInterface.addConstraint('Ratings', ['userId', 'articleSlug'], { | ||
type: 'primary key', | ||
name: 'userRating' | ||
})), | ||
down: (queryInterface, Sequelize) => queryInterface.dropTable('Ratings') | ||
}; |
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,52 @@ | ||
const Article = (sequelize, DataTypes) => { | ||
const article = sequelize.define('Article', | ||
{ | ||
id: { | ||
allowNull: false, | ||
unique: true, | ||
primaryKey: true, | ||
type: DataTypes.UUID, | ||
defaultValue: DataTypes.UUIDV4 | ||
}, | ||
title: { | ||
type: DataTypes.STRING, | ||
allowNull: false | ||
}, | ||
description: { | ||
type: DataTypes.STRING, | ||
allowNull: false | ||
}, | ||
slug: { | ||
type: DataTypes.STRING, | ||
allowNull: false, | ||
unique: true | ||
}, | ||
body: { | ||
type: DataTypes.TEXT, | ||
allowNull: false | ||
}, | ||
tagList: { | ||
type: DataTypes.ARRAY(DataTypes.TEXT), | ||
allowNull: false | ||
}, | ||
userId: { | ||
type: DataTypes.INTEGER, | ||
references: { | ||
model: 'Users', | ||
key: 'id' | ||
}, | ||
onDelete: 'CASCADE', | ||
onUpdate: 'CASCADE', | ||
allowNull: false | ||
} | ||
}, | ||
{}); | ||
|
||
Article.associate = (models) => { | ||
Article.belongsTo(models.User, { foreignKey: 'userId' }); | ||
Article.hasOne(models.Rating, { foreignKey: 'articleSlug' }); | ||
}; | ||
return article; | ||
}; | ||
|
||
export default Article; |
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,38 @@ | ||
export default (sequelize, DataTypes) => { | ||
const Rating = sequelize.define('Ratings', | ||
{ | ||
userId: { | ||
type: DataTypes.INTEGER, | ||
references: { | ||
model: 'Users', | ||
key: 'id' | ||
}, | ||
onDelete: 'CASCADE', | ||
onUpdate: 'CASCADE', | ||
allowNull: false | ||
}, | ||
articleSlug: { | ||
type: DataTypes.STRING, | ||
references: { | ||
model: 'Articles', | ||
key: 'slug' | ||
}, | ||
onDelete: 'CASCADE', | ||
onUpdate: 'CASCADE', | ||
allowNull: false | ||
}, | ||
rating: { | ||
type: DataTypes.INTEGER | ||
} | ||
}, | ||
{ | ||
freezeTableName: true | ||
}); | ||
|
||
Rating.removeAttribute('id'); | ||
Rating.associate = (models) => { | ||
Rating.belongsTo(models.User, { foreignKey: 'userId' }); | ||
Rating.belongsTo(models.Article, { foreignKey: 'articleSlug' }); | ||
}; | ||
return Rating; | ||
}; |
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,19 @@ | ||
import express from 'express'; | ||
import articleTagController from '../controllers/articleTagController'; | ||
import authorization from '../../middlewares/authorization'; | ||
import validdateInputs from '../../middlewares/validations/body.inputs'; | ||
import checkArticle from '../../middlewares/getOneArticle'; | ||
import ownershipValidator from '../../middlewares/checkArticleOwnership'; | ||
import verifyBody from '../../middlewares/validations/body.verifier'; | ||
|
||
const router = express(); | ||
|
||
router.patch('/:slug/tags', | ||
authorization, | ||
verifyBody, | ||
validdateInputs(true, 'addArticleTag', ['tag']), | ||
checkArticle.getArticle, | ||
ownershipValidator.checkOwner, | ||
articleTagController.createArticleTag); | ||
|
||
export default router; |
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,10 +1,14 @@ | ||
import express from 'express'; | ||
import authRouter from './authRouter'; | ||
import resetRouter from './password.reset'; | ||
import articleRouter from './articleRouter'; | ||
import testsRouter from '../../helpers/factory/routes/body.inputs.test'; | ||
|
||
const router = express(); | ||
|
||
router.use('/users', authRouter); | ||
router.use('/password-reset', resetRouter); | ||
router.use('/articles', articleRouter); | ||
router.use('/tests', testsRouter); | ||
|
||
export default router; |
Oops, something went wrong.