refactor: remove some duplicated code

This commit is contained in:
2023-08-01 17:50:00 +02:00
parent 01697a6686
commit c5cf629482
30 changed files with 377 additions and 321 deletions

View File

@ -1,34 +1,10 @@
import { HandlerContext } from "$fresh/server.ts";
import { getDocument, getDocuments } from "@lib/documents.ts";
import { parseMovie } from "@lib/movies.ts";
import { Handlers } from "$fresh/server.ts";
import { getAllMovies } from "@lib/resource/movies.ts";
import { json } from "@lib/helpers.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 });
export const handler: Handlers = {
async GET() {
const movies = await getAllMovies();
return json(movies);
},
};