feat: add movies
This commit is contained in:
23
routes/api/movies/[name].ts
Normal file
23
routes/api/movies/[name].ts
Normal file
@ -0,0 +1,23 @@
|
||||
import { HandlerContext } from "$fresh/server.ts";
|
||||
import { getDocument } from "@lib/documents.ts";
|
||||
import { parseMovie } from "@lib/movies.ts";
|
||||
|
||||
export async function getMovie(name: string) {
|
||||
const document = await getDocument(`Media/movies/${name}.md`);
|
||||
|
||||
const movie = parseMovie(document, name);
|
||||
|
||||
return movie;
|
||||
}
|
||||
|
||||
export const handler = async (
|
||||
_req: Request,
|
||||
_ctx: HandlerContext,
|
||||
): Promise<Response> => {
|
||||
const movie = await getMovie(_ctx.params.name);
|
||||
|
||||
const headers = new Headers();
|
||||
headers.append("Content-Type", "application/json");
|
||||
|
||||
return new Response(JSON.stringify(movie));
|
||||
};
|
34
routes/api/movies/index.ts
Normal file
34
routes/api/movies/index.ts
Normal file
@ -0,0 +1,34 @@
|
||||
import { HandlerContext } from "$fresh/server.ts";
|
||||
import { getDocument, getDocuments } from "@lib/documents.ts";
|
||||
import { parseMovie } from "@lib/movies.ts";
|
||||
|
||||
export async function getMovies() {
|
||||
const documents = await getDocuments();
|
||||
|
||||
return Promise.all(
|
||||
documents.filter((d) => {
|
||||
return d.name.startsWith("Media/movies/") &&
|
||||
d.contentType === "text/markdown" &&
|
||||
!d.name.endsWith("index.md");
|
||||
}).map(async (doc) => {
|
||||
const document = await getDocument(doc.name);
|
||||
const movie = parseMovie(document, doc.name);
|
||||
return {
|
||||
...movie,
|
||||
id: movie.id.replace(/\.md$/, "").replace(/^Media\/movies\//, ""),
|
||||
};
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
export const handler = async (
|
||||
_req: Request,
|
||||
_ctx: HandlerContext,
|
||||
): Promise<Response> => {
|
||||
const headers = new Headers();
|
||||
headers.append("Content-Type", "application/json");
|
||||
|
||||
const movies = await getMovies();
|
||||
|
||||
return new Response(JSON.stringify(movies), { headers });
|
||||
};
|
Reference in New Issue
Block a user