-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(rate article): add user should be able to give a rating to an…
… article [Finishes 167313404]
- Loading branch information
Showing
16 changed files
with
672 additions
and
100 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
Large diffs are not rendered by default.
Oops, something went wrong.
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,129 @@ | ||
import db from '../models/index'; | ||
import RateService from '../services/rate.service'; | ||
/** | ||
* | ||
* | ||
* @class rateController | ||
*/ | ||
class rateController { | ||
/** | ||
* | ||
* | ||
* @static | ||
* @param {*} req | ||
* @param {*} res | ||
* @returns {Object} return rating information to user | ||
* @memberof UserController | ||
*/ | ||
static async setArticleRating(req, res) { | ||
try { | ||
// Initialize rating data | ||
const userEmail = req.auth.email; | ||
const { rate } = req.body; | ||
const { articleSlug } = req.params; | ||
const rateSchema = { userEmail, articleSlug, rate }; | ||
|
||
// check if user is trying to rate his/her own article | ||
const article = await db.Article.findOne({ | ||
where: { slug: articleSlug } | ||
}); | ||
const user = await db.user.findOne({ | ||
where: { id: article.authorId } | ||
}); | ||
if (user.email === userEmail) { | ||
return res.status(400).send({ | ||
status: 400, | ||
message: 'You cannot rate your own article' | ||
}); | ||
} | ||
|
||
// create rating | ||
const createdRate = await RateService.create(rateSchema); | ||
return res.status(200).send({ | ||
status: 200, | ||
message: 'Thank you for rating this article', | ||
data: createdRate | ||
}); | ||
} catch (error) { | ||
return res.status(404).send({ | ||
status: 404, | ||
message: error.message | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* | ||
* @static | ||
* @param {*} req | ||
* @param {*} res | ||
* @returns {Object} return rating information to user | ||
* @memberof UserController | ||
*/ | ||
static async updateArticleRating(req, res) { | ||
try { | ||
// Initialize rating data | ||
const userEmail = req.auth.email; | ||
const { rate } = req.body; | ||
const { articleSlug } = req.params; | ||
|
||
// update article rate | ||
const updatedRate = await RateService.update(articleSlug, userEmail, rate); | ||
if (!updatedRate) { | ||
return res.status(400).send({ | ||
status: 400, | ||
error: `Rating for article with Slug: ${articleSlug} not found` | ||
}); | ||
} | ||
return res.status(200).send({ | ||
status: 200, | ||
message: 'Thank you for rating this article', | ||
newRating: updatedRate | ||
}); | ||
} catch (error) { | ||
return res.status(404).send({ | ||
status: 404, | ||
message: error.message | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* | ||
* @static | ||
* @param {*} req | ||
* @param {*} res | ||
* @returns {Object} return rating information to user | ||
* @memberof rateController | ||
*/ | ||
static async getArticleRating(req, res) { | ||
try { | ||
// Initialize rating data | ||
const userEmail = req.auth.email; | ||
const { articleSlug } = req.params; | ||
|
||
// find particular rating | ||
const userRate = await RateService.findOne(articleSlug, userEmail); | ||
if (!userRate) { | ||
return res.status(400).send({ | ||
status: 400, | ||
error: `Rating for article with slug ${articleSlug} not found`, | ||
}); | ||
} | ||
return res.status(200).send({ | ||
status: 200, | ||
message: `Rating for article with slug ${articleSlug} found`, | ||
data: userRate | ||
}); | ||
} catch (error) { | ||
return res.status(404).send({ | ||
status: 404, | ||
message: error.message | ||
}); | ||
} | ||
} | ||
} | ||
|
||
export default rateController; |
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,21 @@ | ||
import Joi from 'joi'; | ||
import Util from '../../helpers/util'; | ||
|
||
const util = new Util(); | ||
|
||
export default (req, res, next) => { | ||
const { rate } = req.body; | ||
|
||
const schema = { | ||
rate: Joi.number().integer().min(1).max(5), | ||
}; | ||
const { error } = Joi.validate({ | ||
rate | ||
}, schema); | ||
|
||
if (!error) return next(); | ||
const errorMessageFromJoi = error.details[0].message; | ||
const removeEscapeCharacters = errorMessageFromJoi.replace(/['"]+/g, ''); | ||
util.setError(400, removeEscapeCharacters); | ||
return util.send(res); | ||
}; |
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,47 @@ | ||
'use strict'; | ||
module.exports = { | ||
up: (queryInterface, Sequelize) => { | ||
return queryInterface.createTable('Rates', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER | ||
}, | ||
userEmail: { | ||
type: Sequelize.STRING, | ||
references: { | ||
model: 'users', | ||
key: 'email', | ||
}, | ||
onDelete: 'CASCADE', | ||
onUpdate: 'CASCADE' | ||
}, | ||
articleSlug: { | ||
type: Sequelize.STRING, | ||
references: { | ||
model: 'Articles', | ||
key: 'slug', | ||
}, | ||
onDelete: 'CASCADE', | ||
onUpdate: 'CASCADE' | ||
}, | ||
rate: { | ||
type: Sequelize.ENUM({ | ||
values: [1, 2, 3, 4, 5] | ||
}) | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
} | ||
}); | ||
}, | ||
down: (queryInterface, Sequelize) => { | ||
return queryInterface.dropTable('Rates'); | ||
} | ||
}; |
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,35 @@ | ||
'use strict'; | ||
module.exports = (sequelize, DataTypes) => { | ||
const Rate = sequelize.define('Rate', { | ||
userEmail: { | ||
type: DataTypes.STRING, | ||
references: { | ||
model: 'users', | ||
key: 'email', | ||
}, | ||
onDelete: 'CASCADE', | ||
onUpdate: 'CASCADE' | ||
}, | ||
articleSlug: { | ||
type: DataTypes.STRING, | ||
references: { | ||
model: 'Articles', | ||
key: 'slug', | ||
}, | ||
onDelete: 'CASCADE', | ||
onUpdate: 'CASCADE' | ||
}, | ||
rate: DataTypes.ENUM({ | ||
values: [1, 2, 3, 4, 5] | ||
}) | ||
}, {}); | ||
Rate.associate = function (models) { | ||
Rate.belongsTo(models.user, { | ||
foreignKey: 'userEmail', | ||
}); | ||
Rate.belongsTo(models.Article, { | ||
foreignKey: 'articleSlug', | ||
}); | ||
}; | ||
return Rate; | ||
}; |
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
Oops, something went wrong.