wip: Allow videos to be joined on a post /movies

This commit is contained in:
Zoe Roux 2024-11-25 21:00:41 +01:00
parent 8253554304
commit 92ee0b2e7f
No known key found for this signature in database

View File

@ -2,19 +2,22 @@ import { db } from "~/db";
import { import {
entries, entries,
entryTranslations, entryTranslations,
entryVideoJointure,
shows, shows,
showTranslations, showTranslations,
videos,
} from "~/db/schema"; } from "~/db/schema";
import type { SeedMovie } from "~/models/movie"; import type { SeedMovie } from "~/models/movie";
import { processOptImage } from "./images"; import { processOptImage } from "./images";
import { guessNextRefresh } from "./refresh"; import { guessNextRefresh } from "./refresh";
import { count, eq, inArray, sql } from "drizzle-orm";
type Show = typeof shows.$inferInsert; type Show = typeof shows.$inferInsert;
type ShowTrans = typeof showTranslations.$inferInsert; type ShowTrans = typeof showTranslations.$inferInsert;
type Entry = typeof entries.$inferInsert; type Entry = typeof entries.$inferInsert;
export const seedMovie = async (seed: SeedMovie) => { export const seedMovie = async (seed: SeedMovie) => {
const { translations, videos, ...bMovie } = seed; const { translations, videos: vids, ...bMovie } = seed;
const ret = await db.transaction(async (tx) => { const ret = await db.transaction(async (tx) => {
const movie: Show = { const movie: Show = {
@ -55,10 +58,54 @@ export const seedMovie = async (seed: SeedMovie) => {
return { ...ret, entry: entry.pk }; return { ...ret, entry: entry.pk };
}); });
// TODO: insert entry-video links if (vids) {
// await db.transaction(async tx => { await db.transaction(async (tx) => {
// await tx.insert(videos).values(videos); const pks = await tx
// }); .insert(entryVideoJointure)
.select(
tx
.select({
entry: sql<number>`${ret.entry}`.as("entry"),
video: videos.pk,
})
.from(videos)
.where(inArray(videos.id, vids)),
)
.onConflictDoNothing()
.returning({ pk: entryVideoJointure.video });
const toto = tx
.select({ count: count(videos.rendering) })
.from(videos)
.innerJoin(
entryVideoJointure,
eq(videos.pk, entryVideoJointure.video),
)
.where(entryVideoJointure.entry, "");
return await tx
.update(videos)
.set({
slug: sql<string>`
concat(
${entries.slug},
case when ${videos.part} <> null then concat("-p", ${videos.part}) else "" end,
case when ${videos.version} <> 1 then concat("-v", ${videos.version}) else "" end,
${}
)
`,
})
.from(entries)
.where(
inArray(
videos.pk,
pks.map((x) => x.pk),
),
)
.returning({ id: videos.id, slug: videos.slug });
});
}
return ret.id; return ret.id;
}; };