Skip to content

Commit

Permalink
feature(highlight and comment on an article):highlight and comment on…
Browse files Browse the repository at this point in the history
… an article

user can highlight and comment on an article
[finishes #166841035]
  • Loading branch information
Cavdy committed Jul 23, 2019
1 parent bdcb0a5 commit 5594d2f
Show file tree
Hide file tree
Showing 10 changed files with 173 additions and 11 deletions.
44 changes: 43 additions & 1 deletion src/controllers/articleController.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Article, Tag, User, Category } from '../db/models';
import { Article, Tag, User, Category, highlightComment } from '../db/models';
import slugGen from '../helpers/slugGen';
import urlExtractor from '../helpers/urlExtractor';
import tagExtractor from '../helpers/tagExtractor';
Expand Down Expand Up @@ -209,6 +209,48 @@ class ArticleController {
});
}
}

/**
* @description - Highlight and Comment an article
* @static
* @async
* @param {object} req - request
* @param {object} res - response
* @returns {object} highlighted comment
*/
static async highlightAndComment(req, res) {
try {
const { highlightedWord, comment } = req.body;
const userId = req.user;
const article = res.locals.articleObject;
const articleId = article.id

const isHighlightedWordFound = article.body.includes(highlightedWord)
if (isHighlightedWordFound) {
await highlightComment.create({
highlightedWord,
comment,
userId,
articleId
})

return res.status(201).json({
status: 201,
message: `${highlightedWord} has been highlighted`
});
}

return res.status(400).json({
status: 400,
message: 'invalid highlighted word'
});
} catch (error) {
return res.status(500).json({
status: 500,
message: error.message,
});
}
}
}

export default ArticleController;
1 change: 0 additions & 1 deletion src/db/migrations/20190716060554-create-report.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('Reports', {
Expand Down
1 change: 0 additions & 1 deletion src/db/migrations/20190716062433-create-user-report.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('UserReports', {
Expand Down
5 changes: 5 additions & 0 deletions src/db/models/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ module.exports = (sequelize, DataTypes) => {
onDelete: 'CASCADE',
onUpdate: 'CASCADE',
});
Article.hasMany(models.highlightComment, {
foreignKey: 'articleId',
onDelete: 'CASCADE',
onUpdate: 'CASCADE'
});
Article.hasMany(models.Rating, {
foreignKey: 'articleId',
onDelete: 'CASCADE',
Expand Down
1 change: 0 additions & 1 deletion src/db/models/commentHistory.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,3 @@ module.exports = (sequelize, DataTypes) => {
};
return CommentHistory;
};

3 changes: 1 addition & 2 deletions src/db/models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ const env = process.env.NODE_ENV || 'development';
const config = require(__dirname + '/../config/config.js')[env];
const db = {};

let sequelize;
sequelize = new Sequelize(
let sequelize = new Sequelize(
config.database,
config.username,
config.password,
Expand Down
8 changes: 4 additions & 4 deletions src/db/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,6 @@ module.exports = (sequelize, DataTypes) => {
foreignKey: 'reporterId',
as: 'userReports',
});
User.hasMany(models.highlightComment, {
foreignKey: 'userId',
as: 'highlightComment'
});
User.hasMany(models.Rating, {
foreignKey: 'userId',
as: 'userRatings'
Expand All @@ -89,6 +85,10 @@ module.exports = (sequelize, DataTypes) => {
foreignKey: 'userId',
as: 'userBookmarks'
});
User.hasMany(models.highlightComment, {
foreignKey: 'userId',
as: 'highlightComment'
});
};
return User;
};
18 changes: 18 additions & 0 deletions src/middleware/highlightValidator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { body } from 'express-validator';

const highlightValidation = {
validatehighlight: [
body('highlightedWord')
.trim()
.not().isEmpty()
.withMessage('highlighted Word is required.')
],
validateComment: [
body('comment')
.trim()
.not().isEmpty()
.withMessage('comment is required.')
],
};

export default highlightValidation;
13 changes: 12 additions & 1 deletion src/routes/articleRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import express from 'express';
import ArticleController from '../controllers/articleController';
import validateSearchInput from '../middleware/searchInputValidator';
import articleValidator from '../middleware/articleValidator';
import highlightValidator from '../middleware/highlightValidator';
import validate from '../middleware/validate';
import authorize from '../middleware/authorize';
import upload from '../helpers/profilePic';
Expand All @@ -11,9 +12,10 @@ import checkCategoryExists from '../middleware/checkCategoryExists';
import roles from '../helpers/helperData/roles';

const router = express.Router();
const { createArticle, getArticle } = ArticleController;
const { createArticle, getArticle, highlightAndComment } = ArticleController;
const { detailsValidator } = articleValidator;
const { allRoles } = roles;
const { validatehighlight, validateComment } = highlightValidator;

router.get('/articles', validateSearchInput, ArticleController.search);
router.get(
Expand All @@ -31,5 +33,14 @@ router.post(
checkCategoryExists,
createArticle
);
router.post(
'/articles/highlight/:slug',
verify,
findItem.getArticleWithAuthor,
validatehighlight,
validateComment,
validate,
highlightAndComment
);

export default router;
90 changes: 90 additions & 0 deletions test/highlightTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import chaiHttp from 'chai-http';
import chai, { expect } from 'chai';

import app from '../src/server';

chai.use(chaiHttp);

let userToken;
describe('Testing Higlight and comment Controller', () => {
describe('Testing Higlight and comment controller', () => {
const highlight = '/api/v1/articles/highlight/';

// SIGN IN USER TO GET TOKEN
it('should login user successfully', (done) => {
const user = {
email: 'john.doe@andela.com',
password: 'password',
};
chai.request(app).post('/api/v1/auth/login')
.send(user)
.end((err, res) => {
userToken = res.body.token;
done();
});
});

const higlighted = {
highlightedWord: 'survived not only',
comment: 'very good'
}
it(
'should highlight and comment if valid',
async () => {
const response = await chai.request(app)
.post(`${highlight}article`)
.set('token', userToken)
.send(higlighted);
expect(response).to.be.an('object');
expect(response).to.have.status(201);
expect(response.body.message).to.equal(`${higlighted.highlightedWord} has been highlighted`);
},
);

it(
'should not highlight and comment if invalid',
async () => {
const response = await chai.request(app)
.post(`${highlight}article`)
.set('token', userToken)
.send({
highlightedWord: 'survived not onlysssss',
comment: 'very good'
});
expect(response).to.be.an('object');
expect(response).to.have.status(400);
expect(response.body.message).to.equal('invalid highlighted word');
},
);

it(
'should not highlight and comment if no highlighted word',
async () => {
const response = await chai.request(app)
.post(`${highlight}article`)
.set('token', userToken)
.send({
comment: 'very good'
});
expect(response).to.be.an('object');
expect(response).to.have.status(400);
expect(response.body).to.have.property('message');
},
);

it(
'should not highlight and comment if no comment',
async () => {
const response = await chai.request(app)
.post(`${highlight}article`)
.set('token', userToken)
.send({
highlightedWord: 'survived not onlysssss',
});
expect(response).to.be.an('object');
expect(response).to.have.status(400);
expect(response.body).to.have.property('message');
},
);
});
});

0 comments on commit 5594d2f

Please sign in to comment.