Skip to content

Commit

Permalink
feat: line stop api
Browse files Browse the repository at this point in the history
  • Loading branch information
marklai1998 committed Jun 27, 2023
1 parent 4a5fb32 commit 3a8d994
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 29 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"dayjs": "1.11.8",
"koa": "2.14.2",
"koa-logger": "3.2.1",
"mtr-kit": "1.9.1",
"mtr-kit": "1.10.0",
"promise-throttle": "1.1.2",
"tsx": "3.12.7"
},
Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 13 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import cors from '@koa/cors'
import Router from '@koa/router'
import Koa from 'koa'
import logger from 'koa-logger'
import { LineCode, StopCode, lines, stops } from 'mtr-kit'
import { LineCode, StopCode, lineMap, lines, stopMap, stops } from 'mtr-kit'

import { scheduleService } from './services/scheduleService.js'

Expand Down Expand Up @@ -38,7 +38,12 @@ router.get('/api/v1/stops', async ctx => {

router.get('/api/v1/stops/:stop', async ctx => {
const { stop } = ctx.params
ctx.body = stops.find(item => item.code === stop)
ctx.body = stopMap[stop as StopCode]
})

router.get('/api/v1/stops/:stop/schedules', async ctx => {
const { stop } = ctx.params
ctx.body = scheduleService.getStopSchedules(stop as StopCode)
})

// Line API
Expand All @@ -49,22 +54,22 @@ router.get('/api/v1/lines', async ctx => {

router.get('/api/v1/lines/:line', async ctx => {
const { line } = ctx.params
ctx.body = lines.find(item => item.code === line)
ctx.body = lineMap[line as LineCode]
})

router.get('/api/v1/lines/:line/stops', async ctx => {
router.get('/api/v1/lines/:line/schedules', async ctx => {
const { line } = ctx.params
ctx.body = lines.find(item => item.code === line)?.stops
ctx.body = scheduleService.getLineSchedule(line as LineCode)
})

router.get('/api/v1/lines/:line/schedules', async ctx => {
router.get('/api/v1/lines/:line/stops', async ctx => {
const { line } = ctx.params
ctx.body = await scheduleService.getLineSchedule(line as LineCode)
ctx.body = lineMap[line as LineCode]?.stops
})

router.get('/api/v1/lines/:line/stops/:stop/schedules', async ctx => {
const { line, stop } = ctx.params
ctx.body = scheduleService.getStopSchedules(
ctx.body = scheduleService.getLineStopSchedule(
line as LineCode,
stop as StopCode
)
Expand Down
42 changes: 29 additions & 13 deletions src/services/scheduleService.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,49 @@
import { LineCode, StopCode, lines } from 'mtr-kit'
import { LineCode, StopCode, lineMap, lines } from 'mtr-kit'

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

const getStopSchedules = (line: LineCode, stop: StopCode) =>
const getLineStopSchedule = (line: LineCode, stop: StopCode) =>
scheduleMap.get(`${line}-${stop}`)

const getLineSchedule = (code: LineCode) => {
const stops = lines.find(item => item.code === code)?.stops || []
return stops
const getStopSchedules = (stop: StopCode): ({ line: LineCode } & Schedule)[] =>
lines
.filter(line => line.stops.includes(stop))
.map(line => {
const schedule = getLineStopSchedule(line.code, stop)
return schedule
? {
line: line.code,
...schedule,
}
: null
})
.filter((v): v is NonNullable<typeof v> => Boolean(v))

const getLineSchedule = (code: LineCode): ({ stop: StopCode } & Schedule)[] =>
lineMap[code].stops
.map(stop => {
const schedule = getStopSchedules(code, stop.code)
const schedule = getLineStopSchedule(code, stop)
return schedule
? {
code: stop.code,
stop,
...schedule,
}
: null
})
.filter((v): v is NonNullable<typeof v> => Boolean(v))
}

const getSchedule = async () =>
lines.map(line => {
const schedule = getLineSchedule(line.code)
return { code: line.code, stops: schedule }
})
lines.reduce<({ line: LineCode; stop: StopCode } & Schedule)[]>(
(acc, line) => [
...acc,
...getLineSchedule(line.code).map(item => ({ line: line.code, ...item })),
],
[]
)

export const scheduleService = {
getLineSchedule,
getSchedule,
getLineStopSchedule,
getStopSchedules,
}
4 changes: 1 addition & 3 deletions src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,7 @@ if (isMainThread) {

const loop = async (ignoreUndefined: boolean) => {
const lineStops = lines
.map(({ code, stops }) =>
stops.map(stop => ({ line: code, stop: stop.code }))
)
.map(({ code, stops }) => stops.map(stop => ({ line: code, stop })))
.flat()
.map(({ stop, line }) => {
const lastSchedule = threadMap.get(`${line}-${stop}`)
Expand Down

0 comments on commit 3a8d994

Please sign in to comment.