Skip to content

Commit

Permalink
new branch
Browse files Browse the repository at this point in the history
  • Loading branch information
kleva-j committed Mar 12, 2019
1 parent e6d90d1 commit eaee588
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 16 deletions.
40 changes: 27 additions & 13 deletions controllers/articles.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,37 @@ export default class ArticleController {
const {
title, description, body, slug
} = req.body;
const { id } = req.user;
const taglist = req.body.taglist ? req.body.taglist.split(',') : [];
const { user: { id } } = req;
try {
const userExist = await User.findOne({ where: { id } });
if (userExist) {
const result = await Article.create({
title, description, body, slug, images, taglist
});
return res.status(201).json({
success: true,
message: 'New article created successfully',
article: result,
});
}
return res.status(404).json({ success: false, message: 'User not found' });
const result = await Article.create({
title, userId: id, description, body, slug, images, taglist,
});
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] });
}
}

/**
* @description - Get an Article
* @static
* @param {Object} req - the request object
* @param {Object} res - the response object
* @memberof ArticleController
* @returns {Object} class instance
*/
static async getAllArticles(req, res) {
const { id } = req.user;
const articles = await Article.findAll({
include: [{
model: User
}],
});
console.log(articles);
}
}
18 changes: 18 additions & 0 deletions migrations/20190312172128-add-userId.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

module.exports = {
up: (queryInterface, Sequelize) => queryInterface.addColumn(
'Articles',
'userId',
{
type: Sequelize.INTEGER,
allowNull: true
}
),

down(queryInterface) {
return queryInterface.removeColumn(
'Articles',
'userId'
);
},
};
5 changes: 4 additions & 1 deletion models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ module.exports = (sequelize, DataTypes) => {
const User = sequelize.define('User', userSchema, {
classMethods: {
associate(models) {
User.hasMany(models.Article);
User.hasMany(models.Article, {
foreignKey: 'id',
onDelete: 'CASCADE'
});
}
}
});
Expand Down
5 changes: 4 additions & 1 deletion models/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ module.exports = (sequelize, DataTypes) => {
taglist: {
type: DataTypes.ARRAY(DataTypes.STRING),
defaultValue: []
}
},
userId: {
type: DataTypes.INTEGER,
},
}, {
classMethods: {
associate(models) {
Expand Down
3 changes: 2 additions & 1 deletion routes/v1.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
validateArticle,
} from '../middleware/validation';

const { createArticle } = ArticleController;
const { createArticle, getAllArticles } = ArticleController;

const apiRoutes = express.Router();

Expand All @@ -29,6 +29,7 @@ apiRoutes.route('/verify/:verificationId')
.get(UserController.verifyAccount);

apiRoutes.route('/articles')
.get(Auth.verifyUser, isUserVerified, getAllArticles)
.post(Auth.verifyUser, isUserVerified,
addImages,
validateArticle,
Expand Down

0 comments on commit eaee588

Please sign in to comment.