mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-05-24 02:02:36 -04:00
Move seed function to separate file
This commit is contained in:
parent
30d5d65755
commit
31b7c0e035
@ -1,18 +1,6 @@
|
||||
import Elysia, { t } from "elysia";
|
||||
import { Movie, SeedMovie } from "~/models/movie";
|
||||
import { db } from "~/db";
|
||||
import {
|
||||
shows,
|
||||
showTranslations,
|
||||
entries,
|
||||
entryTranslations,
|
||||
} from "~/db/schema";
|
||||
import { guessNextRefresh } from "./refresh";
|
||||
import { processOptImage } from "./images";
|
||||
|
||||
type Show = typeof shows.$inferInsert;
|
||||
type ShowTrans = typeof showTranslations.$inferInsert;
|
||||
type Entry = typeof entries.$inferInsert;
|
||||
import { seedMovie } from "./movies";
|
||||
|
||||
export const seed = new Elysia()
|
||||
.model({
|
||||
@ -23,53 +11,7 @@ export const seed = new Elysia()
|
||||
.post(
|
||||
"/movies",
|
||||
async ({ body }) => {
|
||||
const { translations, videos, ...bMovie } = body;
|
||||
|
||||
const ret = await db.transaction(async (tx) => {
|
||||
const movie: Show = {
|
||||
kind: "movie",
|
||||
startAir: bMovie.airDate,
|
||||
nextRefresh: guessNextRefresh(bMovie.airDate ?? new Date()),
|
||||
...bMovie,
|
||||
};
|
||||
const [ret] = await tx
|
||||
.insert(shows)
|
||||
.values(movie)
|
||||
.returning({ pk: shows.pk, id: shows.id });
|
||||
|
||||
// even if never shown to the user, a movie still has an entry.
|
||||
const movieEntry: Entry = { type: "movie", ...bMovie };
|
||||
const [entry] = await tx
|
||||
.insert(entries)
|
||||
.values(movieEntry)
|
||||
.returning({ pk: entries.pk });
|
||||
|
||||
const trans: ShowTrans[] = await Promise.all(
|
||||
Object.entries(translations).map(async ([lang, tr]) => ({
|
||||
pk: ret.pk,
|
||||
// TODO: normalize lang or error if invalid
|
||||
language: lang,
|
||||
...tr,
|
||||
poster: await processOptImage(tr.poster),
|
||||
thumbnail: await processOptImage(tr.thumbnail),
|
||||
logo: await processOptImage(tr.logo),
|
||||
banner: await processOptImage(tr.banner),
|
||||
})),
|
||||
);
|
||||
await tx.insert(showTranslations).values(trans);
|
||||
|
||||
const entryTrans = trans.map((x) => ({ ...x, pk: entry.pk }));
|
||||
await tx.insert(entryTranslations).values(entryTrans);
|
||||
|
||||
return { ...ret, entry: entry.pk };
|
||||
});
|
||||
|
||||
// TODO: insert entry-video links
|
||||
// await db.transaction(async tx => {
|
||||
// await tx.insert(videos).values(videos);
|
||||
// });
|
||||
|
||||
return ret.id;
|
||||
return await seedMovie(body);
|
||||
},
|
||||
{
|
||||
body: "seed-movie",
|
||||
|
64
api/src/controllers/seed/movies.ts
Normal file
64
api/src/controllers/seed/movies.ts
Normal file
@ -0,0 +1,64 @@
|
||||
import { db } from "~/db";
|
||||
import {
|
||||
entries,
|
||||
entryTranslations,
|
||||
shows,
|
||||
showTranslations,
|
||||
} from "~/db/schema";
|
||||
import type { SeedMovie } from "~/models/movie";
|
||||
import { processOptImage } from "./images";
|
||||
import { guessNextRefresh } from "./refresh";
|
||||
|
||||
type Show = typeof shows.$inferInsert;
|
||||
type ShowTrans = typeof showTranslations.$inferInsert;
|
||||
type Entry = typeof entries.$inferInsert;
|
||||
|
||||
export const seedMovie = async (seed: SeedMovie) => {
|
||||
const { translations, videos, ...bMovie } = seed;
|
||||
|
||||
const ret = await db.transaction(async (tx) => {
|
||||
const movie: Show = {
|
||||
kind: "movie",
|
||||
startAir: bMovie.airDate,
|
||||
nextRefresh: guessNextRefresh(bMovie.airDate ?? new Date()),
|
||||
...bMovie,
|
||||
};
|
||||
const [ret] = await tx
|
||||
.insert(shows)
|
||||
.values(movie)
|
||||
.returning({ pk: shows.pk, id: shows.id });
|
||||
|
||||
// even if never shown to the user, a movie still has an entry.
|
||||
const movieEntry: Entry = { type: "movie", ...bMovie };
|
||||
const [entry] = await tx
|
||||
.insert(entries)
|
||||
.values(movieEntry)
|
||||
.returning({ pk: entries.pk });
|
||||
|
||||
const trans: ShowTrans[] = await Promise.all(
|
||||
Object.entries(translations).map(async ([lang, tr]) => ({
|
||||
pk: ret.pk,
|
||||
// TODO: normalize lang or error if invalid
|
||||
language: lang,
|
||||
...tr,
|
||||
poster: await processOptImage(tr.poster),
|
||||
thumbnail: await processOptImage(tr.thumbnail),
|
||||
logo: await processOptImage(tr.logo),
|
||||
banner: await processOptImage(tr.banner),
|
||||
})),
|
||||
);
|
||||
await tx.insert(showTranslations).values(trans);
|
||||
|
||||
const entryTrans = trans.map((x) => ({ ...x, pk: entry.pk }));
|
||||
await tx.insert(entryTranslations).values(entryTrans);
|
||||
|
||||
return { ...ret, entry: entry.pk };
|
||||
});
|
||||
|
||||
// TODO: insert entry-video links
|
||||
// await db.transaction(async tx => {
|
||||
// await tx.insert(videos).values(videos);
|
||||
// });
|
||||
|
||||
return ret.id;
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user