-
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.
- Loading branch information
Showing
32 changed files
with
700 additions
and
26 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,87 @@ | ||
import status from '../config/status'; | ||
import { findOrCreate, getAll, deleteHighlight } from '../queries/highlights'; | ||
|
||
/** | ||
* Highlight controller class | ||
*/ | ||
export default class Highlights { | ||
/** | ||
* create a highlighted | ||
* @param {object} req - User's request | ||
* @param {object} res - Response's holder | ||
* @returns {Object} response | ||
*/ | ||
static async create(req, res) { | ||
const userId = req.user.id; | ||
const { articleSlug } = req.params; | ||
const { | ||
highlightedText, startIndex, stopIndex, comment | ||
} = req.body; | ||
|
||
const contentLength = highlightedText.split('').length; | ||
const indexesLength = stopIndex - startIndex; | ||
if (contentLength !== indexesLength + 1) { | ||
return res.status(status.BAD_REQUEST).json({ | ||
message: 'Sorry the length of your highlightedText does not match with start and end index' | ||
}); | ||
} | ||
|
||
const created = await findOrCreate({ | ||
articleSlug, | ||
userId, | ||
highlightedText, | ||
startIndex, | ||
stopIndex, | ||
comment | ||
}); | ||
|
||
return res.status(status.CREATED).json({ message: 'You have highlighted this text', created }); | ||
} | ||
|
||
/** , | ||
* Get Highlights | ||
* @param {object} req | ||
* @param {object} res | ||
* @returns {object} highlights object | ||
*/ | ||
static async getHighlights(req, res) { | ||
const { articleSlug } = req.params; | ||
|
||
const highlights = await getAll({ | ||
articleSlug | ||
}); | ||
|
||
return ( | ||
(highlights.length === 0 | ||
&& res.status(status.NOT_FOUND).json({ | ||
message: 'You have no highlights' | ||
})) | ||
|| res.status(status.OK).json({ | ||
highlights | ||
}) | ||
); | ||
} | ||
|
||
/** , | ||
* Remove Highlights | ||
* @param {object} req | ||
* @param {object} res | ||
* @returns {object} message | ||
*/ | ||
static async deleteHighlights(req, res) { | ||
const { id, articleSlug } = req.params; | ||
|
||
const highlight = await deleteHighlight({ articleSlug, id }); | ||
|
||
return ( | ||
(!highlight | ||
&& res.status(status.NOT_FOUND).json({ | ||
message: `the highlight with id ${id} does not exist` | ||
})) | ||
|| res.status(status.OK).json({ | ||
message: 'You have successfully remove your highlight', | ||
highlight | ||
}) | ||
); | ||
} | ||
} |
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,11 @@ | ||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
import { Factory } from 'rosie'; | ||
import Chance from 'chance'; | ||
|
||
// const chance = new Chance(); | ||
|
||
export default Factory.define('highlight') | ||
.attr('highlightedText', 'on sera ensemble bientotssssss') | ||
.attr('startIndex', 0) | ||
.attr('stopIndex', 29) | ||
.attr('comment', 'welcomme to the party'); |
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,18 @@ | ||
import Joi from 'joi'; | ||
|
||
export default (input) => { | ||
const schema = Joi.object().keys({ | ||
highlightedText: Joi.string() | ||
.min(5) | ||
.max(255) | ||
.required(), | ||
startIndex: Joi.number().required(), | ||
stopIndex: Joi.number().required(), | ||
comment: Joi.string() | ||
.min(5) | ||
.max(255) | ||
.required() | ||
}); | ||
|
||
return Joi.validate(input, schema, { abortEarly: false }); | ||
}; |
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,18 @@ | ||
import Joi from 'joi'; | ||
|
||
export default (input) => { | ||
const schema = Joi.object().keys({ | ||
userType: Joi.string() | ||
.min(5) | ||
.max(6) | ||
.required(), | ||
permissions: Joi.object({ | ||
articles: Joi.array().required(), | ||
comments: Joi.array().required(), | ||
tags: Joi.array().required(), | ||
users: Joi.array().required() | ||
}).required() | ||
}); | ||
|
||
return Joi.validate(input, schema, { abortEarly: false }); | ||
}; |
Oops, something went wrong.