diff --git a/api/src/controllers/seed/insert/collection.ts b/api/src/controllers/seed/insert/collection.ts new file mode 100644 index 00000000..99890bfe --- /dev/null +++ b/api/src/controllers/seed/insert/collection.ts @@ -0,0 +1,18 @@ +import type { SeedCollection } from "~/models/collections"; +import { insertShow } from "./shows"; + +export const insertCollection = async (collection?: SeedCollection) => { + if (!collection) return null; + const { translations: colTrans, ...col } = collection; + // TODO: need to compute start/end year & if missing tags & genres + const { updated, status, ...ret } = await insertShow( + { + kind: "collection", + status: "unknown", + nextRefresh, + ...col, + }, + colTrans, + ); + return ret; +}; diff --git a/api/src/controllers/seed/insert/shows.ts b/api/src/controllers/seed/insert/shows.ts index 8986e33e..48e14d62 100644 --- a/api/src/controllers/seed/insert/shows.ts +++ b/api/src/controllers/seed/insert/shows.ts @@ -2,6 +2,7 @@ import { eq, sql } from "drizzle-orm"; import { db } from "~/db"; import { showTranslations, shows } from "~/db/schema"; import { conflictUpdateAllExcept } from "~/db/utils"; +import type { SeedCollection } from "~/models/collections"; import type { SeedMovie } from "~/models/movie"; import type { SeedSerie } from "~/models/serie"; import { getYear } from "~/utils"; @@ -12,7 +13,10 @@ type ShowTrans = typeof showTranslations.$inferInsert; export const insertShow = async ( show: Show, - translations: SeedMovie["translations"] | SeedSerie["translations"], + translations: + | SeedMovie["translations"] + | SeedSerie["translations"] + | SeedCollection["translations"], ) => { return await db.transaction(async (tx) => { const ret = await insertBaseShow(tx, show); @@ -77,13 +81,14 @@ async function insertBaseShow( // if at this point ret is still undefined, we could not reconciliate. // simply bail and let the caller handle this. - const [{ id }] = await db - .select({ id: shows.id }) + const [{ pk, id }] = await db + .select({ pk: shows.pk, id: shows.id }) .from(shows) .where(eq(shows.slug, show.slug)) .limit(1); return { status: 409 as const, + pk, id, slug: show.slug, }; diff --git a/api/src/controllers/seed/movies.ts b/api/src/controllers/seed/movies.ts index eeab21f5..c20188e0 100644 --- a/api/src/controllers/seed/movies.ts +++ b/api/src/controllers/seed/movies.ts @@ -1,6 +1,7 @@ import { t } from "elysia"; import type { SeedMovie } from "~/models/movie"; import { getYear } from "~/utils"; +import { insertCollection } from "./insert/collection"; import { insertEntries } from "./insert/entries"; import { insertShow } from "./insert/shows"; import { guessNextRefresh } from "./refresh"; @@ -11,6 +12,12 @@ export const SeedMovieResponse = t.Object({ videos: t.Array( t.Object({ slug: t.String({ format: "slug", examples: ["bubble-v2"] }) }), ), + collection: t.Nullable( + t.Object({ + id: t.String({ format: "uuid" }), + slug: t.String({ format: "slug", examples: ["sawano-collection"] }), + }), + ), }); export type SeedMovieResponse = typeof SeedMovieResponse.static; @@ -31,14 +38,17 @@ export const seedMovie = async ( seed.slug = `random-${getYear(seed.airDate)}`; } - const { translations, videos, ...bMovie } = seed; + const { translations, videos, collection, ...bMovie } = seed; const nextRefresh = guessNextRefresh(bMovie.airDate ?? new Date()); + const col = await insertCollection(collection); + const show = await insertShow( { kind: "movie", startAir: bMovie.airDate, nextRefresh, + collectionPk: col?.pk, ...bMovie, }, translations, @@ -65,5 +75,6 @@ export const seedMovie = async ( id: show.id, slug: show.slug, videos: entry.videos, + collection: col, }; }; diff --git a/api/src/controllers/seed/series.ts b/api/src/controllers/seed/series.ts index c8c2667d..124d3b9c 100644 --- a/api/src/controllers/seed/series.ts +++ b/api/src/controllers/seed/series.ts @@ -1,6 +1,7 @@ import { t } from "elysia"; import type { SeedSerie } from "~/models/serie"; import { getYear } from "~/utils"; +import { insertCollection } from "./insert/collection"; import { insertEntries } from "./insert/entries"; import { insertSeasons } from "./insert/seasons"; import { insertShow } from "./insert/shows"; @@ -35,6 +36,15 @@ export const SeedSerieResponse = t.Object({ slug: t.String({ format: "slug", examples: ["made-in-abyss-s1e1"] }), }), ), + collection: t.Nullable( + t.Object({ + id: t.String({ format: "uuid" }), + slug: t.String({ + format: "slug", + examples: ["made-in-abyss-collection"], + }), + }), + ), }); export type SeedSerieResponse = typeof SeedSerieResponse.static; @@ -55,13 +65,16 @@ export const seedSerie = async ( seed.slug = `random-${getYear(seed.startAir)}`; } - const { translations, seasons, entries, extras, ...serie } = seed; + const { translations, seasons, entries, extras, collection, ...serie } = seed; const nextRefresh = guessNextRefresh(serie.startAir ?? new Date()); + const col = await insertCollection(collection); + const show = await insertShow( { kind: "serie", nextRefresh, + collectionPk: col?.pk, ...serie, }, translations, @@ -82,5 +95,6 @@ export const seedSerie = async ( seasons: retSeasons, entries: retEntries, extras: retExtras, + collection: col, }; }; diff --git a/api/src/models/collections.ts b/api/src/models/collections.ts index ffa94233..d62f3632 100644 --- a/api/src/models/collections.ts +++ b/api/src/models/collections.ts @@ -52,7 +52,7 @@ export const Collection = t.Intersect([ export type Collection = Prettify; export const SeedCollection = t.Intersect([ - t.Omit(BaseCollection, ["createdAt", "nextRefresh"]), + t.Omit(BaseCollection, ["startAir", "endAir", "createdAt", "nextRefresh"]), t.Object({ slug: t.String({ format: "slug" }), translations: TranslationRecord(