Skip to content

Commit

Permalink
Merge 9849d11 into 21646e3
Browse files Browse the repository at this point in the history
  • Loading branch information
joeeasy authored Oct 1, 2018
2 parents 21646e3 + 9849d11 commit d6299d1
Show file tree
Hide file tree
Showing 7 changed files with 586 additions and 0 deletions.
140 changes: 140 additions & 0 deletions server/controllers/hightlightController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import helpers from '../helpers/helpers';
import models from '../models';

const { Highlights } = models;
const { parsedId } = helpers;

const highlightController = {
/**
* @method create
* @description Allows users to highlight an article
* @param {Object} req - The request object
* @param {Object} res - The response object
* @return {Object} The result from hightlight the article
*/
create: (req, res) => {
const { highlightedText, comment, color } = req.body;
const authorId = parsedId(req.params.authorId);
const articleId = parsedId(req.params.articleId);
Highlights.findOrCreate({
where: {
highlightedText,
userId: req.currentUser.id,
articleId,
comment,
color,
authorId
}
})
.spread((highlight, created) => (!(created) ? res.status(400).jsend.fail({ message: `You have already commented on ${highlightedText}` })
: res.status(201).jsend.success({
highlight,
message: 'Your comment has been saved'
})))
.catch(err => res.status(500).jsend.fail({
message: 'Something went wrong, please try again',
error: err.message
}));
},

/**
* @method fetchAll
* @description All an author to view all highlight on an article
* @param {Object} req - The request object
* @param {Object} res - The response object
* @returns {Object} All highlights on the article
*/
fetchAll: (req, res) => {
const authorId = req.currentUser.id;
const articleId = parsedId(req.params.articleId);
Highlights.findAll({
where: {
authorId,
articleId
}
}).then(highlights => res.status(200).jsend.success({
message: 'Highlighted text',
highlights
}))
.catch(err => res.status(500).jsend.fail({
message: 'Something went wrong, please try again',
error: err.message
}));
},

/**
* @method fetchReaderHighlight
* @description Allows logged in users to view their highlights on an article
* @param {Object} req - The request object
* @param {Object} res - The response object
* @returns {Object} The highlights of the user
*/
fetchReaderHighlight: (req, res) => {
const articleId = parsedId(req.params.articleId);
Highlights.findAll({
where: {
userId: req.currentUser.id,
articleId
}
}).then(highlights => res.status(200).jsend.success({
message: 'Highlighted text',
highlights
}))
.catch(err => res.status(500).jsend.fail({
message: 'Something went wrong, please try again',
error: err.message
}));
},
/**
* @method update
* @description Allows users to update the comment they've created
* @param {Object} req - The request object
* @param {Object} res - The response object
* @returns {Object} The update highlight of the user
*/
update: (req, res) => {
const { comment, color } = req.body;
const articleId = parsedId(req.params.articleId);
Highlights.update({ comment, color }, {
where: {
userId: req.currentUser.id,
articleId
},
}).then((highlight) => {
if (highlight) {
return res.status(200).jsend.success({
message: 'Your highlight has been updated successfully',
highlight
});
}
}).catch(err => res.status(500).jsend.fail({
message: 'Something went wrong, please try again',
error: err.message
}));
},

/**
* @method Delete
* @description Allows users to remove an highlight they've created
* @param {Object} req - The request object
* @param {Object} res - The response object
* @returns {Object} delete success message
*/
delete: (req, res) => {
const authorId = parsedId(req.params.authorId);
const articleId = parsedId(req.params.articleId);
Highlights.destroy({
where: {
userId: req.currentUser.id || authorId,
articleId
}
}).then(() => res.status(200).jsend.success({
message: 'Your highlight have been deleted'
})).catch(err => res.status(500).jsend.fail({
message: 'Something went wrong, please try again',
error: err.message
}));
}
};

export default highlightController;
42 changes: 42 additions & 0 deletions server/helpers/highlightHelpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import helpers from './helpers';


const { parsedId } = helpers;

const highlightValidation = {
authorId: (req, res, next) => {
const authorId = parsedId(req.params.authorId);
if (
!(Number.isInteger(authorId))
) {
return res.status(400).jsend.fail({
message: 'Your request is not valid'
});
}
return next();
},
articleId: (req, res, next) => {
const articleId = parsedId(req.params.articleId);
if (
!(Number.isInteger(articleId))
) {
return res.status(400).jsend.fail({
message: 'Your request is not valid'
});
}
return next();
},
comment: (req, res, next) => {
const { comment } = req.body;
if (
!comment || comment.trim().length < 1
) {
return res.status(400).jsend.fail({
message: 'Comment is required'
});
}
return next();
}
};

export default highlightValidation;
55 changes: 55 additions & 0 deletions server/migrations/20180919080241-create-highlights.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
module.exports = {
up: (queryInterface, Sequelize) => queryInterface.createTable('Highlights', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
highlightedText: {
type: Sequelize.STRING
},
color: {
type: Sequelize.STRING
},
comment: {
type: Sequelize.STRING
},
userId: {
type: Sequelize.INTEGER,
onDelete: 'CASCADE',
references: {
model: 'Users',
key: 'id',
as: 'userId'
}
},
authorId: {
type: Sequelize.INTEGER,
onDelete: 'CASCADE',
references: {
model: 'Users',
key: 'id',
as: 'authorId'
}
},
articleId: {
type: Sequelize.INTEGER,
onDelete: 'CASCADE',
references: {
model: 'Articles',
key: 'id',
as: 'articleId'
}
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
}),
down: queryInterface => queryInterface.dropTable('Highlights')
};
33 changes: 33 additions & 0 deletions server/models/highlights.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const hightlights = (sequelize, DataTypes) => {
const Highlights = sequelize.define('Highlights', {
highlightedText: {
type: DataTypes.STRING,
allowNull: false
},
comment: {
type: DataTypes.STRING,
allowNull: false
},
color: {
type: DataTypes.STRING
}
});
Highlights.associate = (models) => {
Highlights.belongsTo(models.Users, {
foreignKey: 'userId',
onDelete: 'CASCADE'
});
Highlights.belongsTo(models.Users, {
foreignKey: 'authorId',
onDelete: 'CASCADE'
});
Highlights.belongsTo(models.Articles, {
foreignKey: 'articleId',
as: 'articles',
onDelete: 'CASCADE'
});
};
return Highlights;
};

export default hightlights;
24 changes: 24 additions & 0 deletions server/routes/articleRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import usersValidations from '../middleware/usersValidations';
import checkParams from '../middleware/checkParams';
import { multerUploads } from '../config/multer/multerConfig';
import paginationParamsValidations from '../middleware/paginationParamsValidations';
import highlightController from '../controllers/hightlightController';
import highlightValidation from '../helpers/highlightHelpers';

const {
reportArticle,
Expand All @@ -33,6 +35,11 @@ const {
validateViolation,
validateRequestObject,
} = reportValidation;
const {
authorId,
articleId,
comment
} = highlightValidation;

const articleRoutes = express.Router();

Expand Down Expand Up @@ -65,4 +72,21 @@ articleRoutes.post(
reportArticle
);
articleRoutes.get('/search', searchController);

// HIGHLIGHT ROUTES
// create highlights
articleRoutes.post('/highlights/:articleId/:authorId', auth, articleId, authorId, comment, highlightController.create);

// fetch all reader's highlight
articleRoutes.get('/highlights/:articleId/:authorId', auth, articleId, authorId, highlightController.fetchReaderHighlight);

// fetch all highlights for author
articleRoutes.get('/highlights/:articleId/:authorId/all', auth, articleId, authorId, highlightController.fetchReaderHighlight);

// update highlight route
articleRoutes.put('/highlights/:articleId/:authorId', auth, articleId, authorId, comment, highlightController.update);

// delete an highlight
articleRoutes.delete('/highlights/:articleId/:authorId', auth, articleId, authorId, highlightController.delete);

export default articleRoutes;
Loading

0 comments on commit d6299d1

Please sign in to comment.