-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #64 from krystxf/feat/be-route-stops
feat: routes endpoint
- Loading branch information
Showing
12 changed files
with
412 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
apps/backend/prisma/migrations/20241205183812_gtfs_routes/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
-- CreateTable | ||
CREATE TABLE "GtfsRoute" ( | ||
"id" TEXT NOT NULL, | ||
"type" TEXT NOT NULL, | ||
"shortName" TEXT NOT NULL, | ||
"longName" TEXT, | ||
"url" TEXT, | ||
"color" TEXT, | ||
"isNight" BOOLEAN, | ||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"updatedAt" TIMESTAMP(3) NOT NULL, | ||
|
||
CONSTRAINT "GtfsRoute_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateTable | ||
CREATE TABLE "GtfsRouteStop" ( | ||
"id" TEXT NOT NULL, | ||
"routeId" TEXT NOT NULL, | ||
"directionId" TEXT NOT NULL, | ||
"stopId" TEXT NOT NULL, | ||
"stopSequence" INTEGER NOT NULL, | ||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"updatedAt" TIMESTAMP(3) NOT NULL, | ||
|
||
CONSTRAINT "GtfsRouteStop_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "GtfsRouteStop_routeId_directionId_stopId_stopSequence_key" ON "GtfsRouteStop"("routeId", "directionId", "stopId", "stopSequence"); | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "GtfsRouteStop" ADD CONSTRAINT "GtfsRouteStop_routeId_fkey" FOREIGN KEY ("routeId") REFERENCES "GtfsRoute"("id") ON DELETE RESTRICT ON UPDATE CASCADE; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Controller, OnModuleInit } from "@nestjs/common"; | ||
import { Cron, CronExpression } from "@nestjs/schedule"; | ||
|
||
import { GtfsService } from "src/modules/gtfs/gtfs.service"; | ||
|
||
@Controller("gtfs") | ||
export class GtfsController implements OnModuleInit { | ||
constructor(private readonly gtfsService: GtfsService) {} | ||
|
||
async onModuleInit(): Promise<void> { | ||
try { | ||
this.gtfsService.syncGtfsData(); | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
} | ||
|
||
@Cron(CronExpression.EVERY_7_HOURS) | ||
async cronSyncStops(): Promise<void> { | ||
try { | ||
await this.gtfsService.syncGtfsData(); | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Module } from "@nestjs/common"; | ||
|
||
import { GtfsController } from "src/modules/gtfs/gtfs.controller"; | ||
import { GtfsService } from "src/modules/gtfs/gtfs.service"; | ||
|
||
@Module({ | ||
controllers: [GtfsController], | ||
providers: [GtfsService], | ||
imports: [], | ||
}) | ||
export class GtfsModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { Injectable } from "@nestjs/common"; | ||
import { Open as unzipperOpen } from "unzipper"; | ||
|
||
import { PrismaService } from "src/modules/prisma/prisma.service"; | ||
import { parseCsvString } from "src/utils/csv.utils"; | ||
|
||
@Injectable() | ||
export class GtfsService { | ||
constructor(private readonly prisma: PrismaService) {} | ||
|
||
async syncGtfsData() { | ||
const response = await fetch("https://data.pid.cz/PID_GTFS.zip"); | ||
const arrayBuffer = await response.arrayBuffer(); | ||
const buffer = Buffer.from(arrayBuffer); | ||
const directory = await unzipperOpen.buffer(buffer); | ||
|
||
const routes = directory.files.find( | ||
(file) => file.path === "routes.txt", | ||
); | ||
if (!routes) { | ||
console.log("routes.txt not found"); | ||
return; | ||
} | ||
const routeStops = directory.files.find( | ||
(file) => file.path === "route_stops.txt", | ||
); | ||
if (!routeStops) { | ||
console.log("route_stops.txt not found"); | ||
return; | ||
} | ||
|
||
const routesBuffer = await routes.buffer(); | ||
const routeStopsBuffer = await routeStops.buffer(); | ||
|
||
type RouteRecord = { | ||
route_id: string; | ||
route_short_name: string; | ||
route_long_name: string; | ||
route_type: string; | ||
route_color?: string | undefined; | ||
is_night: string; | ||
route_url?: string | undefined; | ||
}; | ||
// FIXME: validate with zod | ||
const routesData = await parseCsvString<RouteRecord>( | ||
routesBuffer.toString(), | ||
); | ||
|
||
type RouteStopRecord = { | ||
route_id: string; | ||
direction_id: string; | ||
stop_id: string; | ||
stop_sequence: string; | ||
}; | ||
// FIXME: validate with zod | ||
const routeStopsData = await parseCsvString<RouteStopRecord>( | ||
routeStopsBuffer.toString(), | ||
); | ||
|
||
await this.prisma.$transaction(async (transaction) => { | ||
await transaction.gtfsRouteStop.deleteMany(); | ||
await transaction.gtfsRoute.deleteMany(); | ||
|
||
await transaction.gtfsRoute.createMany({ | ||
data: routesData.map((route) => ({ | ||
id: route.route_id, | ||
shortName: route.route_short_name, | ||
longName: route.route_long_name ?? null, | ||
type: route.route_type, | ||
isNight: Boolean(route.is_night), | ||
color: route.route_color ?? null, | ||
url: route.route_url ?? null, | ||
})), | ||
}); | ||
|
||
await transaction.gtfsRouteStop.createMany({ | ||
data: routeStopsData.map((routeStop) => ({ | ||
routeId: routeStop.route_id, | ||
directionId: routeStop.direction_id, | ||
stopId: routeStop.stop_id, | ||
stopSequence: Number(routeStop.stop_sequence), | ||
})), | ||
}); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { CacheInterceptor } from "@nestjs/cache-manager"; | ||
import { | ||
Controller, | ||
Get, | ||
HttpException, | ||
HttpStatus, | ||
Param, | ||
UseInterceptors, | ||
Version, | ||
} from "@nestjs/common"; | ||
import { ApiParam, ApiTags } from "@nestjs/swagger"; | ||
|
||
import { EndpointVersion } from "src/enums/endpoint-version"; | ||
import { RouteService } from "src/modules/route/route.service"; | ||
|
||
@ApiTags("route") | ||
@Controller("route") | ||
@UseInterceptors(CacheInterceptor) | ||
export class RouteController { | ||
constructor(private readonly routeService: RouteService) {} | ||
|
||
@Get(":id") | ||
@Version([EndpointVersion.v1]) | ||
@ApiParam({ | ||
name: "id", | ||
description: "Route ID", | ||
required: true, | ||
example: "L991", | ||
type: "string", | ||
}) | ||
async getRoute(@Param("id") id: unknown) { | ||
if (typeof id !== "string") { | ||
throw new HttpException("Missing route ID", HttpStatus.BAD_REQUEST); | ||
} | ||
|
||
return this.routeService.getRoute(id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Module } from "@nestjs/common"; | ||
|
||
import { RouteController } from "src/modules/route/route.controller"; | ||
import { RouteService } from "src/modules/route/route.service"; | ||
|
||
@Module({ | ||
controllers: [RouteController], | ||
providers: [RouteService], | ||
imports: [], | ||
}) | ||
export class RouteModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { Injectable } from "@nestjs/common"; | ||
import { group, unique } from "radash"; | ||
|
||
import { platformSelect } from "src/modules/platform/platform.service"; | ||
import { PrismaService } from "src/modules/prisma/prisma.service"; | ||
|
||
const gtfsRouteSelect = { | ||
id: true, | ||
shortName: true, | ||
longName: true, | ||
isNight: true, | ||
color: true, | ||
url: true, | ||
type: true, | ||
}; | ||
|
||
const gtfsRouteStopSelect = { | ||
directionId: true, | ||
stopId: true, | ||
stopSequence: true, | ||
}; | ||
|
||
@Injectable() | ||
export class RouteService { | ||
constructor(private prisma: PrismaService) {} | ||
|
||
async getRoute(id: string) { | ||
const route = await this.prisma.gtfsRoute.findFirst({ | ||
select: gtfsRouteSelect, | ||
where: { | ||
id, | ||
}, | ||
}); | ||
|
||
const routeStops = await this.prisma.gtfsRouteStop.findMany({ | ||
select: gtfsRouteStopSelect, | ||
where: { | ||
routeId: id, | ||
}, | ||
orderBy: { | ||
stopSequence: "asc", | ||
}, | ||
}); | ||
|
||
const stops = await this.prisma.platform.findMany({ | ||
select: platformSelect, | ||
where: { | ||
id: { | ||
in: unique(routeStops.map((item) => item.stopId)), | ||
}, | ||
}, | ||
}); | ||
|
||
return { | ||
...route, | ||
directions: group( | ||
routeStops.map((routeStop) => ({ | ||
...routeStop, | ||
stop: stops.find((stop) => stop.id === routeStop.stopId), | ||
})), | ||
(item) => item.directionId, | ||
), | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { parseString } from "@fast-csv/parse"; | ||
|
||
export async function parseCsvString<T>(csvString: string): Promise<T[]> { | ||
return new Promise((resolve) => { | ||
const rows: T[] = []; | ||
|
||
parseString(csvString, { headers: true }) | ||
.on("error", (error) => console.error(error)) | ||
.on("data", (row) => rows.push(row)) | ||
.on("end", () => { | ||
resolve(rows); | ||
}); | ||
}); | ||
} |
Oops, something went wrong.
b9aadd3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
metro-now – ./
metro-now-krystofs-projects-e2322253.vercel.app
metro-now-git-main-krystofs-projects-e2322253.vercel.app
metronow.vercel.app