Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Implement use of Pagination DTO #705

Open
GHkrishna opened this issue May 2, 2024 · 0 comments
Open

refactor: Implement use of Pagination DTO #705

GHkrishna opened this issue May 2, 2024 · 0 comments
Labels
good first issue Good for newcomers

Comments

@GHkrishna
Copy link
Contributor

About

  • We have implemented pagination DTO [libs/common/src/dtos/pagination.dto.ts]
  • This DTO is used to sort and get the required data as per the need
  • It is used in Get endpoints

Current behavior

  • There are some instances [apps/api-gateway/src/connection/dtos/get-all-connections.dto.ts] where pagination is separately implemented.
  • Here's an example
export class GetAllConnectionsDto {
  @ApiProperty({ required: false, example: '1' })
  @Transform(({ value }) => toNumber(value))
  @IsOptional()
  pageNumber: number = 1;

  @ApiProperty({ required: false, example: '10' })
  @IsOptional()
  @Transform(({ value }) => toNumber(value))
  @Min(1, { message: 'Page size must be greater than 0' })
  @Max(100, { message: 'Page size must be less than 100' })
  pageSize: number = 10;

  @ApiProperty({ required: false })
  @IsOptional()
  @Transform(({ value }) => trim(value))
  @Type(() => String)
  searchByText: string = '';

  @ApiProperty({
    required: false
  })
  @Transform(({ value }) => trim(value))
  @IsOptional()
  @IsEnum(SortFields)
  sortField: string = SortFields.CREATED_DATE_TIME;

  @ApiProperty({
    enum: [SortValue.DESC, SortValue.ASC],
    required: false
  })
  @Transform(({ value }) => trim(value))
  @IsOptional()
  @IsEnum(SortValue)
  sortBy: string = SortValue.DESC;
} 

Expected behavior

Instead a better way would be to extend and make use of the already existing PaginationDto.
So the above implementation would become something like:

export class GetAllConnectionsDto extends PaginationDto {
  @ApiProperty({
    required: false
  })
  @Transform(({ value }) => trim(value))
  @IsOptional()
  @IsEnum(SortFields)
  sortField: string = SortFields.CREATED_DATE_TIME;

  @ApiProperty({
    enum: [SortValue.DESC, SortValue.ASC],
    required: false
  })
  @Transform(({ value }) => trim(value))
  @IsOptional()
  @IsEnum(SortValue)
  sortBy: string = SortValue.DESC;
} 

Note

  • The changes made on DTO level (in variable names, if any) in API-gateway should also be reflected all the way to the individual service where the actual parameters will be used for a db call or elsewhere.
  • Eg.: For the above DTO [GetAllConnectionsDto], the changes made for function [async getConnections] in [apps/api-gateway/src/connection/connection.controller.ts] would change the variable 'searchByText' to 'search'.
  • Hence the changes would be in following files:
    • [apps/api-gateway/src/connection/connection.controller.ts] and respective Dto 'GetAllConnectionsDto' in [apps/api-gateway/src/connection/dtos/get-all-connections.dto.ts]
    • [apps/api-gateway/src/connection/connection.service.ts] ans the respective interface 'IConnectionSearchCriteria' in [apps/api-gateway/src/interfaces/IConnectionSearch.interface.ts]
    • [apps/connection/src/connection.controller.ts] and the respective interface 'IFetchConnections' in [apps/connection/src/interfaces/connection.interfaces.ts]
    • [apps/connection/src/connection.service.ts] and the respective interface 'IConnectionSearchCriteria' in [apps/connection/src/interfaces/connection.interfaces.ts]
@GHkrishna GHkrishna added the good first issue Good for newcomers label May 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

1 participant