-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
31 changed files
with
934 additions
and
73 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
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,66 @@ | ||
import models from '../models'; | ||
import Response from '../helpers/responseHelper'; | ||
import { STATUS } from '../helpers/constants'; | ||
import getAverageRatings from '../helpers/articleRatingHelper'; | ||
|
||
const { ratings } = models; | ||
/** | ||
* @description Article rating functionality | ||
* @export- ArticleRatingController | ||
* @class ArticleRatingController | ||
* @returns {object} the rated article object | ||
*/ | ||
export default class ArticleRatingController { | ||
/** | ||
* @description Add ratings for a specific article and returns the rated article | ||
* @static | ||
* @param {req} req - the request object | ||
* @param {res} res - the resposne object | ||
* @returns {object} the rated article object | ||
*/ | ||
static async rateArticle(req, res) { | ||
const userId = req.user.id; | ||
const { articleId } = req.params; | ||
const { body: { stars } } = req; | ||
try { | ||
const articleRating = await ratings.create({ | ||
userId, | ||
articleId, | ||
stars | ||
}); | ||
return Response.send(res, STATUS.CREATED, articleRating, 'success'); | ||
} catch (err) { | ||
return Response.send(res, STATUS.SERVER_ERROR, err, 'failed'); | ||
} | ||
} | ||
|
||
/** | ||
* @description Gets all rated article from the database and returns the rated articles | ||
* @static | ||
* @param {function} req the request object | ||
* @param {function} res the resposne object | ||
* @returns {object} the rated article object | ||
*/ | ||
static async getOneRatedArticle(req, res) { | ||
try { | ||
const getRatings = await ratings.findAndCountAll({ | ||
where: { | ||
articleId: req.params.articleId, | ||
} | ||
}); | ||
if (getRatings.count >= 1) { | ||
const averageRatings = getAverageRatings(getRatings, getRatings.count); | ||
console.log(averageRatings); | ||
const finalRatings = Object.assign({}, getRatings, { | ||
averageRatings | ||
}); | ||
console.log(finalRatings); | ||
return Response.send(res, STATUS.OK, finalRatings, 'success'); | ||
} | ||
return Response.send(res, STATUS.NOT_FOUND, [], 'No ratings for this article'); | ||
} catch (err) { | ||
console.log(err); | ||
return Response.send(res, STATUS.SERVER_ERROR, err, 'failed'); | ||
} | ||
} | ||
} |
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
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,24 @@ | ||
// import models from '../models'; | ||
import Response from '../helpers/responseHelper'; | ||
import { STATUS } from '../helpers/constants'; | ||
|
||
/** | ||
* Wrapper class for sending user statistics numbers and breakdowns as response. | ||
* | ||
* @export | ||
* @class StatiticsController | ||
*/ | ||
export default class StatiticsController { | ||
/** | ||
* This controller combines the results of the calculated stats | ||
* and returns the user stats object | ||
* @static | ||
* @param {function} req The request object | ||
* @param {fucntion} res The response object | ||
* @returns {function} The stats object | ||
*/ | ||
static async sendUserStats(req, res) { | ||
const { stats } = res.locals; | ||
return Response.send(res, STATUS.OK, stats, 'stats successfully fetched', true); | ||
} | ||
} |
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,38 @@ | ||
export default { | ||
up: (queryInterface, Sequelize) => queryInterface.createTable('ratings', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER | ||
}, | ||
userId: { | ||
type: Sequelize.INTEGER, | ||
allowNull: false, | ||
references: { | ||
model: 'Users', | ||
key: 'id' | ||
} | ||
}, | ||
articleId: { | ||
type: Sequelize.INTEGER, | ||
allowNull: false, | ||
references: { | ||
model: 'Articles', | ||
key: 'id' | ||
}, | ||
}, | ||
stars: { | ||
type: Sequelize.INTEGER, | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
}), | ||
down: (queryInterface, Sequelize) => queryInterface.dropTable('ratings') | ||
}; |
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,41 @@ | ||
/* eslint no-unused-vars: ["error", { "args": "none" }] */ | ||
export default { | ||
up: (queryInterface, Sequelize) => (queryInterface.createTable('Histories', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER | ||
}, | ||
articleId: { | ||
type: Sequelize.INTEGER, | ||
onDelete: 'CASCADE', | ||
allowNull: false, | ||
references: { | ||
model: 'Articles', | ||
key: 'id' | ||
} | ||
}, | ||
userId: { | ||
type: Sequelize.INTEGER, | ||
allowNull: false, | ||
references: { | ||
model: 'Users', | ||
key: 'id' | ||
}, | ||
}, | ||
categoryId: { | ||
type: Sequelize.INTEGER, | ||
allowNull: false, | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
} | ||
})), | ||
down: (queryInterface, Sequelize) => queryInterface.dropTable('Histories') | ||
}; |
26 changes: 26 additions & 0 deletions
26
database/migrations/20190320121330-create-comment-history.js
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,26 @@ | ||
export default { | ||
up: (queryInterface, Sequelize) => queryInterface.createTable('CommentHistory', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER | ||
}, | ||
commentId: { | ||
allowNull: false, | ||
type: Sequelize.INTEGER | ||
}, | ||
body: { | ||
type: Sequelize.STRING | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
} | ||
}), | ||
down: (queryInterface, Sequelize) => queryInterface.dropTable('CommentHistory'), | ||
}; |
Oops, something went wrong.