diff --git a/packages/functions/src/car.ts b/packages/functions/src/car.ts index 1b66a58..8670c6c 100644 --- a/packages/functions/src/car.ts +++ b/packages/functions/src/car.ts @@ -1,7 +1,21 @@ -import { ApiHandler } from "sst/node/api"; +import { ApiHandler, useQueryParams } from "sst/node/api"; import { Car } from "@lta-datasets-updater/core/car"; -export const list = ApiHandler(async (_evt) => ({ - statusCode: 200, - body: JSON.stringify(await Car.list()), -})); +export const list = ApiHandler(async (_evt) => { + const params = useQueryParams(); + const cars = await Car.list(); + + const filteredCars = + Object.keys(params).length > 0 + ? cars.filter(({ month }) => { + const [year] = month.split("-"); + + return year === params.year; + }) + : cars; + + return { + statusCode: 200, + body: JSON.stringify(filteredCars), + }; +});