Skip to content

Commit

Permalink
feat: remove pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
marklai1998 committed Jul 7, 2023
1 parent 512cf72 commit 4014828
Show file tree
Hide file tree
Showing 16 changed files with 30 additions and 181 deletions.
3 changes: 0 additions & 3 deletions src/constants/pagination.ts

This file was deleted.

10 changes: 2 additions & 8 deletions src/modules/fares/fares.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Controller, Get, Query } from '@nestjs/common'

import { defaultLimit, defaultOffset } from '../../constants/pagination.js'
import { ListFaresQueryDto } from './fares.dto.js'
import { FaresService } from './fares.service.js'

Expand All @@ -11,13 +10,8 @@ export class FaresController {
@Get()
listFares(
@Query()
{
from,
to,
offset = defaultOffset,
limit = defaultLimit,
}: ListFaresQueryDto
{ from, to }: ListFaresQueryDto
) {
return this.fareService.listFares({ from, to, offset, limit })
return this.fareService.listFares({ from, to })
}
}
22 changes: 2 additions & 20 deletions src/modules/fares/fares.dto.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { ArgsType, Field, Int } from '@nestjs/graphql'
import { Type } from 'class-transformer'
import { IsEnum, IsInt, IsOptional, Max, Min } from 'class-validator'
import { ArgsType, Field } from '@nestjs/graphql'
import { IsEnum, IsOptional } from 'class-validator'
import { StopCode } from 'mtr-kit'

import { defaultMaxLimit } from '../../constants/pagination.js'

@ArgsType()
export class ListFaresQueryDto {
@IsEnum(StopCode)
Expand All @@ -16,19 +13,4 @@ export class ListFaresQueryDto {
@IsOptional()
@Field(() => StopCode, { nullable: true })
to?: StopCode

@Type(() => Number)
@IsInt()
@Min(0)
@IsOptional()
@Field(() => Int, { nullable: true })
offset?: number

@Type(() => Number)
@IsInt()
@Max(defaultMaxLimit)
@Min(0)
@IsOptional()
@Field(() => Int, { nullable: true })
limit?: number
}
10 changes: 2 additions & 8 deletions src/modules/fares/fares.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Args, Query, Resolver } from '@nestjs/graphql'

import { defaultLimit, defaultOffset } from '../../constants/pagination.js'
import { ListFaresQueryDto } from './fares.dto.js'
import { Fare } from './fares.model.js'
import { FaresService } from './fares.service.js'
Expand All @@ -12,13 +11,8 @@ export class FaresResolver {
@Query(() => [Fare])
fares(
@Args()
{
from,
to,
offset = defaultOffset,
limit = defaultLimit,
}: ListFaresQueryDto
{ from, to }: ListFaresQueryDto
) {
return this.faresService.listFares({ from, to, offset, limit })
return this.faresService.listFares({ from, to })
}
}
11 changes: 3 additions & 8 deletions src/modules/lines/lines.controller.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { Controller, Get, Param, Query } from '@nestjs/common'
import { Controller, Get, Param } from '@nestjs/common'
import { LineCode, StopCode } from 'mtr-kit'

import { defaultLimit, defaultOffset } from '../../constants/pagination.js'
import { SchedulesService } from '../schedules/schedules.service.js'
import { ListLinesQueryDto } from './lines.dto.js'
import { LinesService } from './lines.service.js'

@Controller('/api/v1/lines')
Expand All @@ -14,11 +12,8 @@ export class LinesController {
) {}

@Get()
listLines(
@Query()
{ offset = defaultOffset, limit = defaultLimit }: ListLinesQueryDto
) {
return this.linesService.listLines({ offset, limit })
listLines() {
return this.linesService.listLines()
}

@Get(':line')
Expand Down
23 changes: 0 additions & 23 deletions src/modules/lines/lines.dto.ts

This file was deleted.

9 changes: 2 additions & 7 deletions src/modules/lines/lines.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { Args, Parent, Query, ResolveField, Resolver } from '@nestjs/graphql'
import { LineCode, StopCode } from 'mtr-kit'

import { defaultLimit, defaultOffset } from '../../constants/pagination.js'
import { Schedule } from '../schedules/schedules.model.js'
import { SchedulesService } from '../schedules/schedules.service.js'
import { ListLinesQueryDto } from './lines.dto.js'
import { Line, LineStop } from './lines.model.js'
import { LinesService } from './lines.service.js'

Expand All @@ -13,11 +11,8 @@ export class LinesResolver {
constructor(private readonly linesService: LinesService) {}

@Query(() => [Line])
lines(
@Args()
{ offset = defaultOffset, limit = defaultLimit }: ListLinesQueryDto
) {
return this.linesService.listLines({ offset, limit })
lines() {
return this.linesService.listLines()
}

@Query(() => Line)
Expand Down
10 changes: 3 additions & 7 deletions src/modules/lines/lines.service.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import { Injectable } from '@nestjs/common'
import { LineCode, StopCode, lineMap, lines } from 'mtr-kit'
import { drop, omit, take } from 'ramda'
import { omit } from 'ramda'

@Injectable()
export class LinesService {
listLines({ offset, limit }: { offset?: number; limit?: number }) {
const res = lines.map(omit(['stops']))

const withOffset = offset ? drop(offset, res) : res
const withLimit = limit ? take(limit, withOffset) : withOffset
return withLimit
listLines() {
return lines.map(omit(['stops']))
}

getLine({ line }: { line: LineCode }) {
Expand Down
10 changes: 2 additions & 8 deletions src/modules/schedules/schedules.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Controller, Get, Query } from '@nestjs/common'

import { defaultLimit, defaultOffset } from '../../constants/pagination.js'
import { ListSchedulesQueryDto } from './schedules.dto.js'
import { SchedulesService } from './schedules.service.js'

Expand All @@ -11,13 +10,8 @@ export class SchedulesController {
@Get()
listSchedules(
@Query()
{
line,
stop,
offset = defaultOffset,
limit = defaultLimit,
}: ListSchedulesQueryDto
{ line, stop }: ListSchedulesQueryDto
) {
return this.schedulesService.listSchedules({ line, stop, offset, limit })
return this.schedulesService.listSchedules({ line, stop })
}
}
22 changes: 2 additions & 20 deletions src/modules/schedules/schedules.dto.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { ArgsType, Field, Int } from '@nestjs/graphql'
import { Type } from 'class-transformer'
import { IsEnum, IsInt, IsOptional, Max, Min } from 'class-validator'
import { ArgsType, Field } from '@nestjs/graphql'
import { IsEnum, IsOptional } from 'class-validator'
import { LineCode, StopCode } from 'mtr-kit'

import { defaultMaxLimit } from '../../constants/pagination.js'

@ArgsType()
export class ListSchedulesQueryDto {
@IsEnum(LineCode)
Expand All @@ -16,19 +13,4 @@ export class ListSchedulesQueryDto {
@IsOptional()
@Field(() => StopCode, { nullable: true })
stop?: StopCode

@Type(() => Number)
@IsInt()
@Min(0)
@IsOptional()
@Field(() => Int, { nullable: true })
offset?: number

@Type(() => Number)
@IsInt()
@Max(defaultMaxLimit)
@Min(0)
@IsOptional()
@Field(() => Int, { nullable: true })
limit?: number
}
10 changes: 2 additions & 8 deletions src/modules/schedules/schedules.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Args, Query, Resolver } from '@nestjs/graphql'

import { defaultLimit, defaultOffset } from '../../constants/pagination.js'
import { ListSchedulesQueryDto } from './schedules.dto.js'
import { Schedule } from './schedules.model.js'
import { SchedulesService } from './schedules.service.js'
Expand All @@ -12,13 +11,8 @@ export class SchedulesResolver {
@Query(() => [Schedule])
schedules(
@Args()
{
line,
stop,
offset = defaultOffset,
limit = defaultLimit,
}: ListSchedulesQueryDto
{ line, stop }: ListSchedulesQueryDto
) {
return this.schedulesService.listSchedules({ line, stop, offset, limit })
return this.schedulesService.listSchedules({ line, stop })
}
}
19 changes: 2 additions & 17 deletions src/modules/schedules/schedules.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Injectable } from '@nestjs/common'
import { LineCode, StopCode, lineMap, lines } from 'mtr-kit'
import { drop, take } from 'ramda'

import { NormalizedSchedule, scheduleMap } from '../../worker.js'

Expand All @@ -23,18 +22,8 @@ export class SchedulesService {
.filter((v): v is NonNullable<typeof v> => Boolean(v))
}

listSchedules({
line,
stop,
offset,
limit,
}: {
line?: LineCode
stop?: StopCode
offset?: number
limit?: number
}) {
const res = lines
listSchedules({ line, stop }: { line?: LineCode; stop?: StopCode }) {
return lines
.reduce<({ line: LineCode; stop: StopCode } & NormalizedSchedule)[]>(
(acc, item) => [
...acc,
Expand All @@ -48,9 +37,5 @@ export class SchedulesService {
.filter(
item => (!line || item.line === line) && (!stop || item.stop === stop)
)

const withOffset = offset ? drop(offset, res) : res
const withLimit = limit ? take(limit, withOffset) : withOffset
return withLimit
}
}
11 changes: 3 additions & 8 deletions src/modules/stops/stops.controller.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { Controller, Get, Param, Query } from '@nestjs/common'
import { Controller, Get, Param } from '@nestjs/common'
import { StopCode } from 'mtr-kit'

import { defaultLimit, defaultOffset } from '../../constants/pagination.js'
import { FaresService } from '../fares/fares.service.js'
import { SchedulesService } from '../schedules/schedules.service.js'
import { ListStopsQueryDto } from './stops.dto.js'
import { StopsService } from './stops.service.js'

@Controller('/api/v1/stops')
Expand All @@ -16,11 +14,8 @@ export class StopsController {
) {}

@Get()
listStops(
@Query()
{ offset = defaultOffset, limit = defaultLimit }: ListStopsQueryDto
) {
return this.stopsService.listStop({ offset, limit })
listStops() {
return this.stopsService.listStop()
}

@Get(':stop')
Expand Down
23 changes: 0 additions & 23 deletions src/modules/stops/stops.dto.ts

This file was deleted.

9 changes: 2 additions & 7 deletions src/modules/stops/stops.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { Args, Parent, Query, ResolveField, Resolver } from '@nestjs/graphql'
import { StopCode } from 'mtr-kit'

import { defaultLimit, defaultOffset } from '../../constants/pagination.js'
import { Fare } from '../fares/fares.model.js'
import { FaresService } from '../fares/fares.service.js'
import { ListLinesQueryDto } from '../lines/lines.dto.js'
import { LineBase } from '../lines/lines.model.js'
import { Schedule } from '../schedules/schedules.model.js'
import { SchedulesService } from '../schedules/schedules.service.js'
Expand All @@ -20,11 +18,8 @@ export class StopsResolver {
) {}

@Query(() => [Stop])
stops(
@Args()
{ offset = defaultOffset, limit = defaultLimit }: ListLinesQueryDto
) {
return this.stopsService.listStop({ offset, limit })
stops() {
return this.stopsService.listStop()
}

@Query(() => Stop)
Expand Down
9 changes: 3 additions & 6 deletions src/modules/stops/stops.service.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import { Injectable } from '@nestjs/common'
import { StopCode, lines, stopMap, stops } from 'mtr-kit'
import { drop, omit, take } from 'ramda'
import { omit } from 'ramda'

@Injectable()
export class StopsService {
listStop({ offset, limit }: { offset?: number; limit?: number }) {
const res = stops
const withOffset = offset ? drop(offset, res) : res
const withLimit = limit ? take(limit, withOffset) : withOffset
return withLimit
listStop() {
return stops
}

getStop({ stop }: { stop: StopCode }) {
Expand Down

0 comments on commit 4014828

Please sign in to comment.