Skip to content

Commit

Permalink
Add group to months API
Browse files Browse the repository at this point in the history
  • Loading branch information
ruchernchong committed Jun 1, 2024
1 parent 6d315a4 commit 25c0024
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,28 @@ app.get("/vehicle-make", async (c) => {
});

app.get("/months", async (c) => {
return c.json(await db.collection<Car>("cars").distinct("month"));
const group = c.req.query("group");
const months = await db.collection<Car>("cars").distinct("month");

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

if (group) {
return c.json(
sortedMonths.reduce((acc: Record<string, string[]>, date) => {
const [year, month] = date.split("-");

if (!acc[year]) {
acc[year] = [];
}

acc[year].push(month);

return acc;
}, {}),
);
}

return c.json(sortedMonths);
});

showRoutes(app);
Expand Down

0 comments on commit 25c0024

Please sign in to comment.