Skip to content

Commit

Permalink
feature(pagination):add support for pagination for novels list
Browse files Browse the repository at this point in the history
[Delivers #167164995]
  • Loading branch information
Ukhu committed Aug 16, 2019
1 parent 5474b7c commit b2f739c
Show file tree
Hide file tree
Showing 13 changed files with 615 additions and 20 deletions.
112 changes: 110 additions & 2 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,114 @@ paths:
409:
description: resource already exists
500:
description: internal server error
description: internal server error
get:
tags:
- Novels
summary: fetches novels with pagination
description: fetches novels from the database with pagination
parameters:
- name: page
in: query
required: true
schema:
type: integer
- name: limit
in: query
required: true
schema:
type: integer
responses:
200:
description: succesfully fetched novels
content:
application/json:
schema:
type: object
properties:
message:
type: string
example: succesfully returned novels
data:
type: array
items:
type: object
properties:
id:
type: integer
example: 1
title:
type: string
example: We Are Here Again
slug:
type: string
example: we-are-here-again
author:
type: string
example: Eden Hazard
genre:
type: string
example: action
description:
type: string
example: Reminder of our presence again
body:
type: string
example: Lorem ipsum dolor sit amet, consectetur adipiscing elit
createdAt:
type: string
example: '2019-08-07T00:53:02.535Z'
updatedAt:
type: string
example: '2019-08-07T00:53:02.535Z'
400:
description: bad request
content:
application/json:
schema:
type: object
properties:
errors:
type: array
items:
type: object
properties:
field:
type: string
example: page
message:
type: string
example: page cannot be empty
401:
description: unauthorized access
content:
application/json:
schema:
type: object
properties:
error:
type: string
example: invalid token
404:
description: entity not found
content:
application/json:
schema:
type: object
properties:
error:
type: string
example: page not found
500:
description: server error
content:
application/json:
schema:
type: object
properties:
error:
type: string
example: error occured
/novels/{slug}/like:
post:
tags:
Expand Down Expand Up @@ -145,7 +252,6 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/StandardServerResponse'

/users/login:
post:
tags:
Expand Down Expand Up @@ -225,6 +331,8 @@ paths:

/auth/forgotpassword:
post:
tags:
- Auth
summary: Forgot password
description: Sends an email to a user with the reset password link
requestBody:
Expand Down
43 changes: 40 additions & 3 deletions src/controllers/novelController.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import services from '../services';
import helpers from '../helpers';
import models from '../database/models';

const { novelServices: { createNewNovel } } = services;
const { errorResponse, successResponse } = helpers;
const { novelServices: { createNewNovel, findAllNovels } } = services;
const {
errorResponse, successResponse, extractNovels, responseMessage
} = helpers;
const { Novel } = models;

/**
* createNovel
Expand All @@ -25,4 +29,37 @@ const createNovel = async (req, res) => {
});
};

export default createNovel;
/**
* getNovels
*
* @param {object} request
* @param {object} response
* @returns {object} json
*/
const getNovels = async (request, response) => {
const { page = 1, limit = 20 } = request.query;
try {
const { count } = await Novel.findAndCountAll();
if (!count) {
return responseMessage(response, 404, { message: 'no novels found in database', data: [] });
}
const pages = Math.ceil(count / limit);
if (page > pages) {
return responseMessage(response, 404, { error: 'page not found' });
}
const offset = limit * (page - 1);
const results = await findAllNovels(offset, limit);
const novels = extractNovels(results);
response.status(200).json({
message: 'succesfully returned novels',
currentPage: page,
totalPages: pages,
limit,
data: novels
});
} catch (error) {
responseMessage(response, 500, { error: error.message });
}
};

export default { createNovel, getNovels };
Loading

0 comments on commit b2f739c

Please sign in to comment.