From 710675180c524194ae20cbbe356239757cf9972a Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Sun, 2 Mar 2025 23:00:27 +0100 Subject: [PATCH] Add studio insert method --- api/src/controllers/seed/insert/studios.ts | 55 ++++++++++++++++++++++ api/src/controllers/seed/movies.ts | 12 ++++- api/src/controllers/seed/series.ts | 20 +++++++- api/src/db/schema/studios.ts | 2 +- 4 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 api/src/controllers/seed/insert/studios.ts diff --git a/api/src/controllers/seed/insert/studios.ts b/api/src/controllers/seed/insert/studios.ts new file mode 100644 index 00000000..53ed9fbc --- /dev/null +++ b/api/src/controllers/seed/insert/studios.ts @@ -0,0 +1,55 @@ +import { db } from "~/db"; +import { showStudioJoin, studioTranslations, studios } from "~/db/schema"; +import { conflictUpdateAllExcept } from "~/db/utils"; +import type { SeedStudio } from "~/models/studio"; +import { processOptImage } from "../images"; + +type StudioI = typeof studios.$inferInsert; +type StudioTransI = typeof studioTranslations.$inferInsert; + +export const insertStudios = async (seed: SeedStudio[], showPk: number) => { + if (!seed.length) return []; + + return await db.transaction(async (tx) => { + const vals: StudioI[] = seed.map((x) => { + const { translations, ...item } = x; + return item; + }); + + const ret = await tx + .insert(studios) + .values(vals) + .onConflictDoUpdate({ + target: studios.slug, + set: conflictUpdateAllExcept(studios, [ + "pk", + "id", + "slug", + "createdAt", + ]), + }) + .returning({ pk: studios.pk, id: studios.id, slug: studios.slug }); + + const trans: StudioTransI[] = seed.flatMap((x, i) => + Object.entries(x.translations).map(([lang, tr]) => ({ + pk: ret[i].pk, + language: lang, + name: tr.name, + logo: processOptImage(tr.logo), + })), + ); + await tx + .insert(studioTranslations) + .values(trans) + .onConflictDoUpdate({ + target: [studioTranslations.pk, studioTranslations.language], + set: conflictUpdateAllExcept(studioTranslations, ["pk", "language"]), + }); + + await tx + .insert(showStudioJoin) + .values(ret.map((studio) => ({ show: showPk, studio: studio.pk }))) + .onConflictDoNothing(); + return ret; + }); +}; diff --git a/api/src/controllers/seed/movies.ts b/api/src/controllers/seed/movies.ts index 968461d2..7ca219a7 100644 --- a/api/src/controllers/seed/movies.ts +++ b/api/src/controllers/seed/movies.ts @@ -5,6 +5,7 @@ import { insertCollection } from "./insert/collection"; import { insertEntries } from "./insert/entries"; import { insertShow } from "./insert/shows"; import { guessNextRefresh } from "./refresh"; +import { insertStudios } from "./insert/studios"; export const SeedMovieResponse = t.Object({ id: t.String({ format: "uuid" }), @@ -18,6 +19,12 @@ export const SeedMovieResponse = t.Object({ slug: t.String({ format: "slug", examples: ["sawano-collection"] }), }), ), + studios: t.Array( + t.Object({ + id: t.String({ format: "uuid" }), + slug: t.String({ format: "slug", examples: ["disney"] }), + }), + ), }); export type SeedMovieResponse = typeof SeedMovieResponse.static; @@ -38,7 +45,7 @@ export const seedMovie = async ( seed.slug = `random-${getYear(seed.airDate)}`; } - const { translations, videos, collection, ...bMovie } = seed; + const { translations, videos, collection, studios, ...bMovie } = seed; const nextRefresh = guessNextRefresh(bMovie.airDate ?? new Date()); const col = await insertCollection(collection, { @@ -74,11 +81,14 @@ export const seedMovie = async ( }, ]); + const retStudios = await insertStudios(studios, show.pk); + return { updated: show.updated, id: show.id, slug: show.slug, videos: entry.videos, collection: col, + studios: retStudios, }; }; diff --git a/api/src/controllers/seed/series.ts b/api/src/controllers/seed/series.ts index 53e9e777..f1761b92 100644 --- a/api/src/controllers/seed/series.ts +++ b/api/src/controllers/seed/series.ts @@ -5,6 +5,7 @@ import { insertCollection } from "./insert/collection"; import { insertEntries } from "./insert/entries"; import { insertSeasons } from "./insert/seasons"; import { insertShow } from "./insert/shows"; +import { insertStudios } from "./insert/studios"; import { guessNextRefresh } from "./refresh"; export const SeedSerieResponse = t.Object({ @@ -45,6 +46,12 @@ export const SeedSerieResponse = t.Object({ }), }), ), + studios: t.Array( + t.Object({ + id: t.String({ format: "uuid" }), + slug: t.String({ format: "slug", examples: ["mappa"] }), + }), + ), }); export type SeedSerieResponse = typeof SeedSerieResponse.static; @@ -65,7 +72,15 @@ export const seedSerie = async ( seed.slug = `random-${getYear(seed.startAir)}`; } - const { translations, seasons, entries, extras, collection, ...serie } = seed; + const { + translations, + seasons, + entries, + extras, + collection, + studios, + ...serie + } = seed; const nextRefresh = guessNextRefresh(serie.startAir ?? new Date()); const col = await insertCollection(collection, { @@ -92,6 +107,8 @@ export const seedSerie = async ( (extras ?? []).map((x) => ({ ...x, kind: "extra", extraKind: x.kind })), ); + const retStudios = await insertStudios(studios, show.pk); + return { updated: show.updated, id: show.id, @@ -100,5 +117,6 @@ export const seedSerie = async ( entries: retEntries, extras: retExtras, collection: col, + studios: retStudios, }; }; diff --git a/api/src/db/schema/studios.ts b/api/src/db/schema/studios.ts index 29a0ddba..7c0dd7d7 100644 --- a/api/src/db/schema/studios.ts +++ b/api/src/db/schema/studios.ts @@ -25,7 +25,7 @@ export const studios = schema.table("studios", { .$onUpdate(() => sql`now()`), }); -export const studio_translations = schema.table( +export const studioTranslations = schema.table( "studio_translations", { pk: integer()