Skip to content

Commit

Permalink
feat(be): stops bbox search
Browse files Browse the repository at this point in the history
  • Loading branch information
krystxf committed Jul 12, 2024
1 parent 35c2592 commit 62a4c88
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 22 deletions.
4 changes: 2 additions & 2 deletions backend-nest/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { AppService } from "./app.service";
import { MetroController } from "./controllers/metro/metro.controller";
import { ConfigModule } from "@nestjs/config";
import { RequestLoggerMiddleware } from "./middleware/request-logger.middleware";
import { TTL } from "./constants/constants";
import { TTL_DEFAULT } from "./constants/constants";
import { ScheduleModule } from "@nestjs/schedule";
import { SyncStopsService } from "./services/sync-stops.service";
import { PrismaService } from "./services/prisma.service";
Expand All @@ -17,7 +17,7 @@ import { StopController } from "./controllers/stop/stop.controller";
ScheduleModule.forRoot(),
CacheModule.register({
isGlobal: true,
ttl: TTL,
ttl: TTL_DEFAULT,
}),
],
controllers: [AppController, MetroController, StopController],
Expand Down
2 changes: 1 addition & 1 deletion backend-nest/src/constants/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export const TTL = 20_000;
export const TTL_DEFAULT = 2 * 1_000;

export const GOLEMIO_API = "https://api.golemio.cz";
78 changes: 61 additions & 17 deletions backend-nest/src/controllers/stop/stop.controller.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,58 @@
import { Controller, Get } from "@nestjs/common";
import { Controller, Get, HttpException, Query } from "@nestjs/common";
import { PrismaService } from "../../services/prisma.service";
import z from "zod";
import { minMax } from "src/utils/math";
import { parseQueryParam } from "src/utils/query-params";
import { stopSelect } from "./stop.utils";

@Controller("stop")
export class StopController {
constructor(private prisma: PrismaService) {}

@Get("all")
async getAllStops(): Promise<any[]> {
@Get()
async getStops(
@Query("latitude") latitudes: string[],
@Query("longitude") longitudes: string[],
) {
const parsedParams = {
latitudes: parseQueryParam(latitudes),
longitudes: parseQueryParam(longitudes),
};

const schema = z.number().array().length(2);
const parsedValues = {
latitudes: schema.safeParse(parsedParams.latitudes),
longitudes: schema.safeParse(parsedParams.longitudes),
};

if (!parsedValues.latitudes.success) {
throw new HttpException(
"Invalid latitude: " +
JSON.stringify(parsedValues.latitudes.error.errors),
400,
);
}

if (!parsedValues.longitudes.success) {
throw new HttpException(
"Invalid longitude: " + parsedValues.longitudes.error,
400,
);
}

const latitude = minMax(parsedValues.latitudes.data);
const longitude = minMax(parsedValues.longitudes.data);

const stops = await this.prisma.stop.findMany({
select: {
id: true,
latitude: true,
longitude: true,
name: true,
routes: {
select: {
route: {
select: {
id: true,
name: true,
},
},
},
select: stopSelect,
where: {
latitude: {
gte: latitude.min,
lte: latitude.max,
},
longitude: {
gte: longitude.min,
lte: longitude.max,
},
},
});
Expand All @@ -31,4 +62,17 @@ export class StopController {
routes: stop.routes.map(({ route }) => route),
}));
}

@Get("all")
async getAllStops() {
console.log("Fetching all stops");
const stops = await this.prisma.stop.findMany({
select: stopSelect,
});

return stops.map((stop) => ({
...stop,
routes: stop.routes.map(({ route }) => route),
}));
}
}
18 changes: 18 additions & 0 deletions backend-nest/src/controllers/stop/stop.utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { Prisma } from "@prisma/client";

export const stopSelect = {
id: true,
latitude: true,
longitude: true,
name: true,
routes: {
select: {
route: {
select: {
id: true,
name: true,
},
},
},
},
} satisfies Prisma.StopSelect;
4 changes: 2 additions & 2 deletions backend-nest/src/utils/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { PlatformID } from "../data/platforms";
import { diff, group } from "radash";
import { getDepartures } from "../controllers/metro/getDepartures";
import { TTL } from "../constants";
import { TTL_DEFAULT } from "../constants";
import type { CacheManager } from "../types/types";

export const getGolemioHeaders = () => {
Expand Down Expand Up @@ -35,7 +35,7 @@ export const fetchDeparturesByGtfsID = async <TCachedData>(
) as Record<string, TCachedData>;

for (const key in newResByPlatform) {
await cacheManager.set(key, newResByPlatform[key], TTL);
await cacheManager.set(key, newResByPlatform[key], TTL_DEFAULT);
}

return { ...res, ...newResByPlatform };
Expand Down

0 comments on commit 62a4c88

Please sign in to comment.