-
Notifications
You must be signed in to change notification settings - Fork 1
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
7 changed files
with
586 additions
and
0 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
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; |
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,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; |
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,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') | ||
}; |
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,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; |
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.