mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-05-30 19:54:16 -04:00
Move video slug to jointure
This commit is contained in:
parent
92ee0b2e7f
commit
c20aa862a9
@ -1,11 +1,12 @@
|
|||||||
import Elysia, { t } from "elysia";
|
import Elysia, { t } from "elysia";
|
||||||
import { Movie, SeedMovie } from "~/models/movie";
|
import { Movie, SeedMovie } from "~/models/movie";
|
||||||
import { seedMovie } from "./movies";
|
import { seedMovie, SeedMovieResponse } from "./movies";
|
||||||
|
|
||||||
export const seed = new Elysia()
|
export const seed = new Elysia()
|
||||||
.model({
|
.model({
|
||||||
movie: Movie,
|
movie: Movie,
|
||||||
"seed-movie": SeedMovie,
|
"seed-movie": SeedMovie,
|
||||||
|
"seed-movie-response": SeedMovieResponse,
|
||||||
error: t.String(),
|
error: t.String(),
|
||||||
})
|
})
|
||||||
.post(
|
.post(
|
||||||
@ -15,7 +16,7 @@ export const seed = new Elysia()
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
body: "seed-movie",
|
body: "seed-movie",
|
||||||
response: { 200: "movie", 400: "error" },
|
response: { 200: "seed-movie-response", 400: "error" },
|
||||||
tags: ["movies"],
|
tags: ["movies"],
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
@ -10,13 +10,23 @@ import {
|
|||||||
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";
|
import { inArray, sql } from "drizzle-orm";
|
||||||
|
import { t } from "elysia";
|
||||||
|
import { Resource } from "~/models/utils";
|
||||||
|
|
||||||
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 SeedMovieResponse = t.Intersect([
|
||||||
|
Resource,
|
||||||
|
t.Object({ videos: t.Array(Resource) }),
|
||||||
|
]);
|
||||||
|
export type SeedMovieResponse = typeof SeedMovieResponse.static;
|
||||||
|
|
||||||
|
export const seedMovie = async (
|
||||||
|
seed: SeedMovie,
|
||||||
|
): Promise<SeedMovieResponse> => {
|
||||||
const { translations, videos: vids, ...bMovie } = seed;
|
const { translations, videos: vids, ...bMovie } = seed;
|
||||||
|
|
||||||
const ret = await db.transaction(async (tx) => {
|
const ret = await db.transaction(async (tx) => {
|
||||||
@ -29,7 +39,7 @@ export const seedMovie = async (seed: SeedMovie) => {
|
|||||||
const [ret] = await tx
|
const [ret] = await tx
|
||||||
.insert(shows)
|
.insert(shows)
|
||||||
.values(movie)
|
.values(movie)
|
||||||
.returning({ pk: shows.pk, id: shows.id });
|
.returning({ pk: shows.pk, id: shows.id, slug: shows.slug });
|
||||||
|
|
||||||
// even if never shown to the user, a movie still has an entry.
|
// even if never shown to the user, a movie still has an entry.
|
||||||
const movieEntry: Entry = { type: "movie", ...bMovie };
|
const movieEntry: Entry = { type: "movie", ...bMovie };
|
||||||
@ -58,54 +68,37 @@ export const seedMovie = async (seed: SeedMovie) => {
|
|||||||
return { ...ret, entry: entry.pk };
|
return { ...ret, entry: entry.pk };
|
||||||
});
|
});
|
||||||
|
|
||||||
|
let retVideos: { id: string; slug: string }[] = [];
|
||||||
if (vids) {
|
if (vids) {
|
||||||
await db.transaction(async (tx) => {
|
retVideos = await db.transaction(async (tx) => {
|
||||||
const pks = await tx
|
return await tx
|
||||||
.insert(entryVideoJointure)
|
.insert(entryVideoJointure)
|
||||||
.select(
|
.select(
|
||||||
tx
|
tx
|
||||||
.select({
|
.select({
|
||||||
entry: sql<number>`${ret.entry}`.as("entry"),
|
entry: sql<number>`${ret.entry}`.as("entry"),
|
||||||
video: videos.pk,
|
video: videos.pk,
|
||||||
|
// TODO: do not add rendering if all videos of the entry have the same rendering
|
||||||
|
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,
|
||||||
|
"-", ${videos.rendering}
|
||||||
|
)
|
||||||
|
`.as("slug"),
|
||||||
})
|
})
|
||||||
.from(videos)
|
.from(videos)
|
||||||
.where(inArray(videos.id, vids)),
|
.where(inArray(videos.id, vids)),
|
||||||
)
|
)
|
||||||
.onConflictDoNothing()
|
.onConflictDoNothing()
|
||||||
.returning({ pk: entryVideoJointure.video });
|
.returning({ id: videos.id, slug: entryVideoJointure.slug });
|
||||||
|
|
||||||
|
|
||||||
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 {
|
||||||
|
id: ret.id,
|
||||||
|
slug: ret.slug,
|
||||||
|
videos: retVideos,
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { relations, sql } from "drizzle-orm";
|
import { sql } from "drizzle-orm";
|
||||||
import {
|
import {
|
||||||
check,
|
check,
|
||||||
integer,
|
integer,
|
||||||
@ -17,7 +17,6 @@ export const videos = schema.table(
|
|||||||
{
|
{
|
||||||
pk: integer().primaryKey().generatedAlwaysAsIdentity(),
|
pk: integer().primaryKey().generatedAlwaysAsIdentity(),
|
||||||
id: uuid().notNull().unique().defaultRandom(),
|
id: uuid().notNull().unique().defaultRandom(),
|
||||||
slug: varchar({ length: 255 }).unique(),
|
|
||||||
path: text().notNull().unique(),
|
path: text().notNull().unique(),
|
||||||
rendering: text().notNull(),
|
rendering: text().notNull(),
|
||||||
part: integer(),
|
part: integer(),
|
||||||
@ -43,6 +42,7 @@ export const entryVideoJointure = schema.table(
|
|||||||
video: integer()
|
video: integer()
|
||||||
.notNull()
|
.notNull()
|
||||||
.references(() => videos.pk, { onDelete: "cascade" }),
|
.references(() => videos.pk, { onDelete: "cascade" }),
|
||||||
|
slug: varchar({ length: 255 }).notNull().unique(),
|
||||||
},
|
},
|
||||||
(t) => [primaryKey({ columns: [t.entry, t.video] })],
|
(t) => [primaryKey({ columns: [t.entry, t.video] })],
|
||||||
);
|
);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user