Skip to content

Commit 91aafcc

Browse files
committed
feat(be): route endpoint
1 parent 247f360 commit 91aafcc

File tree

5 files changed

+126
-2
lines changed

5 files changed

+126
-2
lines changed

apps/backend/src/app.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { ImportModule } from "src/modules/import/import.module";
1414
import { LoggerModule } from "src/modules/logger/logger.module";
1515
import { PlatformModule } from "src/modules/platform/platform.module";
1616
import { PrismaModule } from "src/modules/prisma/prisma.module";
17+
import { RouteModule } from "src/modules/route/route.module";
1718
import { StatusModule } from "src/modules/status/status.module";
1819
import { StopModule } from "src/modules/stop/stop.module";
1920

@@ -27,6 +28,7 @@ import { StopModule } from "src/modules/stop/stop.module";
2728
LoggerModule,
2829
StatusModule,
2930
GtfsModule,
31+
RouteModule,
3032
ConfigModule.forRoot(configModuleConfig),
3133
ScheduleModule.forRoot(),
3234
CacheModule.registerAsync(cacheModuleConfig),

apps/backend/src/modules/gtfs/gtfs.controller.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,19 @@ export class GtfsController implements OnModuleInit {
88
constructor(private readonly gtfsService: GtfsService) {}
99

1010
async onModuleInit(): Promise<void> {
11-
await this.gtfsService.syncGtfsData();
11+
try {
12+
this.gtfsService.syncGtfsData();
13+
} catch (error) {
14+
console.error(error);
15+
}
1216
}
1317

1418
@Cron(CronExpression.EVERY_7_HOURS)
1519
async cronSyncStops(): Promise<void> {
16-
await this.gtfsService.syncGtfsData();
20+
try {
21+
await this.gtfsService.syncGtfsData();
22+
} catch (error) {
23+
console.error(error);
24+
}
1725
}
1826
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { CacheInterceptor } from "@nestjs/cache-manager";
2+
import {
3+
Controller,
4+
Get,
5+
HttpException,
6+
HttpStatus,
7+
Param,
8+
UseInterceptors,
9+
Version,
10+
} from "@nestjs/common";
11+
import { ApiParam, ApiTags } from "@nestjs/swagger";
12+
13+
import { EndpointVersion } from "src/enums/endpoint-version";
14+
import { RouteService } from "src/modules/route/route.service";
15+
16+
@ApiTags("route")
17+
@Controller("route")
18+
@UseInterceptors(CacheInterceptor)
19+
export class RouteController {
20+
constructor(private readonly routeService: RouteService) {}
21+
22+
@Get(":id")
23+
@Version([EndpointVersion.v1])
24+
@ApiParam({
25+
name: "id",
26+
description: "Route ID",
27+
required: true,
28+
example: "L991",
29+
type: "string",
30+
})
31+
async getRoute(@Param("id") id: unknown) {
32+
if (typeof id !== "string") {
33+
throw new HttpException("Missing route ID", HttpStatus.BAD_REQUEST);
34+
}
35+
36+
return this.routeService.getRoute(id);
37+
}
38+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Module } from "@nestjs/common";
2+
3+
import { RouteController } from "src/modules/route/route.controller";
4+
import { RouteService } from "src/modules/route/route.service";
5+
6+
@Module({
7+
controllers: [RouteController],
8+
providers: [RouteService],
9+
imports: [],
10+
})
11+
export class RouteModule {}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { Injectable } from "@nestjs/common";
2+
import { group, unique } from "radash";
3+
4+
import { platformSelect } from "src/modules/platform/platform.service";
5+
import { PrismaService } from "src/modules/prisma/prisma.service";
6+
7+
const gtfsRouteSelect = {
8+
id: true,
9+
shortName: true,
10+
longName: true,
11+
isNight: true,
12+
color: true,
13+
url: true,
14+
type: true,
15+
};
16+
17+
const gtfsRouteStopSelect = {
18+
directionId: true,
19+
stopId: true,
20+
stopSequence: true,
21+
};
22+
23+
@Injectable()
24+
export class RouteService {
25+
constructor(private prisma: PrismaService) {}
26+
27+
async getRoute(id: string) {
28+
const route = await this.prisma.gtfsRoute.findFirst({
29+
select: gtfsRouteSelect,
30+
where: {
31+
id,
32+
},
33+
});
34+
35+
const routeStops = await this.prisma.gtfsRouteStop.findMany({
36+
select: gtfsRouteStopSelect,
37+
where: {
38+
routeId: id,
39+
},
40+
orderBy: {
41+
stopSequence: "asc",
42+
},
43+
});
44+
45+
const stops = await this.prisma.platform.findMany({
46+
select: platformSelect,
47+
where: {
48+
id: {
49+
in: unique(routeStops.map((item) => item.stopId)),
50+
},
51+
},
52+
});
53+
54+
return {
55+
...route,
56+
directions: group(
57+
routeStops.map((routeStop) => ({
58+
...routeStop,
59+
stop: stops.find((stop) => stop.id === routeStop.stopId),
60+
})),
61+
(item) => item.directionId,
62+
),
63+
};
64+
}
65+
}

0 commit comments

Comments
 (0)