Skip to content

Commit

Permalink
refactor(query.builder.ts): use Pagination class in paginate method
Browse files Browse the repository at this point in the history
feat(pagination.ts): remove IPagination parameter from constructor and add page and limit parameters
refactor(movie.service.ts): remove unused import of Pagination class
  • Loading branch information
mdwitr0 committed Jun 22, 2023
1 parent 1f1685b commit fbe183e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 16 deletions.
6 changes: 4 additions & 2 deletions src/builder/query.builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { SPECIAL_VALUE } from '../enums/special-value.enum';
import { IQueryFields } from '../interfaces/query-fields.interface';
import { MovieFields } from '../types/movie-fields.type';
import { PersonFields } from '../types/person-fields.type';
import { Pagination } from '../classes/pagination';

export abstract class QueryBuilder<T extends IQueryFields> {
protected query: any;
Expand Down Expand Up @@ -65,8 +66,9 @@ export abstract class QueryBuilder<T extends IQueryFields> {
}

paginate(page: number, limit: number): this {
this.query.page = page;
this.query.limit = limit;
const pagination = new Pagination(page, limit);
this.query.page = pagination.page;
this.query.limit = pagination.limit;
return this;
}

Expand Down
20 changes: 7 additions & 13 deletions src/classes/pagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,12 @@ import { IPagination } from '../interfaces/pagination.interface';
export class Pagination implements IPagination {
public page: number;
public limit: number;
constructor(pagination: IPagination) {
if (!pagination) {
this.page = 1;
this.limit = 10;
} else {
const { page, limit } = pagination;
if (page && page < 1) throw new Error('Page must be greater than 0');
if (limit && limit < 1) throw new Error('Limit must be greater than 0');
if (limit && limit > 250)
throw new Error('Limit must be less than or equal to 250');
this.page = page || 1;
this.limit = limit || 10;
}
constructor(page: number, limit: number) {
if (page && page < 1) throw new Error('Page must be greater than 0');
if (limit && limit < 1) throw new Error('Limit must be greater than 0');
if (limit && limit > 250)
throw new Error('Limit must be less than or equal to 250');
this.page = page || 1;
this.limit = limit || 10;
}
}
1 change: 0 additions & 1 deletion src/services/movie.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
SearchMovieResponseDto,
} from '../interfaces/api.interface';
import { IPagination } from '../interfaces/pagination.interface';
import { Pagination } from '../classes/pagination';
import { VERSIONS } from '../enums/version.enum';
import { IResponseError } from '../interfaces/error.interface';

Expand Down

0 comments on commit fbe183e

Please sign in to comment.