Skip to content

Implement PaginationService and Remove the Empty Misspelled Duplicate #156

@portableDD

Description

@portableDD

Description

PaginationService is declared as @Injectable() but has absolutely no methods. Both CoursesController and LessonsController call this.paginationService.findAll() and this.paginationService.findAllLessons() on it — these calls will throw TypeError: this.paginationService.findAll is not a function at runtime the moment either endpoint is hit.

There is also a misspelled duplicate: dto/paggination.dto.ts (double-g) which is completely empty and unused.


Proposed Actions

  • Implement a generic reusable PaginationService in common/services/pagination.service.ts with a paginate<T>(repository, options) method:
async paginate<T>(
  repository: Repository<T>,
  options: { page: number; limit: number },
): Promise<{
  data: T[];
  total: number;
  page: number;
  limit: number;
  totalPages: number;
}> {
  const [data, total] = await repository.findAndCount({
    skip: (options.page - 1) * options.limit,
    take: options.limit,
  });
  return {
    data,
    total,
    page: options.page,
    limit: options.limit,
    totalPages: Math.ceil(total / options.limit),
  };
}
  • Create a PaginationDto in common/dto/pagination.dto.ts with:
    • @IsOptional() @IsInt() @Min(1) @Type(() => Number) page?: number = 1
    • @IsOptional() @IsInt() @Min(1) @Max(100) @Type(() => Number) limit?: number = 10
  • Remove the duplicate GET /courses handler in CoursesController (see Issue Implement Role-Based Access Control (RBAC) for Students and Tutors #9) and add @Query() pagination params to the existing findAll() instead
  • Remove the duplicate GET /lessons handler in LessonsController (see Issue Implement JWT Token Generation for Tutor #10) and add @Query() pagination params to the appropriate handler instead
  • Delete the empty misspelled file dto/paggination.dto.ts

Expected Outcome

  • PaginationService has a working paginate() method usable by any controller
  • GET /courses and GET /lessons support ?page=1&limit=10 query params
  • No TypeError is thrown when pagination endpoints are called
  • The empty misspelled DTO file is gone

Complexity

Medium

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions