Skip to content

Commit

Permalink
[Feature #65412893] highlight text in article
Browse files Browse the repository at this point in the history
  • Loading branch information
rwajon committed Jul 19, 2019
1 parent 3bee01f commit df1bfb9
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 39 deletions.
9 changes: 6 additions & 3 deletions src/config/dbConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,26 @@ const config = {
password: process.env.DB_PASSWORD_DEV,
database: process.env.DB_NAME_DEV,
host: process.env.DB_HOST_DEV,
dialect: 'postgres'
dialect: 'postgres',
seederStorage: 'sequelize'
},
test: {
use_env_variable: 'DATABASE_URL_TEST',
username: process.env.DB_USER_TEST,
password: process.env.DB_PASSWORD_TEST,
database: process.env.DB_NAME_TEST,
host: process.env.DB_HOST_TEST,
dialect: 'postgres'
dialect: 'postgres',
seederStorage: 'sequelize'
},
production: {
use_env_variable: 'DATABASE_URL',
username: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
host: process.env.DB_HOST,
dialect: 'postgres'
dialect: 'postgres',
seederStorage: 'sequelize'
}
};

Expand Down
6 changes: 3 additions & 3 deletions src/controllers/HighlightController.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default class Highlights {

const contentLength = highlightedText.split('').length;
const indexesLength = stopIndex - startIndex;
if (contentLength !== indexesLength + 1) {
if (contentLength !== indexesLength) {
return res.status(status.BAD_REQUEST).json({
message: 'Sorry the length of your highlightedText does not match with start and end index'
});
Expand Down Expand Up @@ -79,8 +79,8 @@ export default class Highlights {
message: `the highlight with id ${id} does not exist`
}))
|| res.status(status.OK).json({
message: 'You have successfully remove your highlight',
highlight
message: 'You have successfully removed your highlight',
highlightId: id
})
);
}
Expand Down
5 changes: 3 additions & 2 deletions src/helpers/queryHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ const dbFindSingle = async (model, whereCondition = {}) => model.findOne({
logging: false
});

const dbFindAll = async (model, whereCondition, offset = 0, limit = 20) => model.findAll({
const dbFindAll = async (model, whereCondition, offset = 0, limit = 20, include) => model.findAll({
offset,
limit,
where: whereCondition,
logging: false
logging: false,
include
});

const dbCreate = async (model, condition) => model.create(condition, { logging: false });
Expand Down
6 changes: 2 additions & 4 deletions src/helpers/validation/createHighlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ 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()
.allow(null, '')
.optional()
});

return Joi.validate(input, schema, { abortEarly: false });
Expand Down
2 changes: 1 addition & 1 deletion src/middlewares/checkArticle.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const checkArticle = async (req, res, next) => {
});
if (!findArticle) {
return res.status(status.NOT_FOUND).send({
message: 'That article does not exist'
message: 'This article does not exist'
});
}
next();
Expand Down
9 changes: 3 additions & 6 deletions src/models/highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ module.exports = (sequelize, DataTypes) => {
},
userId: {
type: DataTypes.INTEGER,
allowNull: true,
allowNull: false,
references: {
model: 'Users',
key: 'id'
},
onUpdate: 'CASCADE',
onDelete: 'SET NULL'
onDelete: 'CASCADE'
},
highlightedText: {
type: DataTypes.STRING,
Expand Down Expand Up @@ -52,10 +52,7 @@ module.exports = (sequelize, DataTypes) => {
{}
);
Highlight.associate = (models) => {
Highlight.belongsTo(models.User, {
foreignKey: 'userId',
onDelete: 'CASCADE'
});
Highlight.belongsTo(models.User, { foreignKey: 'userId', as: 'commentAuthor' });
};
return Highlight;
};
8 changes: 7 additions & 1 deletion src/queries/highlights/getAll.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import db from '../../models';
import { dbFindAll } from '../../helpers/queryHelper';

const getAll = async (condition = {}) => dbFindAll(db.Highlight, condition);
const getAll = async (condition = {}) => dbFindAll(db.Highlight, condition, null, null, [
{
model: db.User,
as: 'commentAuthor',
attributes: ['firstName', 'lastName', 'username', 'email', 'image']
}
]);

export default getAll;
8 changes: 4 additions & 4 deletions src/seeders/20190605075225-permissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import * as Factory from '../helpers/factory';

const permissionsNormal = Factory.permissionsNormal.build();
const permissionsAdmin = Factory.permissionsAdmin.build();
permissionsNormal.createdAt = '2019-05-12T22:00:00';
permissionsNormal.updatedAt = '2019-05-12T22:00:00';
permissionsAdmin.createdAt = '2019-05-12T22:00:00';
permissionsAdmin.updatedAt = '2019-05-12T22:00:00';
permissionsNormal.createdAt = new Date();
permissionsNormal.updatedAt = new Date();
permissionsAdmin.createdAt = new Date();
permissionsAdmin.updatedAt = new Date();

export default {
up: queryInterface => queryInterface.bulkInsert('Permissions', [permissionsNormal, permissionsAdmin], {}),
Expand Down
6 changes: 3 additions & 3 deletions src/seeders/20190715123639-admin-user.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ const userAdmin = {
email: 'admin@admin.com',
password: password.hash('admin'),
role: 'admin',
permissions: Factory.permissionsAdmin.build(),
createdAt: '2019-05-12T22:00:00',
updatedAt: '2019-05-12T22:00:00'
permissions: Factory.permissionsAdmin.build().permissions,
createdAt: new Date(),
updatedAt: new Date()
};

export default {
Expand Down
15 changes: 3 additions & 12 deletions src/tests/routes/highlight.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,6 @@ describe('Highlight on Article', () => {
cascade: true,
logging: false
});
await db.Article.destroy({
truncate: true,
cascade: true,
logging: false
});
await db.Highlight.destroy({
truncate: true,
cascade: true,
logging: false
});

createdUser = (await db.User.create(newUser, { logging: false })).dataValues;
newArticle.userId = createdUser.id;
Expand All @@ -52,6 +42,7 @@ describe('Highlight on Article', () => {
{ expiresIn: '1d' }
);
createdArticle = (await db.Article.create(newArticle, { logging: false })).dataValues;
newHighlight.userId = createdArticle.userId;
newHighlight.articleSlug = createdArticle.slug;
createdHighlight = await db.Highlight.create(newHighlight, { logging: false });
newArticleSlug = createdHighlight.articleSlug;
Expand All @@ -69,7 +60,7 @@ describe('Highlight on Article', () => {
.send({
highlightedText: 'on sera ensemble bientotssssss',
startIndex: 0,
stopIndex: 29,
stopIndex: 30,
comment: 'welcomme to the party'
})
.end((err, res) => {
Expand Down Expand Up @@ -99,7 +90,7 @@ describe('Highlight on Article', () => {
.send({
highlightedText: 'on sera ensemble bientotssssss',
startIndex: 0,
stopIndex: 30,
stopIndex: 39,
comment: 'welcomme to the party'
})
.end((err, res) => {
Expand Down

0 comments on commit df1bfb9

Please sign in to comment.