Skip to content

Commit

Permalink
feat(pagination): Implementating pagination for articles
Browse files Browse the repository at this point in the history
  • Loading branch information
Elie Mugenzi authored and Elie Mugenzi committed Jun 19, 2019
1 parent 3473a82 commit 24a7980
Show file tree
Hide file tree
Showing 12 changed files with 394 additions and 262 deletions.
8 changes: 8 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,11 @@ TWITTER_APP_SECRET=
GOOGLE_ID=
GOOGLE_SECRET=
SECRET_KEY =
LOCAL_DB_USER=
LOCAL_DB_PASSWORD=
LOCAL_DB_NAME=
TEST_DATABASE_URL=
AUTHOSHAVEN_USER=
AUTHOSHAVEN_PASS=
BASE_URL=
SECRET
18 changes: 18 additions & 0 deletions src/api/controllers/articlesController.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,24 @@ class articlesController {
}

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!' });
res.status(200).send({
Expand Down
2 changes: 1 addition & 1 deletion src/api/routes/authRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ 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.get('/verify', verifyAccount);
authRouter.post('/reset', RequestPasswordReset);
authRouter.get('/reset/:token', ConfirmPasswordReset);
authRouter.patch('/reset/:aprvToken', ApplyPasswordReset);

export default authRouter;
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ dotenv.config();
const port = process.env.PORT || 3000;
const app = express();

globalMiddleware(app);

app.use(session({
secret: process.env.SECRET,
Expand All @@ -26,7 +25,7 @@ app.use(session({

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 @@ -35,6 +34,7 @@ app.use('*', (req, res) => {
});
});


sequelize.sync().then(() => {
app.listen(port, () => {
// eslint-disable-next-line no-console
Expand Down
57 changes: 0 additions & 57 deletions src/sequelize/migrations/20190612225745-create-user.js

This file was deleted.

57 changes: 0 additions & 57 deletions src/sequelize/migrations/20190613144226-create-user.js

This file was deleted.

55 changes: 0 additions & 55 deletions src/sequelize/migrations/20190617175548-create-user.js

This file was deleted.

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 24a7980

Please sign in to comment.