Skip to content

Commit

Permalink
feat(pagination): Implementating pagination for articles (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
eliemugenzi authored and Quantum-35 committed Jun 20, 2019
1 parent d31b882 commit 199a486
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 93 deletions.
18 changes: 18 additions & 0 deletions src/api/controllers/articlesController.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,24 @@ class articlesController {
* @return {object} returns array of articles
*/
static async getAllArticle(req, res) {
const { page, limit } = req.query;
const pageNumber = parseInt(page, 10);
const limitNumber = parseInt(limit, 10);
if (typeof pageNumber === 'number' && typeof limitNumber === 'number' && typeof page !== 'undefined' && typeof limit !== 'undefined') {
if (pageNumber <= 0 || limitNumber <= 0) {
return res.status(400).json({
error: 'Invalid request'
});
}
const offset = limitNumber * (pageNumber - 1);
const foundArticles = await Article.findAll({
limit: limitNumber,
offset
});
return res.json({
data: foundArticles
});
}
const allArticle = await articles.getAllArticle();

if (!allArticle[0]) return res.status(404).send({ error: 'Whoops! No Articles found!' });
Expand Down
1 change: 1 addition & 0 deletions src/api/routes/authRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ authRouter.get('/login/facebook/redirect', passport.authenticate('facebook', { s
authRouter.get('/login/twitter', passport.authenticate('twitter', { scope: ['profile', 'email'] }));
authRouter.get('/login/twitter/redirect', passport.authenticate('twitter', { session: false }), twitter, socialLogin.twitterLogin);

authRouter.get('/signout', verifyToken, dropToken, SignOut);
authRouter.post('/signup', validateBody('signup'), validateGender, usernameExists, emailExists, register);
authRouter.post('/login', validateBody('login'), login);
authRouter.get('/verify', verifyAccount);
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@ dotenv.config();
const port = process.env.PORT || 3000;
const app = express();

globalMiddleware(app);

app.use(session({
secret: process.env.SECRET,
saveUninitialized: true
}));
app.use(passport.initialize());
app.use(passport.session());

globalMiddleware(app);
app.use('/api', api);
app.use('/docs', swaggerUi.serve, swaggerUi.setup(swaggerDoc));
app.use('*', (req, res) => {
Expand All @@ -33,6 +32,7 @@ app.use('*', (req, res) => {
});
});


sequelize.sync().then(() => {
app.listen(port, () => {
// eslint-disable-next-line no-console
Expand Down
1 change: 0 additions & 1 deletion src/sequelize/migrations/20190612172050-create-user.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@


module.exports = {
up: (queryInterface, Sequelize) => queryInterface.createTable('Users', {
id: {
Expand Down
3 changes: 1 addition & 2 deletions src/sequelize/models/user.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/* eslint-disable func-names */


module.exports = (sequelize, DataTypes) => {
const User = sequelize.define('User', {
firstName: DataTypes.STRING,
Expand All @@ -16,6 +14,7 @@ module.exports = (sequelize, DataTypes) => {
socialId: DataTypes.STRING,
verified: DataTypes.BOOLEAN
}, {});

User.associate = function (models) {
// associations can be defined here
User.hasMany(models.Article, {
Expand Down
Loading

0 comments on commit 199a486

Please sign in to comment.