Skip to content
This repository was archived by the owner on Jan 17, 2025. It is now read-only.

Commit

Permalink
feat: add metadata endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanprobst committed Mar 8, 2024
1 parent e45c933 commit 3484535
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 0 deletions.
58 changes: 58 additions & 0 deletions app/api/metadata/curricula/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { readFile } from "node:fs/promises";
import { join } from "node:path";

import { log } from "@acdh-oeaw/lib";
import type { NextRequest } from "next/server";
import { z } from "zod";

import type { Resource } from "@/lib/content/types";

const filePath = join(process.cwd(), "./public/metadata/curricula.json");

const searchParamsSchema = z.object({
limit: z.coerce.number().int().positive().max(100).optional().default(10),
offset: z.coerce.number().int().nonnegative().optional().default(0),
});

export async function GET(request: NextRequest) {
const { searchParams } = request.nextUrl;

const result = searchParamsSchema.safeParse({
limit: searchParams.get("limit"),
offset: searchParams.get("offset"),
});

if (!result.success) {
return Response.json({ message: "Bad request" }, { status: 400 });
}

const { offset, limit } = result.data;

try {
const curricula = await readCurricula();

return Response.json({
curricula: curricula.slice(offset, offset + limit),
offset,
limit,
total: curricula.length,
});
} catch (error) {
log.error(error);
return Response.json({ message: "Internal server error" }, { status: 500 });
}
}

const cache = { curricula: null as Array<Resource> | null };

async function readCurricula() {
if (cache.curricula != null) return cache.curricula;

const fileContent = await readFile(filePath, { encoding: "utf-8" });
const { curricula } = JSON.parse(fileContent) as { curricula: Array<Resource> };

// eslint-disable-next-line require-atomic-updates
cache.curricula = curricula;

return curricula;
}
58 changes: 58 additions & 0 deletions app/api/metadata/resources/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { readFile } from "node:fs/promises";
import { join } from "node:path";

import { log } from "@acdh-oeaw/lib";
import type { NextRequest } from "next/server";
import { z } from "zod";

import type { Resource } from "@/lib/content/types";

const filePath = join(process.cwd(), "./public/metadata/resources.json");

const searchParamsSchema = z.object({
limit: z.coerce.number().int().positive().max(100).optional().default(10),
offset: z.coerce.number().int().nonnegative().optional().default(0),
});

export async function GET(request: NextRequest) {
const { searchParams } = request.nextUrl;

const result = searchParamsSchema.safeParse({
limit: searchParams.get("limit"),
offset: searchParams.get("offset"),
});

if (!result.success) {
return Response.json({ message: "Bad request" }, { status: 400 });
}

const { offset, limit } = result.data;

try {
const resources = await readResources();

return Response.json({
resources: resources.slice(offset, offset + limit),
offset,
limit,
total: resources.length,
});
} catch (error) {
log.error(error);
return Response.json({ message: "Internal server error" }, { status: 500 });
}
}

const cache = { resources: null as Array<Resource> | null };

async function readResources() {
if (cache.resources != null) return cache.resources;

const fileContent = await readFile(filePath, { encoding: "utf-8" });
const { resources } = JSON.parse(fileContent) as { resources: Array<Resource> };

// eslint-disable-next-line require-atomic-updates
cache.resources = resources;

return resources;
}
14 changes: 14 additions & 0 deletions scripts/generate-metadata-dump.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { log } from "@acdh-oeaw/lib";

async function generate() {
//
}

generate()
.then(() => {
log.success("Successfully generated metadata dump.");
})
.catch((error) => {
log.error("Failed to generate metadata dump.", String(error));
process.exitCode = 1;
});

0 comments on commit 3484535

Please sign in to comment.