-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ft(highlight_&_comment):The user should be able the highlight the art…
…icle and comment about it.
- Loading branch information
Showing
6 changed files
with
207 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,56 @@ | ||
import db from '../../sequelize/models'; | ||
|
||
const { Highlights } = db; | ||
/** | ||
* @author Diane Mahoro | ||
* @class Highlight | ||
* @description this class performs the whole of highlightings | ||
*/ | ||
class Highlight { | ||
/** | ||
* @param {Object} req - Request object | ||
* @param {Object} res - Response object | ||
* @returns {Object} - Response object | ||
*/ | ||
static async createHighlights(req, res) { | ||
const { id } = req.user; | ||
const { | ||
highlightText, comment, occurencyNumber | ||
} = req.body; | ||
const { article } = req; // This contains the article | ||
const response = await Highlights.findOne({ | ||
where: { | ||
articleId: article.id, | ||
userId: id, | ||
highlightText, | ||
occurencyNumber | ||
} | ||
}); | ||
if (!response) { | ||
const { body } = article; | ||
const regX = new RegExp(highlightText, 'g'); | ||
const count = (body.match(regX) || []).length; | ||
if (count === 0 || occurencyNumber > count) { | ||
return res.status(404).json({ | ||
Message: 'Highlight text not found' | ||
}); | ||
} | ||
const newHighlight = await Highlights.create({ | ||
articleId: article.id, | ||
userId: id, | ||
highlightText, | ||
comment, | ||
occurencyNumber, | ||
}); | ||
return res.status(201).json({ | ||
Message: `Thank you for highlighting this text ${newHighlight.highlightText}` | ||
}); | ||
} | ||
|
||
return res.status(403).json({ | ||
Message: 'you have already highlighted this text.' | ||
}); | ||
} | ||
} | ||
|
||
export default 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
36 changes: 36 additions & 0 deletions
36
src/sequelize/migrations/20190627142139-create-highlights.js
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,36 @@ | ||
|
||
|
||
module.exports = { | ||
up: (queryInterface, Sequelize) => queryInterface.createTable('Highlights', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER | ||
}, | ||
articleId: { | ||
type: Sequelize.INTEGER | ||
}, | ||
userId: { | ||
type: Sequelize.INTEGER | ||
}, | ||
highlightText: { | ||
type: Sequelize.TEXT | ||
}, | ||
comment: { | ||
type: Sequelize.TEXT | ||
}, | ||
occurencyNumber: { | ||
type: Sequelize.INTEGER | ||
}, | ||
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,13 @@ | ||
export default (sequelize, DataTypes) => { | ||
const Highlights = sequelize.define('Highlights', { | ||
articleId: DataTypes.INTEGER, | ||
userId: DataTypes.INTEGER, | ||
highlightText: DataTypes.TEXT, | ||
comment: DataTypes.TEXT, | ||
occurencyNumber: DataTypes.INTEGER | ||
}, {}); | ||
Highlights.associate = () => { | ||
// associations can be defined here | ||
}; | ||
return 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,82 @@ | ||
import chai from 'chai'; | ||
import chaiHttp from 'chai-http'; | ||
import dotenv from 'dotenv'; | ||
import server from '../src/index'; | ||
import db from '../src/sequelize/models'; | ||
import tokenHelper from '../src/helpers/Token.helper'; | ||
|
||
|
||
const { User, Article } = db; | ||
// eslint-disable-next-line no-unused-vars | ||
const should = chai.should(); | ||
|
||
chai.use(chaiHttp); | ||
const { expect } = chai; | ||
|
||
dotenv.config(); | ||
|
||
let newArticle; | ||
const highlight = { | ||
highlightText: 'Rwanda', | ||
comment: 'Thank you', | ||
occurencyNumber: 1 | ||
}; | ||
|
||
describe('Highlight the Article', () => { | ||
let token; | ||
before(async () => { | ||
const user = { | ||
firstName: 'Emy', | ||
lastName: 'Rukundo', | ||
username: 'mifeillee', | ||
email: 'nimilleer@gmail.com', | ||
password: 'Rukundo1!', | ||
confirmPassword: 'Rukundo1!' | ||
}; | ||
const newUser = await User.create(user); | ||
|
||
token = await tokenHelper.generateToken({ id: newUser.id }); | ||
|
||
const article = { | ||
title: 'Andela Kigali Launch', | ||
description: 'Andela Rwanda, a technology company specializing in training software engineers on Thursday last week launched its Kigali office. Its new offices are housed within the University of Rwanda - College of Science and Technology, in Muhabura Block.', | ||
body: 'Andela Rwanda, a technology company specializing in training software engineers on Thursday last week launched its Kigali office. Its new offices are housed within the University of Rwanda - College of Science and Technology, in Muhabura Block. The firm made their debut in Rwanda in July last year, with pan-African hub, the first of its kind. This was followed by the announcement in October of Clement Uwajeneza as the Country Director. The firm has a Memorandum of Understanding with the government to recruit, train and connect to market about 500 young software engineers in the country', | ||
tagList: ['Tech', 'Kigali'], | ||
authorId: newUser.id, | ||
slug: 'slyg', | ||
readtime: '1 min' | ||
}; | ||
newArticle = await Article.create(article); | ||
}); | ||
it('should highlight with comment or without', (done) => { | ||
chai.request(server) | ||
.post(`/api/articles/${newArticle.slug}/highlight`) | ||
.set('token', token) | ||
.send(highlight) | ||
.end((err, res) => { | ||
res.should.have.status(201); | ||
expect(res.body.Message).to.be.a('string'); | ||
done(); | ||
}); | ||
}); | ||
it('should raise an error when the user highlights the same text ', (done) => { | ||
chai.request(server) | ||
.post(`/api/articles/${newArticle.slug}/highlight`) | ||
.set('token', token) | ||
.send(highlight) | ||
.end((err, res) => { | ||
res.should.have.status(403); | ||
expect(res.body.Message).to.be.a('string'); | ||
done(); | ||
}); | ||
}); | ||
it('should raise an error when the user is not logged in', (done) => { | ||
chai.request(server) | ||
.post(`/api/articles/${newArticle.slug}/highlight`) | ||
.set('token', ' ') | ||
.end((err, res) => { | ||
res.should.have.status(401); | ||
done(); | ||
}); | ||
}); | ||
}); |