From 5489acd10c441c3df1ea72163663a2594f8adc7b Mon Sep 17 00:00:00 2001 From: Ru Chern Chong Date: Tue, 14 Nov 2023 17:45:49 +0800 Subject: [PATCH] Add car filter by year --- packages/functions/src/car.ts | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) 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), + }; +});