Skip to content

Commit

Permalink
167942408-(article)-get-related-articles
Browse files Browse the repository at this point in the history
 - add get related articles route
 - add get related articles controller
 - add validation
[Delivers #167942408]
  • Loading branch information
vincentayorinde committed Aug 19, 2019
1 parent 14c5179 commit b8e7172
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 2 deletions.
40 changes: 40 additions & 0 deletions controllers/articles/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import Sequelize from 'sequelize';
import {
uploadImage, deleteImage, findRatedArticle, storeRating
} from '../../utils';
import db from '../../db/models';
import Notification from '../../utils/notifications';

const { Op } = Sequelize;

export default {
getArticles: async (req, res) => {
try {
Expand Down Expand Up @@ -679,5 +682,42 @@ export default {
error: e.message
});
}
},

getRelatedArticles: async (req, res) => {
const { slug, category } = req.params;
try {
const foundCategory = await db.Category.findOne(
{ where: { name: category } }
);
if (!foundCategory) {
return res.status(404).json({
error: 'Category not found'
});
}
const foundArticles = await db.Article.findAndCountAll(
{
limit: 2,
where: {
categoryId: foundCategory.id,
slug: {
[Op.ne]: slug
}
},
attributes: ['title', 'slug', 'image', 'createdAt', 'categoryId', 'description'],
order: [
['createdAt', 'DESC']
]
}
);
return res.status(200).json({
articles: foundArticles
});
} catch (e) {
console.log('the error', e);
return res.status(500).json({
error: 'somthing went wrong'
});
}
}
};
3 changes: 2 additions & 1 deletion db/models/ArticleTag.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ module.exports = (sequelize) => {
}, {});
ArticleTag.associate = (models) => {
ArticleTag.belongsTo(models.Article, {
foreignKey: 'articleId'
foreignKey: 'articleId',
as: 'article'
});

ArticleTag.belongsTo(models.Tag, {
Expand Down
7 changes: 7 additions & 0 deletions routes/v1/articles.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,11 @@ router.get(
Article.getBookmarkedArticles
);

router.get(
'/:slug/:category/related',
Validation.category,
Validation.articleSlug,
Article.getRelatedArticles,
);

export default router;
21 changes: 20 additions & 1 deletion validators/articles/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { messages, validatorInstance } from '../../utils';
import { sanitize } from 'indicative';
import { messages, validatorInstance, sanitizeRules } from '../../utils';
import db from '../../db/models';

export default {
Expand Down Expand Up @@ -246,4 +247,22 @@ export default {
});
}
},

category: async (req, res, next) => {
const rules = {
category: 'string|required'
};

const data = req.params;

sanitize(data, sanitizeRules);
try {
await validatorInstance.validateAll(data, rules, messages);
next();
} catch (e) {
return res.status(400).json({
error: e,
});
}
},
};

0 comments on commit b8e7172

Please sign in to comment.