feat: add loading of recommendations to movie page
This commit is contained in:
37
routes/api/recommendation/data.ts
Normal file
37
routes/api/recommendation/data.ts
Normal file
@ -0,0 +1,37 @@
|
||||
import { Handlers } from "$fresh/server.ts";
|
||||
import { AccessDeniedError } from "@lib/errors.ts";
|
||||
import { getAllRecommendations } from "@lib/recommendation.ts";
|
||||
import { json } from "@lib/helpers.ts";
|
||||
|
||||
export const handler: Handlers = {
|
||||
async GET(_, ctx) {
|
||||
const session = ctx.state.session;
|
||||
if (!session) {
|
||||
throw new AccessDeniedError();
|
||||
}
|
||||
|
||||
const recs = await getAllRecommendations();
|
||||
|
||||
const allKeywords: Record<string, number> = {};
|
||||
|
||||
for (const rec of recs) {
|
||||
if (rec.keywords?.length) {
|
||||
for (const keyword of rec.keywords) {
|
||||
if (keyword in allKeywords) {
|
||||
allKeywords[keyword] += 1;
|
||||
} else {
|
||||
allKeywords[keyword] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const keywords = Object.entries(allKeywords).sort((a, b) =>
|
||||
a[1] > b[1] ? -1 : 1
|
||||
).slice(0, 100);
|
||||
|
||||
return json({
|
||||
keywords,
|
||||
});
|
||||
},
|
||||
};
|
17
routes/api/recommendation/movie/[id].ts
Normal file
17
routes/api/recommendation/movie/[id].ts
Normal file
@ -0,0 +1,17 @@
|
||||
import { Handlers } from "$fresh/server.ts";
|
||||
import { AccessDeniedError } from "@lib/errors.ts";
|
||||
import { getSimilarMovies } from "@lib/recommendation.ts";
|
||||
import { json } from "@lib/helpers.ts";
|
||||
|
||||
export const handler: Handlers = {
|
||||
async GET(_, ctx) {
|
||||
const session = ctx.state.session;
|
||||
if (!session) {
|
||||
throw new AccessDeniedError();
|
||||
}
|
||||
|
||||
const recommendations = await getSimilarMovies(ctx.params.id);
|
||||
|
||||
return json(recommendations);
|
||||
},
|
||||
};
|
@ -6,6 +6,7 @@ import { HashTags } from "@components/HashTags.tsx";
|
||||
import { renderMarkdown } from "@lib/documents.ts";
|
||||
import { KMenu } from "@islands/KMenu.tsx";
|
||||
import { RedirectSearchHandler } from "@islands/Search.tsx";
|
||||
import { Recommendations } from "@islands/Recommendations.tsx";
|
||||
|
||||
export default async function Greet(
|
||||
props: PageProps<{ movie: Movie; session: Record<string, string> }>,
|
||||
@ -52,6 +53,8 @@ export default async function Greet(
|
||||
>
|
||||
{content}
|
||||
</pre>
|
||||
|
||||
<Recommendations id={movie.id} type="movie"></Recommendations>
|
||||
</div>
|
||||
</MainLayout>
|
||||
);
|
||||
|
Reference in New Issue
Block a user