diff --git a/source/ccci-stolaf-college/v1/index.ts b/source/ccci-stolaf-college/v1/index.ts index 1bbe5987..cc5cea1d 100644 --- a/source/ccci-stolaf-college/v1/index.ts +++ b/source/ccci-stolaf-college/v1/index.ts @@ -113,6 +113,7 @@ api.get('/transit/modes', transit.modes) // streams api.get('/streams/archived', streams.archived) api.get('/streams/upcoming', streams.upcoming) +api.get('/streams/search', streams.search) // stoprint api.get('/printing/color-printers', printing.colorPrinters) diff --git a/source/ccci-stolaf-college/v1/streams.ts b/source/ccci-stolaf-college/v1/streams.ts index 76a0d5f7..5bc8ce8d 100644 --- a/source/ccci-stolaf-college/v1/streams.ts +++ b/source/ccci-stolaf-college/v1/streams.ts @@ -31,6 +31,7 @@ const GetStreamsParamsSchema = z.object({ dateFrom: z.string().date().optional(), dateTo: z.string().date().optional(), sort: z.enum(['ascending', 'descending']).default('ascending'), + query: z.string().optional(), }) type StOlafStreamsParamsType = z.infer @@ -39,10 +40,11 @@ const StOlafStreamsParamsSchema = z.object({ date_to: z.string().date(), sort: z.enum(['ascending', 'descending']), class: z.enum(['current', 'archived']), + squery: z.string().optional(), }) const getStreams = mem( - async (params: StOlafStreamsParamsType) => { + async (params: StOlafStreamsParamsType & {squery?: string}) => { const url = 'https://www.stolaf.edu/multimedia/api/collection' const response = await get(url, {searchParams: params}) const json = (await response.clone().json()) as Promise< @@ -94,3 +96,22 @@ export async function archived(ctx: Context) { sort, }) } + +export async function search(ctx: Context) { + ctx.cacheControl(ONE_HOUR) + + const { + dateFrom = moment().subtract(30, 'year').tz('America/Chicago').format('YYYY-MM-DD'), + dateTo = moment().tz('America/Chicago').format('YYYY-MM-DD'), + sort, + query, + } = GetStreamsParamsSchema.parse(Object.fromEntries(ctx.URL.searchParams.entries())) + + ctx.body = await getStreams({ + class: 'archived', + date_from: dateFrom, + date_to: dateTo, + sort, + ...(query ? {squery: query} : {}), + }) +}