Add with=videos support for movies

This commit is contained in:
Zoe Roux 2025-03-09 18:41:18 +01:00
parent e0ad458d73
commit b093df96ec
No known key found for this signature in database
3 changed files with 55 additions and 3 deletions

View File

@ -2,11 +2,14 @@ import type { StaticDecode } from "@sinclair/typebox";
import { type SQL, and, eq, exists, sql } from "drizzle-orm";
import { db } from "~/db";
import {
entries,
entryVideoJoin,
showStudioJoin,
showTranslations,
shows,
studioTranslations,
studios,
videos,
} from "~/db/schema";
import {
coalesce,
@ -28,6 +31,7 @@ import {
keysetPaginate,
sortToSql,
} from "~/models/utils";
import type { EmbeddedVideo } from "~/models/video";
export const showFilters: FilterDef = {
genres: {
@ -120,7 +124,24 @@ const showRelations = {
},
// only available for movies
videos: () => {
throw new Error();
const { guess, createdAt, updatedAt, ...videosCol } = getColumns(videos);
return db
.select({
videos: coalesce(
jsonbAgg(
jsonbBuildObject<EmbeddedVideo>({
slug: entryVideoJoin.slug,
...videosCol,
}),
),
sql`'[]'::jsonb`,
).as("videos"),
})
.from(entryVideoJoin)
.where(eq(entryVideoJoin.entryPk, entries.pk))
.leftJoin(entries, eq(entries.showPk, shows.pk))
.leftJoin(videos, eq(videos.pk, entryVideoJoin.videoPk))
.as("videos");
},
};

View File

@ -14,7 +14,7 @@ import {
TranslationRecord,
} from "./utils";
import { Original } from "./utils/original";
import { Video } from "./video";
import { EmbeddedVideo } from "./video";
export const MovieStatus = t.UnionEnum(["unknown", "finished", "planned"]);
export type MovieStatus = typeof MovieStatus.static;
@ -62,7 +62,7 @@ export const FullMovie = t.Intersect([
Movie,
t.Object({
translations: t.Optional(TranslationRecord(MovieTranslation)),
videos: t.Optional(t.Array(Video)),
videos: t.Optional(t.Array(EmbeddedVideo)),
studios: t.Optional(t.Array(Studio)),
}),
]);

View File

@ -134,4 +134,35 @@ describe("Get movie", () => {
expectStatus(resp, body).toBe(200);
expect(body.isAvailable).toBe(false);
});
it("with=translations", async () => {
const [resp, body] = await getMovie(bubble.slug, {
with: ["translations"],
});
expectStatus(resp, body).toBe(200);
expect(body.translations).toMatchObject({
en: { name: bubble.translations.en.name },
ja: { name: bubble.translations.ja.name },
});
});
it("with=translations,videos", async () => {
const [resp, body] = await getMovie(bubble.slug, {
with: ["translations", "videos"],
});
expectStatus(resp, body).toBe(200);
expect(body.translations).toMatchObject({
en: { name: bubble.translations.en.name },
ja: { name: bubble.translations.ja.name },
});
expect(body.videos).toBeArrayOfSize(bubble.videos!.length);
expect(body.videos[0]).toMatchObject({
path: bubbleVideo.path,
slug: bubbleVideo.slug,
version: bubbleVideo.version,
rendering: bubbleVideo.rendering,
part: bubbleVideo.part,
});
});
});