Skip to content

Commit

Permalink
resolve conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexandra Collins authored and Alexandra Collins committed Mar 22, 2019
2 parents c321492 + 0f6971c commit ae68020
Show file tree
Hide file tree
Showing 31 changed files with 934 additions and 73 deletions.
5 changes: 5 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,10 @@ GOOGLE_CLIENT_ID=
GOOGLE_CLIENT_SECRET=
GOOGLE_CALLBACK_URL=

JWT_TOKEN=

# sessions details
COOKIE_SECRET=


API_DOMAIN=localhost:3000
66 changes: 66 additions & 0 deletions controllers/articleRatingController.js
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');
}
}
}
11 changes: 10 additions & 1 deletion controllers/articlesController.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import Response from '../helpers/responseHelper';
import articleHelpers from '../helpers/articleHelpers';
import { STATUS } from '../helpers/constants';
import Logger from '../helpers/logger';
import statsHelper from '../helpers/statsHelper';

const { Article, Bookmark } = models;
// const { ArticleCategory } = models;

const { Article, Bookmark, ratings } = models;

/**
* Wrapper class for sending article objects as response.
Expand Down Expand Up @@ -121,6 +124,10 @@ export default class ArticlesController {
where: {
...categoryQuery,
}
},
{
model: ratings,
attributes: ['stars', 'userId']
}
],
});
Expand All @@ -144,6 +151,7 @@ export default class ArticlesController {
* @returns {function} an array of Articles object
*/
static async getOne(req, res) {
const { userId } = res.locals;
const { slug } = req.params;
try {
const article = await models.Article.findOne({
Expand All @@ -158,6 +166,7 @@ export default class ArticlesController {
if (!article) {
return Response.send(res, STATUS.NOT_FOUND, [], `no article with slug: ${slug} found`, false);
}
if (userId) await statsHelper.confirmUser(userId, article.id, article.categoryId);
return Response.send(res, STATUS.OK, article, 'article was successfully fetched', true);
} catch (error) {
return Response.send(res, STATUS.BAD_REQUEST, error, 'server error', false);
Expand Down
26 changes: 25 additions & 1 deletion controllers/commentsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import models from '../models';
import Response from '../helpers/responseHelper';
import { STATUS } from '../helpers/constants';
import paginationHelper from '../helpers/articleHelpers';
import Logger from '../helpers/logger';

/**
* Wrapper class for sending comments objects as response.
Expand Down Expand Up @@ -64,9 +65,12 @@ export default class CommentsController {
]
},
as: 'author'
},
{
model: models.CommentHistory,
}
],
attributes: { exclude: ['authorId'] },
attributes: {},
limit,
offset,
order: [['createdAt']]
Expand All @@ -76,6 +80,7 @@ export default class CommentsController {
} = paginationHelper.getResourcesAsPages(req, comments);
return Response.send(res, code, data, 'comments successfully fetched', status);
} catch (error) {
Logger.log(error);
return Response.send(res, STATUS.BAD_REQUEST, error, 'Server error', false);
}
}
Expand All @@ -94,6 +99,25 @@ export default class CommentsController {
const { body } = req.body;
const { id } = req.params;

// here we get the commet to be updated and then we create a new comment history for it
// with the previous comment then we update the comment data
try {
const oldCommentData = await models.Comment.findOne({
where: { articleId, id, authorId },
raw: true
});
const count = await models.CommentHistory.count({ where: { commentId: id } });
// limiting the comment history to 5
if (count < 5) {
const oldMessage = oldCommentData.body;
await models.CommentHistory.create({ body: oldMessage, commentId: id });
} else {
return Response.send(res, STATUS.BAD_REQUEST, null, 'You can only edit a comment 5 times', false);
}
} catch (e) {
Logger.log(e);
return Response.send(res, STATUS.BAD_REQUEST, null, 'Server error', false);
}
try {
const comment = await models.Comment.update({ body: body.trim(), isAnonymousUser }, {
where: { articleId, id, authorId },
Expand Down
24 changes: 24 additions & 0 deletions controllers/statsController.js
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);
}
}
38 changes: 38 additions & 0 deletions database/migrations/20190317233511-create-ratings.js
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')
};
41 changes: 41 additions & 0 deletions database/migrations/20190320080157-create-history.js
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 database/migrations/20190320121330-create-comment-history.js
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'),
};
Loading

0 comments on commit ae68020

Please sign in to comment.