diff --git a/src/pages/api/v1/years/[year].json.ts b/src/pages/api/v1/years/[year].json.ts index 10e85ea..3d01e67 100644 --- a/src/pages/api/v1/years/[year].json.ts +++ b/src/pages/api/v1/years/[year].json.ts @@ -11,21 +11,21 @@ const quoteRecords = await getCollection("quoteRecords"); const quoteDates = new Set( quoteRecords.flatMap((quoteRecord) => Object.keys(quoteRecord.data)), ); -const quoteYears = new Set( +export const quoteYears = new Set( [...quoteDates].map((date) => new Date(date).getUTCFullYear()), ); -export const getStaticPaths = (() => { - const quotesByYearBySymbol = new Map( - quoteRecords.map((quoteRecord) => { - const quotes = Object.entries(quoteRecord.data); - return [ - quoteRecord.id, - Map.groupBy(quotes, ([date]) => new Date(date).getUTCFullYear()), - ]; - }), - ); +export const quotesByYearBySymbol = new Map( + quoteRecords.map((quoteRecord) => { + const quotes = Object.entries(quoteRecord.data); + return [ + quoteRecord.id, + Map.groupBy(quotes, ([date]) => new Date(date).getUTCFullYear()), + ]; + }), +); +export const getStaticPaths = (() => { return [...quoteYears].map((year) => ({ params: { year: year.toString() }, props: Object.fromEntries( diff --git a/src/pages/api/v1/years/index.json.ts b/src/pages/api/v1/years/index.json.ts index 6c53712..22df5d4 100644 --- a/src/pages/api/v1/years/index.json.ts +++ b/src/pages/api/v1/years/index.json.ts @@ -1,26 +1,15 @@ import type { APIRoute } from "astro"; -import { getCollection } from "astro:content"; -const quoteRecords = await getCollection("quoteRecords"); - -const symbolsByYear = new Map(); -for (const quoteRecord of quoteRecords) { - const years = [ - ...new Set( - Object.keys(quoteRecord.data).map((date) => - new Date(date).getUTCFullYear(), - ), - ), - ]; - for (const year of years) { - let symbols = symbolsByYear.get(year); - if (symbols == null) { - symbols = []; - symbolsByYear.set(year, symbols); - } - symbols.push(quoteRecord.id); - } -} +import { quoteYears, quotesByYearBySymbol } from "./[year].json"; export const GET: APIRoute = () => - Response.json(Object.fromEntries(symbolsByYear)); + Response.json( + Object.fromEntries( + [...quoteYears].map((year) => [ + year, + [...quotesByYearBySymbol] + .filter(([, quotesByYear]) => quotesByYear.has(year)) + .map(([symbol]) => symbol), + ]), + ), + );