Skip to content

Commit

Permalink
feat(api): endpoints for filtering by both symbol and year
Browse files Browse the repository at this point in the history
  • Loading branch information
kripod committed Apr 17, 2024
1 parent 1ea3221 commit 072f301
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 22 deletions.
37 changes: 37 additions & 0 deletions src/pages/api/v1/symbols/[symbol]/[year].json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import type {
APIRoute,
GetStaticPaths,
InferGetStaticParamsType,
InferGetStaticPropsType,
} from "astro";
import { getCollection } from "astro:content";

const quoteRecords = await getCollection("quoteRecords");

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 [...quotesByYearBySymbol].flatMap(([symbol, quotesByYear]) =>
[...quotesByYear].map(([year, quotes]) => ({
params: {
symbol,
year: year.toString(),
},
props: Object.fromEntries(quotes),
})),
);
}) satisfies GetStaticPaths;

type Params = InferGetStaticParamsType<typeof getStaticPaths>;
type Props = InferGetStaticPropsType<typeof getStaticPaths>;

export const GET: APIRoute<Props, Params> = (context) =>
Response.json(context.props);
4 changes: 3 additions & 1 deletion src/pages/api/v1/symbols/[symbol]/index.json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ const quoteRecords = await getCollection("quoteRecords");

export const getStaticPaths = (() => {
return quoteRecords.map((quoteRecord) => ({
params: { symbol: quoteRecord.id },
params: {
symbol: quoteRecord.id,
},
props: quoteRecord.data,
}));
}) satisfies GetStaticPaths;
Expand Down
25 changes: 5 additions & 20 deletions src/pages/api/v1/years/[year].json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,15 @@ import type {
InferGetStaticParamsType,
InferGetStaticPropsType,
} from "astro";
import { getCollection } from "astro:content";

const quoteRecords = await getCollection("quoteRecords");

const quoteDates = new Set(
quoteRecords.flatMap((quoteRecord) => Object.keys(quoteRecord.data)),
);
export const quoteYears = new Set(
[...quoteDates].map((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()),
];
}),
);
import { quotesByYearBySymbol } from "../symbols/[symbol]/[year].json";
import { quoteYears } from "./index.json";

export const getStaticPaths = (() => {
return [...quoteYears].map((year) => ({
params: { year: year.toString() },
params: {
year: year.toString(),
},
props: Object.fromEntries(
[...quotesByYearBySymbol].map(([symbol, quotesByYear]) => {
const quotes = quotesByYear.get(year);
Expand Down
12 changes: 11 additions & 1 deletion src/pages/api/v1/years/index.json.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import type { APIRoute } from "astro";
import { getCollection } from "astro:content";

import { quoteYears, quotesByYearBySymbol } from "./[year].json";
import { quotesByYearBySymbol } from "../symbols/[symbol]/[year].json";

const quoteRecords = await getCollection("quoteRecords");

const quoteDates = new Set(
quoteRecords.flatMap((quoteRecord) => Object.keys(quoteRecord.data)),
);
export const quoteYears = new Set(
[...quoteDates].map((date) => new Date(date).getUTCFullYear()),
);

export const GET: APIRoute = () =>
Response.json(
Expand Down

0 comments on commit 072f301

Please sign in to comment.