feat: add initial command pallete
This commit is contained in:
47
routes/api/tmdb/[id].ts
Normal file
47
routes/api/tmdb/[id].ts
Normal file
@ -0,0 +1,47 @@
|
||||
import { HandlerContext } from "$fresh/server.ts";
|
||||
import { getMovie } from "@lib/tmdb.ts";
|
||||
import * as cache from "@lib/cache/cache.ts";
|
||||
|
||||
type CachedMovieCredits = {
|
||||
lastUpdated: number;
|
||||
data: unknown;
|
||||
};
|
||||
|
||||
const CACHE_INTERVAL = 1000 * 60 * 24 * 30;
|
||||
|
||||
export const handler = async (
|
||||
_req: Request,
|
||||
_ctx: HandlerContext,
|
||||
) => {
|
||||
const id = _ctx.params.id;
|
||||
|
||||
if (!id) {
|
||||
return new Response("Bad Request", {
|
||||
status: 400,
|
||||
});
|
||||
}
|
||||
|
||||
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 });
|
||||
}
|
||||
|
||||
const res = await getMovie(+id);
|
||||
|
||||
cache.set(
|
||||
cacheId,
|
||||
JSON.stringify({
|
||||
lastUpdated: Date.now(),
|
||||
data: res,
|
||||
}),
|
||||
);
|
||||
|
||||
return new Response(JSON.stringify(res));
|
||||
};
|
46
routes/api/tmdb/credits/[id].ts
Normal file
46
routes/api/tmdb/credits/[id].ts
Normal file
@ -0,0 +1,46 @@
|
||||
import { HandlerContext } from "$fresh/server.ts";
|
||||
import { getMovieCredits } from "@lib/tmdb.ts";
|
||||
import * as cache from "@lib/cache/cache.ts";
|
||||
|
||||
type CachedMovieCredits = {
|
||||
lastUpdated: number;
|
||||
data: unknown;
|
||||
};
|
||||
|
||||
const CACHE_INTERVAL = 1000 * 60 * 24 * 30;
|
||||
|
||||
export const handler = async (
|
||||
_req: Request,
|
||||
_ctx: HandlerContext,
|
||||
) => {
|
||||
const id = _ctx.params.id;
|
||||
|
||||
if (!id) {
|
||||
return new Response("Bad Request", {
|
||||
status: 400,
|
||||
});
|
||||
}
|
||||
|
||||
const headers = new Headers();
|
||||
headers.append("Content-Type", "application/json");
|
||||
|
||||
const cacheId = `/movie/credits/${id}`;
|
||||
|
||||
const cachedResponse = await cache.get<CachedMovieCredits>(cacheId);
|
||||
if (
|
||||
cachedResponse && Date.now() < (cachedResponse.lastUpdated + CACHE_INTERVAL)
|
||||
) {
|
||||
return new Response(JSON.stringify(cachedResponse.data), { headers });
|
||||
}
|
||||
|
||||
const res = await getMovieCredits(+id);
|
||||
cache.set(
|
||||
cacheId,
|
||||
JSON.stringify({
|
||||
lastUpdated: Date.now(),
|
||||
data: res,
|
||||
}),
|
||||
);
|
||||
|
||||
return new Response(JSON.stringify(res));
|
||||
};
|
49
routes/api/tmdb/query.ts
Normal file
49
routes/api/tmdb/query.ts
Normal file
@ -0,0 +1,49 @@
|
||||
import { HandlerContext } from "$fresh/server.ts";
|
||||
import { searchMovie } from "@lib/tmdb.ts";
|
||||
import * as cache from "@lib/cache/cache.ts";
|
||||
|
||||
type CachedMovieQuery = {
|
||||
lastUpdated: number;
|
||||
data: unknown;
|
||||
};
|
||||
|
||||
const CACHE_INTERVAL = 1000 * 60 * 24 * 30;
|
||||
|
||||
export const handler = async (
|
||||
_req: Request,
|
||||
_ctx: HandlerContext,
|
||||
) => {
|
||||
const u = new URL(_req.url);
|
||||
|
||||
const query = u.searchParams.get("q");
|
||||
|
||||
if (!query) {
|
||||
return new Response("Bad Request", {
|
||||
status: 400,
|
||||
});
|
||||
}
|
||||
|
||||
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 });
|
||||
}
|
||||
|
||||
const res = await searchMovie(query);
|
||||
|
||||
cache.set(
|
||||
cacheId,
|
||||
JSON.stringify({
|
||||
lastUpdated: Date.now(),
|
||||
data: res,
|
||||
}),
|
||||
);
|
||||
|
||||
return new Response(JSON.stringify(res.results));
|
||||
};
|
@ -3,6 +3,7 @@ import { MainLayout } from "@components/layouts/main.tsx";
|
||||
import { Movie } from "@lib/movies.ts";
|
||||
import { getMovie } from "../api/movies/[name].ts";
|
||||
import { RecipeHero } from "@components/RecipeHero.tsx";
|
||||
import { KMenu } from "@islands/KMenu.tsx";
|
||||
|
||||
export const handler: Handlers<Movie | null> = {
|
||||
async GET(_, ctx) {
|
||||
@ -17,6 +18,7 @@ export default function Greet(props: PageProps<Movie>) {
|
||||
return (
|
||||
<MainLayout url={props.url}>
|
||||
<RecipeHero data={movie} backlink="/movies" />
|
||||
<KMenu type="main" context={movie} />
|
||||
<div class="px-8 text-white mt-10">
|
||||
<pre
|
||||
class="whitespace-break-spaces"
|
||||
|
Reference in New Issue
Block a user