Skip to content

Commit

Permalink
Merge 8e0e58c into 96af5eb
Browse files Browse the repository at this point in the history
  • Loading branch information
jean luc tuyishime committed Jun 5, 2019
2 parents 96af5eb + 8e0e58c commit 2823969
Show file tree
Hide file tree
Showing 32 changed files with 700 additions and 26 deletions.
41 changes: 30 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/controllers/ArticleController.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ export default class ArticleController {
* @returns {object} Object representing the response returned
*/
static async saveArticle(req, res) {
const image = await helpers.upload(req);
// const image = await helpers.upload(req);
const { tagList } = req.body;
const newArticle = await Article.create({
userId: req.user.id || 0,
slug: helpers.generator.slug(req.body.title),
title: req.body.title.trim(),
description: req.body.description.trim(),
body: req.body.body.trim(),
coverUrl: image.image.original,
coverUrl: req.body.coverUrl || 'placeholder.png',
tagList,
readTime: helpers.generator.readtime(req.body.body)
});
Expand Down
20 changes: 11 additions & 9 deletions src/controllers/AuthLocalController.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ export default class AuthLocalController {
if (!newUser.errors && newUser && Object.keys(newUser).length > 0) {
delete newUser.password;
return res.status(status.CREATED).send({
user: newUser, token: helper.token.generate({ email, role: req.body.isAdmin })
user: newUser,
token: helper.token.generate({ email, role: req.body.isAdmin })
});
}
}
Expand All @@ -50,23 +51,24 @@ export default class AuthLocalController {
const { email, password } = req.body;
const checkUser = await User.findOne({ email });
if (Object.keys(checkUser).length > 0) {
const comparePassword = helper.password.compare(password, checkUser.password);
if (!comparePassword) {
if (!helper.password.compare(password, checkUser.password)) {
return res.status(status.UNAUTHORIZED).send({
message: 'The credentials you provided is incorrect'
});
}
const payload = {
id: checkUser.id, role: checkUser.role, permissions: checkUser.permissions
};
const token = helper.token.generate(payload);
delete checkUser.password;
return res.status(status.OK).send({
user: checkUser,
token
token: helper.token.generate({
id: checkUser.id,
role: checkUser.role,
permissions: checkUser.permissions
})
});
}
return res.status(status.UNAUTHORIZED).send({ error: `User with ${email} email doesn't exist!!!` });
return res
.status(status.UNAUTHORIZED)
.send({ error: `User with ${email} email doesn't exist!!!` });
} catch (error) {
return res.status(500).send({ error });
}
Expand Down
87 changes: 87 additions & 0 deletions src/controllers/HighlightController.js
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
})
);
}
}
2 changes: 2 additions & 0 deletions src/controllers/PermissionController.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { User } from '../queries';
import status from '../config/status';

// import * as validate from '../helpers/validation';

/**
* A class to handle user local authentication
*/
Expand Down
11 changes: 11 additions & 0 deletions src/helpers/factory/highlight.js
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');
4 changes: 3 additions & 1 deletion src/helpers/factory/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import report from './report';
import editComment from './editComment';
import permissionsNormal from './permissionsNormal';
import permissionsAdmin from './permissionsAdmin';
import highlight from './highlight';

export {
user,
Expand All @@ -27,5 +28,6 @@ export {
report,
editComment,
permissionsNormal,
permissionsAdmin
permissionsAdmin,
highlight
};
1 change: 1 addition & 0 deletions src/helpers/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as factory from './factory';
import * as validation from './validation';
import * as token from './tokens';
// import tokenGenerator from './tokenGenerator';
import sendgridMailTemplate from './sendgridMailTemplate';
import * as password from './password';
import sendMail from './sendMail';
Expand Down
18 changes: 18 additions & 0 deletions src/helpers/validation/createHighlight.js
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 });
};
18 changes: 18 additions & 0 deletions src/helpers/validation/createPermissions.js
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 });
};
Loading

0 comments on commit 2823969

Please sign in to comment.