-
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.
- Loading branch information
Showing
14 changed files
with
134 additions
and
9 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,50 @@ | ||
import { Bookmark } from '../db/models'; | ||
|
||
/** | ||
* @description - Article Bookmarks | ||
* @class BookmarkController | ||
*/ | ||
class BookmarkController{ | ||
/** | ||
* @description - Bookmark an article | ||
* @return {object } res | ||
* @static | ||
* @param { object } req | ||
* @param { object } res | ||
* @memberof BookmarkController | ||
*/ | ||
static async bookmarkArticles(req, res){ | ||
const userId = req.user; | ||
const {article} = res.locals; | ||
const articleId = article.id | ||
|
||
try{ | ||
const bookmarkedArticle = await Bookmark.findOne({ | ||
where: { userId, articleId } | ||
}); | ||
if (bookmarkedArticle) { | ||
await bookmarkedArticle.destroy(); | ||
return res.status(200).json({ | ||
status: 200, | ||
message: 'You just unbookmarked this article', | ||
}); | ||
} | ||
await Bookmark.create({ | ||
userId, articleId | ||
}); | ||
return res.status(200).json({ | ||
status: 200, | ||
message: 'You just bookmarked this article' | ||
}) | ||
|
||
}catch(err){ | ||
return res.status(500).json({ | ||
status: 500, | ||
message: 'Internal server error' | ||
}) | ||
} | ||
|
||
} | ||
|
||
} | ||
export default BookmarkController; |
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 |
---|---|---|
|
@@ -35,4 +35,4 @@ module.exports = { | |
down: (queryInterface, Sequelize) => { | ||
return queryInterface.dropTable('Reports'); | ||
} | ||
}; | ||
}; |
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 |
---|---|---|
|
@@ -39,4 +39,3 @@ module.exports = { | |
}), | ||
down: queryInterface => queryInterface.dropTable('Bookmarks'), | ||
}; | ||
|
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 |
---|---|---|
|
@@ -23,4 +23,3 @@ module.exports = (sequelize, DataTypes) => { | |
}; | ||
return Bookmark; | ||
}; | ||
|
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 |
---|---|---|
|
@@ -23,4 +23,3 @@ module.exports = (sequelize, DataTypes) => { | |
}; | ||
return CommentLike; | ||
}; | ||
|
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 |
---|---|---|
|
@@ -42,4 +42,4 @@ module.exports = (sequelize, DataTypes) => { | |
}); | ||
}; | ||
return highlightComment; | ||
}; | ||
}; |
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 |
---|---|---|
|
@@ -7,4 +7,4 @@ module.exports = (sequelize, DataTypes) => { | |
Report.associate = function(models) { | ||
}; | ||
return Report; | ||
}; | ||
}; |
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 |
---|---|---|
|
@@ -14,4 +14,4 @@ module.exports = (sequelize, DataTypes) => { | |
}); | ||
}; | ||
return UserReport; | ||
}; | ||
}; |
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 @@ | ||
import express from 'express'; | ||
import BookmarkController from '../controllers/bookmarkController'; | ||
import verify from '../helpers/verifyToken'; | ||
import findItem from '../helpers/findItem'; | ||
|
||
|
||
const router = express.Router(); | ||
|
||
router.post('/articles/bookmark/:slug', verify, findItem.findArticle, BookmarkController.bookmarkArticles); | ||
|
||
export default router; |
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,61 @@ | ||
import chai from 'chai'; | ||
import chaiHttp from 'chai-http'; | ||
import app from '../src/server'; | ||
|
||
chai.should(); | ||
chai.use(chaiHttp); | ||
const { expect } = chai; | ||
|
||
let testToken; | ||
|
||
const user = { | ||
email: 'john.doe@andela.com', | ||
password: 'password', | ||
}; | ||
|
||
describe('BookmarkController', () => { | ||
it('should enable a user to bookmark an article', (done) => { | ||
chai.request(app).post('/api/v1/auth/login') | ||
.send(user) | ||
.end((err, res) => { | ||
testToken = res.body.token; | ||
chai.request(app).post('/api/v1/articles/bookmark/article') | ||
.set('token', testToken) | ||
.end((err, res) => { | ||
res.should.have.status(200); | ||
expect(res.body.message).to.equal('You just bookmarked this article'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
it('should enable a user to unbookmark an article', (done) => { | ||
chai.request(app).post('/api/v1/auth/login') | ||
.send(user) | ||
.end((err, res) => { | ||
testToken = res.body.token; | ||
chai.request(app).post('/api/v1/articles/bookmark/article') | ||
.set('token', testToken) | ||
.end((err, res) => { | ||
res.should.have.status(200); | ||
expect(res.body.message).to.equal('You just unbookmarked this article'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
it('should indicate that an article does not exist', (done) => { | ||
chai.request(app).post('/api/v1/auth/login') | ||
.send(user) | ||
.end((err, res) => { | ||
testToken = res.body.token; | ||
chai.request(app).post('/api/v1/articles/bookmark/art') | ||
.set('token', testToken) | ||
.end((err, res) => { | ||
res.should.have.status(404); | ||
expect(res.body.message).equal('Article not found'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); |