Skip to content

Commit

Permalink
feat: add support for text pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
eamon0989 committed Apr 14, 2023
1 parent 6724fb8 commit 91e6bf6
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 17 deletions.
2 changes: 2 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* eslint-disable import/prefer-default-export */
export const DEFAULT_OFFSET = 10;
20 changes: 15 additions & 5 deletions src/data-access/texts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { QueryResult } from 'pg';
import dbQuery from '../model/db-query';
import { Text } from '../types';
import { DEFAULT_OFFSET } from '../constants';

const getAll = async function (): Promise<QueryResult> {
const ALL_TEXTS: string = `
Expand All @@ -24,14 +25,23 @@ const getById = async function (textId: number): Promise<QueryResult> {

const getByUserAndLanguage = async function (
userId: number,
languageId: string
languageId: string,
page: number
): Promise<QueryResult> {
const TEXTS_BY_USER: string = `
SELECT * FROM texts
WHERE user_id = %L AND language_id = %L
ORDER BY upload_time DESC NULLS LAST`;
SELECT *, COUNT(*) OVER () as totalTexts
FROM texts
WHERE user_id = %L AND language_id = %L
ORDER BY upload_time DESC NULLS LAST
OFFSET %L FETCH NEXT %L ROWS ONLY`;

const result = await dbQuery(TEXTS_BY_USER, userId, languageId);
const result = await dbQuery(
TEXTS_BY_USER,
userId,
languageId,
DEFAULT_OFFSET * page,
DEFAULT_OFFSET
);

return result;
};
Expand Down
14 changes: 7 additions & 7 deletions src/routes/texts.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import express from 'express';
import texts from '../services/texts';
import users from '../services/users';
import { Text } from '../types';
import { TextPagination, Text } from '../types';

const router: express.Router = express.Router();

router.get('/language/:languageId/', async (req, res): Promise<void> => {
router.get('/language/:languageId/:page/', async (req, res): Promise<void> => {
const { user } = res.locals;
const { languageId } = req.params;

const allTexts: Array<Text> = await texts.getByUserAndLanguage(
const { languageId, page } = req.params;
const textPagination: TextPagination = await texts.getByUserAndLanguage(
Number(user.id),
languageId
languageId,
page
);
res.json(allTexts);
res.json(textPagination);
});

router.get('/:id', async (req, res): Promise<void> => {
Expand Down
22 changes: 17 additions & 5 deletions src/services/texts.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import boom from '@hapi/boom';
import { QueryResult } from 'pg';
import textData from '../data-access/texts';
import { convertTextTypes, Text, TextDB } from '../types';
import { convertTextTypes, TextPagination, Text, TextDB } from '../types';
import { DEFAULT_OFFSET } from '../constants';

const getAll = async function (): Promise<Array<Text>> {
const result: QueryResult = await textData.getAll();
Expand All @@ -20,14 +21,25 @@ const getById = async function (textId: number): Promise<Text> {

const getByUserAndLanguage = async function (
userId: number,
languageId: string
): Promise<Array<Text>> {
languageId: string,
pageNumber: string
): Promise<TextPagination> {
const result: QueryResult = await textData.getByUserAndLanguage(
userId,
languageId
languageId,
Number(pageNumber) - 1
);

return result.rows.map((dbItem: TextDB) => convertTextTypes(dbItem));
const totalTexts = result.rows[0]?.totaltexts || 0;
const paginatedTexts: TextPagination = {
currentPage: Number(pageNumber),
totalPages: Math.ceil(totalTexts / DEFAULT_OFFSET),
totalTexts,
nextPage: Number(pageNumber) + 1,
prevPage: Number(pageNumber) - 1,
data: result.rows.map((dbItem: TextDB) => convertTextTypes(dbItem)),
};
return paginatedTexts;
};

const addNew = async function (textObject: Text): Promise<Text> {
Expand Down

0 comments on commit 91e6bf6

Please sign in to comment.