Skip to content

Commit

Permalink
Merge pull request #31 from andela/ch-replace-articleIdtoSlug-rating-…
Browse files Browse the repository at this point in the history
…165965800

#165965800 rating using article slug instead of article id
  • Loading branch information
Denis Niwemugisha committed May 20, 2019
2 parents 4049d88 + 8317251 commit 554819c
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 41 deletions.
4 changes: 2 additions & 2 deletions controllers/rating.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class RatingController {
image: rateAuthor.image
};
const ratings = await Rating.findOne({
where: { reviewerId: rateAuthor.id, articleId: req.params.id }
where: { reviewerId: rateAuthor.id, articleSlug: req.params.slug }
});
if (ratings) {
const [, updatedRating] = await Rating.update(
Expand All @@ -40,7 +40,7 @@ class RatingController {
}
const rating = await Rating.create({
rate,
articleId: req.params.id,
articleSlug: req.params.slug,
reviewerId: rateAuthor.id
});
return res.status(201).send({
Expand Down
11 changes: 6 additions & 5 deletions helpers/articleRatevalidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,21 @@ class ArticRatelehelper {
/**
* Check the environment
* @function validateArticleRated
* @param {string} article - Check the rate
* @param {string} slug - Check the rate
* @param {integer} user - Check the user
* @return {string} Validate the rate
*/
static async validateArticleRated(article, user) {
const findarticle = await Article.findOne({ where: { id: article } });
static async validateArticleRated(slug, user) {
const findarticle = await Article.findOne({ where: { slug } });
if (findarticle) {
const articleOwner = await Article.findAll({
where: { id: article, authorId: user }
where: { slug, authorId: user }
});
if (articleOwner.length) {
return 'Oops! You cannot rate your article';
}
} else {
}
if (findarticle == null) {
return 'This Article does not exist';
}
return true;
Expand Down
2 changes: 1 addition & 1 deletion middlewares/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ArticleMiddleware {
if (req.body.rate === undefined) {
errorMessage.push('Oops! Rate is required');
}
const validateArticle = await Articlehepler.validateArticleRated(req.params.id, req.user.id);
const validateArticle = await Articlehepler.validateArticleRated(req.params.slug, req.user.id);
if (validateArticle !== true) {
errorMessage.push(validateArticle);
}
Expand Down
18 changes: 15 additions & 3 deletions migrations/20190508204409-create-rating.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,22 @@ const ratingsMigration = {
type: Sequelize.INTEGER
},
reviewerId: {
type: Sequelize.INTEGER
type: Sequelize.INTEGER,
onDelete: 'CASCADE',
onUpdate: 'CASCADE',
references: {
model: 'Users',
key: 'id'
}
},
articleId: {
type: Sequelize.INTEGER
articleSlug: {
type: Sequelize.STRING,
onDelete: 'CASCADE',
onUpdate: 'CASCADE',
references: {
model: 'Articles',
key: 'slug'
}
},
rate: {
type: Sequelize.INTEGER
Expand Down
6 changes: 5 additions & 1 deletion models/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const articles = (sequelize, DataTypes) => {
}, {});
Article.associate = (models) => {
Article.belongsTo(models.User, { as: 'author' });
Article.hasMany(models.Rating, { foreignKey: 'articleId', allowNull: false });
Article.hasMany(models.Rating, { foreignKey: 'articleSlug', allowNull: false });
Article.hasMany(models.Like, {
foreignKey: 'titleSlug',
sourceKey: 'slug'
Expand All @@ -29,6 +29,10 @@ const articles = (sequelize, DataTypes) => {
foreignKey: 'articleId',
as: 'tagList'
});
Article.hasMany(models.Rating, {
foreignKey: 'articleSlug',
sourceKey: 'slug'
});
};
return Article;
};
Expand Down
8 changes: 5 additions & 3 deletions models/rating.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ const ratings = (sequelize, DataTypes) => {
type: DataTypes.INTEGER,
allowNull: false
},
articleId: {
type: DataTypes.INTEGER,
articleSlug: {
type: DataTypes.STRING,
allowNull: false
},
rate: {
Expand All @@ -28,7 +28,9 @@ const ratings = (sequelize, DataTypes) => {
Rating.associate = (models) => {
// associations can be defined here
Rating.belongsTo(models.User, { foreignKey: 'reviewerId', onDelete: 'CASCADE', onUpdate: 'CASCADE' });
Rating.belongsTo(models.Article, { foreignKey: 'articleId', onUpdate: 'CASCADE' });
Rating.belongsTo(models.Article, {
foreignKey: 'articleSlug', targetKey: 'slug', onDelete: 'CASCADE', onUpdate: 'CASCADE'
});
};
return Rating;
};
Expand Down
2 changes: 1 addition & 1 deletion routes/api/article/articles.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ router.put('/:slug', Auth, ArticleHelper.isOwner, ArticleHelper.isValidUpdatedAr
router.delete('/:slug', Auth, ArticleHelper.isOwner, articleController.deleteArticle);
router.get('/:slug', articleController.getArticle);
router.get('/', articleController.getAllArticles);
router.post('/rate/:id', Auth, ArticleMiddleware.checkRatedArticle, Ratingcontroller.rateArticle);
router.post('/:slug/rate', Auth, ArticleMiddleware.checkRatedArticle, Ratingcontroller.rateArticle);
router.post('/:slug/reaction/:option', Auth, ArticleHelper.isExisting, articleController.reactOnArticle);
router.get('/:slug/likes', Auth, ArticleHelper.likesNumber, articleController.getLikes);
router.get('/:slug/dislikes', Auth, ArticleHelper.dislikesNumber, articleController.getDislikes);
Expand Down
6 changes: 3 additions & 3 deletions routes/api/article/doc.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

/**
* @swagger
* /articles/rate/{id}:
* /articles/{slug}/rate:
* post:
* tags:
* - Article
Expand All @@ -23,12 +23,12 @@
* consumes:
* - application/json
* parameters:
* - name: id
* - name: slug
* in: path
* schema:
* type: integer
* required:
* - id
* - slug
* - name: authorization
* in: header
* schema:
Expand Down
10 changes: 0 additions & 10 deletions routes/api/article/rating.js

This file was deleted.

37 changes: 25 additions & 12 deletions tests/rate-article.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const articleTest = {
const rateTest = { rate: 2 };

let toKen = null;
let articleId = null;
let articleSlug = null;
describe('Rate user posted article', () => {
it('User Signup', (done) => {
chai.request(app)
Expand All @@ -46,7 +46,7 @@ describe('Rate user posted article', () => {
.set('authorization', toKen)
.send(articleTest)
.end((error, res) => {
articleId = res.body.article.id;
articleSlug = res.body.article.slug;
expect(res.body).to.have.property('article');
expect(res.body.article).to.have.property('author');
expect(res.body.article).to.have.property('title');
Expand All @@ -61,7 +61,7 @@ describe('Rate user posted article', () => {
});
it('User try to rate their article', (done) => {
chai.request(app)
.post(`/api/v1/articles/rate/${articleId}`)
.post(`/api/v1/articles/${articleSlug}/rate`)
.set('authorization', toKen)
.send(rateTest)
.end((error, res) => {
Expand All @@ -83,22 +83,22 @@ describe('Rate user posted article', () => {
});
it('User rate another user article', (done) => {
chai.request(app)
.post(`/api/v1/articles/rate/${articleId}`)
.post(`/api/v1/articles/${articleSlug}/rate`)
.set('authorization', toKen)
.send(rateTest)
.end((error, res) => {
expect(res.body).to.have.status(201);
expect(res.body.data.rating).to.have.property('rate').to.be.equal(rateTest.rate);
expect(res.body.data.rating).to.have.property('articleId').to.be.equal(articleId);
expect(res.body.data.rating).to.have.property('articleSlug').to.be.equal(articleSlug);
done();
});
});
it('User give a rate an article < 5', (done) => {
chai.request(app)
.post(`/api/v1/articles/rate/${articleId}`)
.post(`/api/v1/articles/${articleSlug}/rate`)
.set('authorization', toKen)
.send({
rate: 6,
rate: 6
})
.end((error, res) => {
expect(res.body).to.have.status(400);
Expand All @@ -108,21 +108,21 @@ describe('Rate user posted article', () => {
});
it('User update their rate on article', (done) => {
chai.request(app)
.post(`/api/v1/articles/rate/${articleId}`)
.post(`/api/v1/articles/${articleSlug}/rate`)
.set('authorization', toKen)
.send({
rate: 5,
rate: 5
})
.end((error, res) => {
expect(res.body).to.have.status(200);
expect(res.body.data.rating[0]).to.have.property('rate').to.be.equal(5);
expect(res.body.data.rating[0]).to.have.property('articleId').to.be.equal(articleId);
expect(res.body.data.rating[0]).to.have.property('articleSlug').to.be.equal(articleSlug);
done();
});
});
it('Check Article existance', (done) => {
it('Check if rate was provided', (done) => {
chai.request(app)
.post('/api/v1/articles/rate/29')
.post(`/api/v1/articles/${articleSlug}/rate`)
.set('authorization', toKen)
.send()
.end((error, res) => {
Expand All @@ -131,4 +131,17 @@ describe('Rate user posted article', () => {
done();
});
});
it('Check if the article exist', (done) => {
chai.request(app)
.post('/api/v1/articles/this-is-the-title-of-the-article/rate')
.set('authorization', toKen)
.send({
rate: 4
})
.end((error, res) => {
expect(res.body).to.have.status(400);
expect(res.body.errors.body[0]).to.be.equal('This Article does not exist');
done();
});
});
});

0 comments on commit 554819c

Please sign in to comment.