Skip to content

Commit

Permalink
Merge pull request #22 from andela/feature/166816209-rate-article
Browse files Browse the repository at this point in the history
166816209 Rate an article
  • Loading branch information
andela-johia committed Jul 11, 2019
2 parents 11d4923 + 18a5c67 commit b6d9bdf
Show file tree
Hide file tree
Showing 12 changed files with 305 additions and 45 deletions.
41 changes: 40 additions & 1 deletion controllers/articles/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,46 @@ export default {
});
}
},

rateArticle: async (req, res) => {
const { params: { slug }, body: { rate }, user } = req;
try {
const foundArticle = await db.Article.findOne({
where: { slug }
});
if (!foundArticle) {
return res.status(404).json({
message: 'Article does not exist'
});
}
const checkRating = await db.Ratings.findOne({
where: {
userId: user.id,
articleId: foundArticle.id
}
});
if (checkRating) {
checkRating.update({
stars: rate
});
return res.status(200).json({
message: 'Rating updated successfully'
});
}
const rating = await user.createRate({
articleId: foundArticle.id,
stars: rate
});
return res.status(201).json({
message: 'Article rated successfully',
rating,
});
} catch (e) {
return res.status(500).json({
message: 'Something went wrong',
error: e.message
});
}
},
deleteArticle: async (req, res) => {
try {
const data = { ...req.params };
Expand Down
2 changes: 0 additions & 2 deletions controllers/users/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export default {
user: user.response()
});
} catch (e) {
console.log(e);
return res.status(500).json({
message: 'Something went wrong'
});
Expand Down Expand Up @@ -80,7 +79,6 @@ export default {
user: user.response()
});
} catch (e) {
console.log(e);
return res.status(500).json({
message: 'Something went wrong'
});
Expand Down
45 changes: 45 additions & 0 deletions db/migrations/20190708163548-create-ratings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@

module.exports = {
up: (queryInterface, Sequelize) => queryInterface.createTable('Ratings', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
userId: {
type: Sequelize.INTEGER,
required: true,
references: {
model: 'Users',
key: 'id',
as: 'user'
}
},
articleId: {
type: Sequelize.INTEGER,
required: true,
references: {
model: 'Articles',
key: 'id',
as: 'article'
}
},
stars: {
type: Sequelize.INTEGER,
defaultValue: null,
validate: { min: 0, max: 5 }
},
createdAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.literal('NOW()')
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.literal('NOW()')
}
}),
down: queryInterface => queryInterface.dropTable('Ratings')
};
20 changes: 20 additions & 0 deletions db/models/Ratings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

module.exports = (sequelize, DataTypes) => {
const Ratings = sequelize.define('Ratings', {
userId: DataTypes.INTEGER,
articleId: DataTypes.INTEGER,
stars: DataTypes.INTEGER
}, {});
Ratings.associate = (models) => {
Ratings.belongsTo(models.User, {
foreignKey: 'userId',
as: 'user'
});
Ratings.belongsTo(models.Article, {
foreignKey: 'articleId',
as: 'article',
cascade: true
});
};
return Ratings;
};
17 changes: 12 additions & 5 deletions db/models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,18 @@ module.exports = (sequelize, DataTypes) => {
},
}
);
User.associate = models => User.hasMany(models.Article, {
foreignKey: 'userId',
as: 'article',
cascade: true,
});
User.associate = (models) => {
User.hasMany(models.Article, {
foreignKey: 'userId',
as: 'article',
cascade: true,
}),
User.hasMany(models.Ratings, {
foreignKey: 'userId',
as: 'rate',
cascade: true,
});
};

User.prototype.passwordsMatch = function match(password) {
return bcrypt.compare(password, this.password);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,4 @@
"cloudinary": "^1.14.0",
"express-fileupload": "^1.1.5"
}
}
}
7 changes: 7 additions & 0 deletions routes/v1/articles.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ router.post(
Validation.createArticle,
Article.createArticle,
);
router.post(
'/rate/:slug',
Middleware.authenticate,
Middleware.isblackListedToken,
Validation.rateArticle,
Article.rateArticle,
);
router.get(
'/:slug',
Validation.articleSlug,
Expand Down
42 changes: 42 additions & 0 deletions swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,39 @@ paths:
responses:
'201':
description: Article created successfully
/articles/rate/{slug}:
post:
tags:
- Articles
summary: rate an article
operationId: rateArticle
consumes:
- application/json
- application/x-www-form-urlencoded
produces:
- application/json
parameters:
- name: "slug"
in: "path"
description: An article slug to be rated by a user
required: true
type: string
- name: x-access-token
in: header
description: Authorization token
required: true
type: string
- name: "rate"
in: body
description: A user rating
required: true
schema:
$ref: '#/definitions/ratings'
responses:
'201':
description: Article rated successfully
'400':
description: "Invalid rating supplied"
/articles/{slug}:
get:
tags:
Expand Down Expand Up @@ -299,6 +332,15 @@ definitions:
type: string
xml:
name: NewArticle
ratings:
type: object
required:
- rate
properties:
rate:
type: string
xml:
name: rateArticle
UpdateProfile:
type: object
required:
Expand Down
17 changes: 3 additions & 14 deletions tests/helpers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable require-jsdoc */
import { db } from '../server';


/**
* App Middleware.
* middleware methods.
Expand Down Expand Up @@ -44,17 +45,5 @@ export const createUser = async (user) => {
return newUser;
};

export const createArticle = async (author, article) => {
const newUser = await createUser(author);
const {
description, body, title
} = article;
/* console.log(newUser); */
const newArticle = await newUser.createArticle({
description,
body,
title
});

return { newUser, newArticle };
};
export const createArticle = async article => db.Article.create(article);
export const createRate = async rating => db.Ratings.create(rating);
Loading

0 comments on commit b6d9bdf

Please sign in to comment.