Skip to content

Commit

Permalink
Add car filter by year
Browse files Browse the repository at this point in the history
  • Loading branch information
ruchernchong committed Nov 14, 2023
1 parent f79dcb1 commit 5489acd
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions packages/functions/src/car.ts
Original file line number Diff line number Diff line change
@@ -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),
};
});

0 comments on commit 5489acd

Please sign in to comment.