Skip to content

Commit

Permalink
Refactor APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
Ru Chern Chong committed Dec 25, 2023
1 parent 09d6df9 commit 5e5f293
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
14 changes: 9 additions & 5 deletions packages/core/src/coe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ import db from "../../config/db";

const collection = db.collection("coe");

export const latest = async () => {
export const list = async () => collection.find().toArray();

export const getCOEResultByMonth = async (month?: string) => {
const date = new Date();
const year = date.getFullYear();
const month = date.getMonth() + 1;
const formattedMonth = [year, month].join("-");
const formattedMonth =
month || [date.getFullYear(), date.getMonth() + 1].join("-");

return collection.find({ month: formattedMonth }).toArray();
return collection
.find({ month: formattedMonth })
.sort({ vehicle_class: 1 })
.toArray();
};

export * as COE from "./coe";
17 changes: 14 additions & 3 deletions packages/functions/src/coe.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
import { ApiHandler } from "sst/node/api";
import { ApiHandler, useQueryParam, useQueryParams } from "sst/node/api";
import { COE } from "@lta-cars-dataset/core/coe";
import { createResponse } from "./utils/createResponse";

export const latest = ApiHandler(async (_evt) => {
const coeResult = await COE.latest();
export const list = ApiHandler(async (_evt) => {
const result = await COE.list();
return createResponse(result);
});

export const byMonth = ApiHandler(async (_evt) => {
const month = useQueryParam("month");
const coeResult = await COE.getCOEResultByMonth(month);

return createResponse(coeResult);
});

export const latest = ApiHandler(async (_evt) => {
const result = await COE.getCOEResultByMonth();
return createResponse(result);
});
14 changes: 12 additions & 2 deletions stacks/ApiStack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ const CUSTOM_DOMAINS: Record<string, any> = {
},
};

const CORS_SETTINGS: Record<string, any> = {
dev: {
allowOrigins: ["*"],
},
prod: {
allowOrigins: ["https://singapore-ev-trends.ruchern.xyz"],
},
};

export const api = ({ stack }: StackContext) => {
const MONGODB_URI = new Config.Secret(stack, "MONGODB_URI");

Expand All @@ -23,13 +32,14 @@ export const api = ({ stack }: StackContext) => {
},
customDomain: CUSTOM_DOMAINS[stack.stage],
cors: {
allowOrigins: ["https://singapore-ev-trends.ruchern.xyz"],
...CORS_SETTINGS[stack.stage],
},
routes: {
"GET /": "packages/functions/src/cars.electric",
"GET /cars/electric": "packages/functions/src/cars.electric",
"GET /cars/petrol": "packages/functions/src/cars.petrol",
"GET /coe": "packages/functions/src/coe.latest",
"GET /coe": "packages/functions/src/coe.list",
"GET /coe/latest": "packages/functions/src/coe.latest",
"GET /updater/cars": "packages/functions/src/updater.cars",
"GET /updater/coe": "packages/functions/src/updater.coe",
"GET /vehicle-make": "packages/functions/src/vehicle-make.list",
Expand Down

0 comments on commit 5e5f293

Please sign in to comment.