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

25
routes/_middleware.ts Normal file
View File

@ -0,0 +1,25 @@
//routes/middleware-error-handler/_middleware.ts
import { MiddlewareHandlerContext } from "$fresh/server.ts";
import { DomainError } from "@lib/errors.ts";
export async function handler(
_req: Request,
ctx: MiddlewareHandlerContext,
) {
try {
ctx.state.flag = true;
return await ctx.next();
} catch (error) {
console.error(error);
if (error instanceof DomainError) {
return new Response(error.statusText, {
status: error.status,
});
}
return new Response("Internal Server Error", {
status: 500,
});
}
}

View File

@ -1,4 +1,4 @@
import { HandlerContext } from "$fresh/server.ts";
import { HandlerContext, Handlers } from "$fresh/server.ts";
import {
ImageMagick,
initializeImageMagick,
@ -6,6 +6,7 @@ import {
} from "https://deno.land/x/imagemagick_deno@0.0.14/mod.ts";
import { parseMediaType } from "https://deno.land/std@0.175.0/media_types/parse_media_type.ts";
import * as cache from "@lib/cache/image.ts";
import { SILVERBULLET_SERVER } from "@lib/env.ts";
await initializeImageMagick();
@ -81,7 +82,7 @@ function parseParams(reqUrl: URL) {
};
}
export const handler = async (
const GET = async (
_req: Request,
_ctx: HandlerContext,
): Promise<Response> => {
@ -92,7 +93,7 @@ export const handler = async (
return new Response(params, { status: 400 });
}
const imageUrl = Deno.env.get("SILVERBULLET_SERVER") + "/" + params.image;
const imageUrl = SILVERBULLET_SERVER + "/" + params.image;
console.log("[api/image] " + imageUrl);
@ -138,3 +139,7 @@ export const handler = async (
},
});
};
export const handler: Handlers = {
GET,
};

View File

@ -1,11 +1,10 @@
import { HandlerContext } from "$fresh/server.ts";
import { Handlers } from "$fresh/server.ts";
import { getDocuments } from "@lib/documents.ts";
import { json } from "@lib/helpers.ts";
export const handler = async (
_req: Request,
_ctx: HandlerContext,
): Promise<Response> => {
const documents = await getDocuments();
const response = new Response(JSON.stringify(documents));
return response;
export const handler: Handlers = {
async GET() {
const documents = await getDocuments();
return json(documents);
},
};

View File

@ -1,136 +1,10 @@
import { HandlerContext } from "$fresh/server.ts";
import {
createDocument,
getDocument,
transformDocument,
} from "@lib/documents.ts";
import { fileExtension } from "https://deno.land/x/file_extension@v2.1.0/mod.ts";
import { type Movie, parseMovie } from "@lib/movies.ts";
import * as tmdb from "@lib/tmdb.ts";
import { parse, stringify } from "https://deno.land/std@0.194.0/yaml/mod.ts";
import { formatDate } from "@lib/string.ts";
import { Handlers } from "$fresh/server.ts";
import { getMovie } from "@lib/resource/movies.ts";
import { json } from "@lib/helpers.ts";
function safeFileName(inputString: string): string {
// Convert the string to lowercase
let fileName = inputString.toLowerCase();
// Replace spaces with underscores
fileName = fileName.replace(/ /g, "_");
// Remove characters that are not safe for file names
fileName = fileName.replace(/[^\w.-]/g, "");
return fileName;
}
export async function getMovie(name: string) {
const document = await getDocument(`Media/movies/${name}.md`);
const movie = parseMovie(document, name);
return movie;
}
async function updateMovieMetadata(
name: string,
metadata: Partial<Movie["meta"]>,
) {
const docId = `Media/movies/${name}.md`;
const currentDoc = await getDocument(docId);
if (!currentDoc) return;
const newDoc = transformDocument(currentDoc, (root) => {
const frontmatterNode = root.children.find((c) => c.type === "yaml");
const frontmatter = frontmatterNode?.value as string;
if (frontmatter) {
const value = parse(frontmatter) as Movie["meta"];
if (metadata.author && !value.author) {
value.author = metadata.author;
}
if (metadata.image && !value.image) {
value.image = metadata.image;
}
if (metadata.date && !value.date) {
value.date = formatDate(metadata.date);
}
frontmatterNode.value = stringify(value);
}
return root;
});
const response = await createDocument(docId, newDoc);
return response;
}
export const handler = async (
_req: Request,
_ctx: HandlerContext,
): Promise<Response> => {
const headers = new Headers();
headers.append("Content-Type", "application/json");
const movie = await getMovie(_ctx.params.name);
if (_req.method === "GET") {
return new Response(JSON.stringify(movie));
}
if (_req.method === "POST") {
const body = await _req.json();
const name = _ctx.params.name;
const { tmdbId } = body;
if (!name || !tmdbId) {
return new Response("Bad Request", {
status: 400,
});
}
const movieDetails = await tmdb.getMovie(tmdbId);
const movieCredits = !movie.meta.author &&
await tmdb.getMovieCredits(tmdbId);
const releaseDate = movieDetails.release_date;
const posterPath = movieDetails.poster_path;
const director = movieCredits?.crew?.filter?.((person) =>
person.job === "Director"
)[0];
let finalPath = "";
if (posterPath && !movie.meta.image) {
const poster = await tmdb.getMoviePoster(posterPath);
const extension = fileExtension(posterPath);
finalPath = `Media/movies/images/${
safeFileName(name)
}_cover.${extension}`;
await createDocument(finalPath, poster);
}
const metadata = {} as Movie["meta"];
if (releaseDate) {
metadata.date = new Date(releaseDate);
}
if (finalPath) {
metadata.image = finalPath;
}
if (director) {
metadata.author = director.name;
}
await updateMovieMetadata(name, metadata);
return new Response(JSON.stringify(movieCredits), {
headers,
});
}
return new Response();
export const handler: Handlers = {
async GET(_, ctx) {
const movie = await getMovie(ctx.params.name);
return json(movie);
},
};

View File

@ -0,0 +1,103 @@
import { HandlerContext, Handlers } from "$fresh/server.ts";
import {
createDocument,
getDocument,
transformDocument,
} from "@lib/documents.ts";
import { fileExtension } from "https://deno.land/x/file_extension@v2.1.0/mod.ts";
import { getMovie, type Movie } from "@lib/resource/movies.ts";
import * as tmdb from "@lib/tmdb.ts";
import { parse, stringify } from "https://deno.land/std@0.194.0/yaml/mod.ts";
import { formatDate, safeFileName } from "@lib/string.ts";
import { json } from "@lib/helpers.ts";
import { BadRequestError } from "@lib/errors.ts";
async function updateMovieMetadata(
name: string,
metadata: Partial<Movie["meta"]>,
) {
const docId = `Media/movies/${name}.md`;
const currentDoc = await getDocument(docId);
if (!currentDoc) return;
const newDoc = transformDocument(currentDoc, (root) => {
const frontmatterNode = root.children.find((c) => c.type === "yaml");
const frontmatter = frontmatterNode?.value as string;
if (frontmatter) {
const value = parse(frontmatter) as Movie["meta"];
if (metadata.author && !value.author) {
value.author = metadata.author;
}
if (metadata.image && !value.image) {
value.image = metadata.image;
}
if (metadata.date && !value.date) {
value.date = formatDate(metadata.date);
}
frontmatterNode.value = stringify(value);
}
return root;
});
const response = await createDocument(docId, newDoc);
return response;
}
const GET = async (
_req: Request,
_ctx: HandlerContext,
): Promise<Response> => {
const movie = await getMovie(_ctx.params.name);
const body = await _req.json();
const name = _ctx.params.name;
const { tmdbId } = body;
if (!name || !tmdbId) {
throw new BadRequestError();
}
const movieDetails = await tmdb.getMovie(tmdbId);
const movieCredits = !movie.meta.author &&
await tmdb.getMovieCredits(tmdbId);
const releaseDate = movieDetails.release_date;
const posterPath = movieDetails.poster_path;
const director =
movieCredits?.crew?.filter?.((person) => person.job === "Director")[0];
let finalPath = "";
if (posterPath && !movie.meta.image) {
const poster = await tmdb.getMoviePoster(posterPath);
const extension = fileExtension(posterPath);
finalPath = `Media/movies/images/${safeFileName(name)}_cover.${extension}`;
await createDocument(finalPath, poster);
}
const metadata = {} as Movie["meta"];
if (releaseDate) {
metadata.date = new Date(releaseDate);
}
if (finalPath) {
metadata.image = finalPath;
}
if (director) {
metadata.author = director.name;
}
await updateMovieMetadata(name, metadata);
return json(movieCredits);
};
export const handler: Handlers = {
GET,
};

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);
},
};

View File

@ -1,23 +1,10 @@
import { HandlerContext } from "$fresh/server.ts";
import { getDocument } from "@lib/documents.ts";
import { parseRecipe } from "@lib/recipes.ts";
import { Handlers } from "$fresh/server.ts";
import { getRecipe } from "@lib/resource/recipes.ts";
import { json } from "@lib/helpers.ts";
export async function getRecipe(name: string) {
const document = await getDocument(`Recipes/${name}.md`);
const recipe = parseRecipe(document, name);
return recipe;
}
export const handler = async (
_req: Request,
_ctx: HandlerContext,
): Promise<Response> => {
const recipe = await getRecipe(_ctx.params.name);
const headers = new Headers();
headers.append("Content-Type", "application/json");
return new Response(JSON.stringify(recipe));
export const handler: Handlers = {
async GET(_, ctx) {
const recipe = await getRecipe(ctx.params.name);
return json(recipe);
},
};

View File

@ -1,34 +1,10 @@
import { HandlerContext } from "$fresh/server.ts";
import { getDocument, getDocuments } from "@lib/documents.ts";
import { parseRecipe } from "@lib/recipes.ts";
import { Handlers } from "$fresh/server.ts";
import { getAllRecipes } from "@lib/resource/recipes.ts";
import { json } from "@lib/helpers.ts";
export async function getRecipes() {
const documents = await getDocuments();
return Promise.all(
documents.filter((d) => {
return d.name.startsWith("Recipes/") &&
d.contentType === "text/markdown" &&
!d.name.endsWith("index.md");
}).map(async (doc) => {
const document = await getDocument(doc.name);
const recipe = parseRecipe(document, doc.name);
return {
...recipe,
id: recipe.id.replace(/^Recipes\//, "").replace(/\.md$/, ""),
};
}),
);
}
export const handler = async (
_req: Request,
_ctx: HandlerContext,
): Promise<Response> => {
const headers = new Headers();
headers.append("Content-Type", "application/json");
const recipes = await getRecipes();
return new Response(JSON.stringify(recipes), { headers });
export const handler: Handlers = {
async GET() {
const recipes = await getAllRecipes();
return json(recipes);
},
};

View File

@ -1,6 +1,7 @@
import { HandlerContext } from "$fresh/server.ts";
import { HandlerContext, Handlers } from "$fresh/server.ts";
import { getMovie } from "@lib/tmdb.ts";
import * as cache from "@lib/cache/cache.ts";
import { json } from "@lib/helpers.ts";
type CachedMovieCredits = {
lastUpdated: number;
@ -9,7 +10,7 @@ type CachedMovieCredits = {
const CACHE_INTERVAL = 1000 * 60 * 24 * 30;
export const handler = async (
const GET = async (
_req: Request,
_ctx: HandlerContext,
) => {
@ -21,16 +22,13 @@ export const handler = async (
});
}
const headers = new Headers();
headers.append("Content-Type", "application/json");
const cacheId = `/movie/${id}`;
const cachedResponse = await cache.get<CachedMovieCredits>(cacheId);
if (
cachedResponse && Date.now() < (cachedResponse.lastUpdated + CACHE_INTERVAL)
) {
return new Response(JSON.stringify(cachedResponse.data), { headers });
return json(cachedResponse.data);
}
const res = await getMovie(+id);
@ -43,5 +41,9 @@ export const handler = async (
}),
);
return new Response(JSON.stringify(res));
return json(res);
};
export const handler: Handlers = {
GET,
};

View File

@ -1,6 +1,7 @@
import { HandlerContext } from "$fresh/server.ts";
import { getMovieCredits } from "@lib/tmdb.ts";
import * as cache from "@lib/cache/cache.ts";
import { json } from "@lib/helpers.ts";
type CachedMovieCredits = {
lastUpdated: number;
@ -21,9 +22,6 @@ export const handler = async (
});
}
const headers = new Headers();
headers.append("Content-Type", "application/json");
console.log("[api] getting movie credits");
const cacheId = `/movie/credits/${id}`;
@ -32,7 +30,7 @@ export const handler = async (
if (
cachedResponse && Date.now() < (cachedResponse.lastUpdated + CACHE_INTERVAL)
) {
return new Response(JSON.stringify(cachedResponse.data), { headers });
return json(cachedResponse.data);
}
const res = await getMovieCredits(+id);
@ -44,5 +42,5 @@ export const handler = async (
}),
);
return new Response(JSON.stringify(res));
return json(res);
};

View File

@ -1,6 +1,8 @@
import { HandlerContext } from "$fresh/server.ts";
import { HandlerContext, Handlers } from "$fresh/server.ts";
import { searchMovie } from "@lib/tmdb.ts";
import * as cache from "@lib/cache/cache.ts";
import { BadRequestError } from "@lib/errors.ts";
import { json } from "@lib/helpers.ts";
type CachedMovieQuery = {
lastUpdated: number;
@ -9,7 +11,7 @@ type CachedMovieQuery = {
const CACHE_INTERVAL = 1000 * 60 * 24 * 30;
export const handler = async (
const GET = async (
_req: Request,
_ctx: HandlerContext,
) => {
@ -18,21 +20,16 @@ export const handler = async (
const query = u.searchParams.get("q");
if (!query) {
return new Response("Bad Request", {
status: 400,
});
throw new BadRequestError();
}
const headers = new Headers();
headers.append("Content-Type", "application/json");
const cacheId = `/movie/query/${query}`;
const cachedResponse = await cache.get<CachedMovieQuery>(cacheId);
if (
cachedResponse && Date.now() < (cachedResponse.lastUpdated + CACHE_INTERVAL)
) {
return new Response(JSON.stringify(cachedResponse.data), { headers });
return json(cachedResponse.data);
}
const res = await searchMovie(query);
@ -47,3 +44,7 @@ export const handler = async (
return new Response(JSON.stringify(res.results));
};
export const handler: Handlers = {
GET,
};

View File

@ -1,7 +1,6 @@
import { Handlers, PageProps } from "$fresh/server.ts";
import { MainLayout } from "@components/layouts/main.tsx";
import { Movie } from "@lib/movies.ts";
import { getMovie } from "../api/movies/[name].ts";
import { getMovie, Movie } from "@lib/resource/movies.ts";
import { RecipeHero } from "@components/RecipeHero.tsx";
import { KMenu } from "@islands/KMenu.tsx";

View File

@ -1,16 +1,12 @@
import { Handlers, PageProps } from "$fresh/server.ts";
import { RecipeCard } from "@components/RecipeCard.tsx";
import { MainLayout } from "@components/layouts/main.tsx";
import { Recipe } from "@lib/recipes.ts";
import { getRecipes } from "../api/recipes/index.ts";
import IconArrowLeft from "https://deno.land/x/tabler_icons_tsx@0.0.3/tsx/arrow-left.tsx";
import { getMovies } from "../api/movies/index.ts";
import { Movie } from "@lib/movies.ts";
import { getAllMovies, Movie } from "@lib/resource/movies.ts";
import { MovieCard } from "@components/MovieCard.tsx";
export const handler: Handlers<Movie[] | null> = {
async GET(_, ctx) {
const movies = await getMovies();
const movies = await getAllMovies();
return ctx.render(movies);
},
};

View File

@ -2,10 +2,9 @@ import { Handlers, PageProps } from "$fresh/server.ts";
import { IngredientsList } from "@islands/IngredientsList.tsx";
import { RecipeHero } from "@components/RecipeHero.tsx";
import { MainLayout } from "@components/layouts/main.tsx";
import { Recipe } from "@lib/recipes.ts";
import { getRecipe } from "../api/recipes/[name].ts";
import Counter from "@islands/Counter.tsx";
import { useSignal } from "@preact/signals";
import { getRecipe, Recipe } from "@lib/resource/recipes.ts";
export const handler: Handlers<Recipe | null> = {
async GET(_, ctx) {

View File

@ -1,13 +1,12 @@
import { Handlers, PageProps } from "$fresh/server.ts";
import { RecipeCard } from "@components/RecipeCard.tsx";
import { MainLayout } from "@components/layouts/main.tsx";
import { Recipe } from "@lib/recipes.ts";
import { getRecipes } from "../api/recipes/index.ts";
import { getAllRecipes, Recipe } from "@lib/resource/recipes.ts";
import IconArrowLeft from "https://deno.land/x/tabler_icons_tsx@0.0.3/tsx/arrow-left.tsx";
export const handler: Handlers<Recipe[] | null> = {
async GET(_, ctx) {
const recipes = await getRecipes();
const recipes = await getAllRecipes();
return ctx.render(recipes);
},
};