Skip to content

Commit

Permalink
Merge pull request #59 from sgcarstrends/58-update-months-endpoint
Browse files Browse the repository at this point in the history
Refactor months endpoint
  • Loading branch information
ruchernchong authored Oct 13, 2024
2 parents 23dd745 + 16cc9f4 commit 05d84a8
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 28 deletions.
17 changes: 17 additions & 0 deletions src/lib/getUniqueMonths.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import db from "../config/db";
import redis from "../config/redis";

const CACHE_TTL = 60 * 60 * 24; // 1 day in seconds

export const getUniqueMonths = async (collection, key = "month") => {
const CACHE_KEY = `${collection}:months`;

let months: string[] = await redis.smembers(CACHE_KEY);
if (months.length === 0) {
months = await db.collection(collection).distinct(key);
await redis.sadd(CACHE_KEY, ...months);
await redis.expire(CACHE_KEY, CACHE_TTL);
}

return months.sort((a, b) => b.localeCompare(a));
};
1 change: 0 additions & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export interface LatestMonth {
}

export type Make = Car["make"];
export type Month = Car["month"];

export enum Collection {
Cars = "cars",
Expand Down
16 changes: 15 additions & 1 deletion src/v1/routes/cars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ import { Hono } from "hono";
import db from "../../config/db";
import redis from "../../config/redis";
import type { Car, Make } from "../../types";
import { Collection } from "../../types";
import { HYBRID_REGEX } from "../../config";
import type { WithId } from "mongodb";
import { getUniqueMonths } from "../../lib/getUniqueMonths";
import { groupMonthsByYear } from "../../lib/groupMonthsByYear";

const app = new Hono();

const collection = db.collection<Car>("cars");
const collection = db.collection<Car>(Collection.Cars);

interface QueryParams {
month?: string;
Expand Down Expand Up @@ -69,6 +72,17 @@ app.get("/", async (c) => {
return c.json(cars);
});

app.get("/months", async (c) => {
const { grouped } = c.req.query();

const months = await getUniqueMonths(Collection.Cars);
if (grouped) {
return c.json(groupMonthsByYear(months));
}

return c.json(months);
});

app.get("/makes", async (c) => {
const CACHE_KEY = "makes";
const CACHE_TTL = 60 * 60 * 24; // 1 day in seconds
Expand Down
13 changes: 13 additions & 0 deletions src/v1/routes/coe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { Collection, OrderBy } from "../../types";
import redis from "../../config/redis";
import { getLatestMonth } from "../../lib/getLatestMonth";
import { isValid, parse } from "date-fns";
import { getUniqueMonths } from "../../lib/getUniqueMonths";
import { groupMonthsByYear } from "../../lib/groupMonthsByYear";

type QueryParams = {
sort?: string;
Expand Down Expand Up @@ -85,6 +87,17 @@ app.get("/", async (c) => {
return c.json(result);
});

app.get("/months", async (c) => {
const { grouped } = c.req.query();

const months = await getUniqueMonths(Collection.COE);
if (grouped) {
return c.json(groupMonthsByYear(months));
}

return c.json(months);
});

app.get("/latest", async (c) => {
const CACHE_KEY = `coe:latest`;
const cachedData = await getCachedData<COEResult[]>(CACHE_KEY);
Expand Down
26 changes: 0 additions & 26 deletions src/v1/routes/months.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,8 @@
import { Hono } from "hono";
import db from "../../config/db";
import { getLatestMonth } from "../../lib/getLatestMonth";
import { groupMonthsByYear } from "../../lib/groupMonthsByYear";
import type { Car, Month } from "../../types";
import redis from "../../config/redis";

const app = new Hono();

app.get("/", async (c) => {
const CACHE_KEY = "months";
const CACHE_TTL = 60 * 60 * 24; // 1 day in seconds

const grouped = c.req.query("grouped");

let months: Month[] = await redis.smembers(CACHE_KEY);
if (months.length === 0) {
months = await db.collection<Car>("cars").distinct("month");
await redis.sadd(CACHE_KEY, ...months);
await redis.expire(CACHE_KEY, CACHE_TTL);
}

months.sort((a, b) => b.localeCompare(a));

if (grouped) {
return c.json(groupMonthsByYear(months));
}

return c.json(months);
});

app.get("/latest", async (c) => {
const collection = c.req.query("collection");
const dbCollections = ["cars", "coe"];
Expand Down

0 comments on commit 05d84a8

Please sign in to comment.