Skip to content

Commit

Permalink
Merge 7c06793 into 7983c75
Browse files Browse the repository at this point in the history
  • Loading branch information
chokonaira committed Jul 18, 2019
2 parents 7983c75 + 7c06793 commit b4cd50c
Show file tree
Hide file tree
Showing 14 changed files with 134 additions and 9 deletions.
50 changes: 50 additions & 0 deletions src/controllers/bookmarkController.js
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;
2 changes: 1 addition & 1 deletion src/db/migrations/20190716060554-create-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ module.exports = {
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Reports');
}
};
};
2 changes: 1 addition & 1 deletion src/db/migrations/20190716062433-create-user-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ module.exports = {
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('UserReports');
}
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ module.exports = {
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('highlightComments');
}
};
};
1 change: 0 additions & 1 deletion src/db/migrations/30181008085230-create-bookmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,3 @@ module.exports = {
}),
down: queryInterface => queryInterface.dropTable('Bookmarks'),
};

1 change: 0 additions & 1 deletion src/db/models/bookmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,3 @@ module.exports = (sequelize, DataTypes) => {
};
return Bookmark;
};

1 change: 0 additions & 1 deletion src/db/models/commentLike.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,3 @@ module.exports = (sequelize, DataTypes) => {
};
return CommentLike;
};

2 changes: 1 addition & 1 deletion src/db/models/highlightcomment.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ module.exports = (sequelize, DataTypes) => {
});
};
return highlightComment;
};
};
2 changes: 1 addition & 1 deletion src/db/models/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ module.exports = (sequelize, DataTypes) => {
Report.associate = function(models) {
};
return Report;
};
};
4 changes: 4 additions & 0 deletions src/db/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ module.exports = (sequelize, DataTypes) => {
foreignKey: 'userId',
as: 'highlightComment'
});
User.hasMany(models.Rating, {
foreignKey: 'userId',
as: 'userRatings'
});
User.hasMany(models.CommentLike, {
foreignKey: 'userId',
as: 'userCommentsLike'
Expand Down
2 changes: 1 addition & 1 deletion src/db/models/userreport.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ module.exports = (sequelize, DataTypes) => {
});
};
return UserReport;
};
};
11 changes: 11 additions & 0 deletions src/routes/bookmarkRoute.js
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;
2 changes: 2 additions & 0 deletions src/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import likeRoute from './likeRoute';
import articleRoute from './articleRoute';
import commentRoute from './commentRoute';
import ratingRoute from './ratingRoute';
import bookmarkRoute from './bookmarkRoute'

const router = express.Router();

Expand All @@ -17,5 +18,6 @@ router.use('/api/v1/', articleRoute);
router.use('/api/v1', userRoute);
router.use('/api/v1/articles', commentRoute);
router.use('/api/v1', ratingRoute);
router.use('/api/v1', bookmarkRoute);

export default router;
61 changes: 61 additions & 0 deletions test/bookmark.test.js
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();
});
});
});
});

0 comments on commit b4cd50c

Please sign in to comment.