mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-07-09 03:04:20 -04:00
Fix POST /videos
This commit is contained in:
parent
d2bb37b3a7
commit
07a41bb175
@ -161,8 +161,7 @@ export const videosH = new Elysia({ prefix: "/videos", tags: ["videos"] })
|
||||
.post(
|
||||
"",
|
||||
async ({ body, error }) => {
|
||||
const vidsI = db.$with("vidsI").as(
|
||||
db
|
||||
const vids = await db
|
||||
.insert(videos)
|
||||
.values(body)
|
||||
.onConflictDoUpdate({
|
||||
@ -173,8 +172,38 @@ export const videosH = new Elysia({ prefix: "/videos", tags: ["videos"] })
|
||||
pk: videos.pk,
|
||||
id: videos.id,
|
||||
path: videos.path,
|
||||
}),
|
||||
});
|
||||
|
||||
const vidEntries = body.flatMap((x) => {
|
||||
if (!x.for) return [];
|
||||
return x.for.map((e) => ({
|
||||
video: vids.find((v) => v.path === x.path)!.pk,
|
||||
path: x.path,
|
||||
needRendering: x.for!.length > 1,
|
||||
entry: {
|
||||
...e,
|
||||
movie:
|
||||
"movie" in e
|
||||
? isUuid(e.movie)
|
||||
? { id: e.movie }
|
||||
: { slug: e.movie }
|
||||
: undefined,
|
||||
serie:
|
||||
"serie" in e
|
||||
? isUuid(e.serie)
|
||||
? { id: e.serie }
|
||||
: { slug: e.serie }
|
||||
: undefined,
|
||||
},
|
||||
}));
|
||||
});
|
||||
|
||||
if (!vidEntries.length) {
|
||||
return error(
|
||||
201,
|
||||
vids.map((x) => ({ id: x.id, path: x.path, entries: [] })),
|
||||
);
|
||||
}
|
||||
|
||||
const entriesQ = db
|
||||
.select({
|
||||
@ -197,45 +226,18 @@ export const videosH = new Elysia({ prefix: "/videos", tags: ["videos"] })
|
||||
.where(eq(entryVideoJoin.entryPk, entriesQ.pk));
|
||||
|
||||
const ret = await db
|
||||
.with(vidsI)
|
||||
.insert(entryVideoJoin)
|
||||
.select(
|
||||
db
|
||||
.select({
|
||||
entry: entries.pk,
|
||||
video: vidsI.pk,
|
||||
video: sql`j.video`,
|
||||
slug: computeVideoSlug(
|
||||
entriesQ.showSlug,
|
||||
sql`j.needRendering::boolean || exists(${hasRenderingQ})`,
|
||||
),
|
||||
})
|
||||
.from(
|
||||
values(
|
||||
body.flatMap((x) => {
|
||||
if (!x.for) return [];
|
||||
return x.for.map((e) => ({
|
||||
path: x.path,
|
||||
needRendering: x.for!.length > 1,
|
||||
entry: {
|
||||
...e,
|
||||
movie:
|
||||
"movie" in e
|
||||
? isUuid(e.movie)
|
||||
? { id: e.movie }
|
||||
: { slug: e.movie }
|
||||
: undefined,
|
||||
serie:
|
||||
"serie" in e
|
||||
? isUuid(e.serie)
|
||||
? { id: e.serie }
|
||||
: { slug: e.serie }
|
||||
: undefined,
|
||||
},
|
||||
}));
|
||||
}),
|
||||
).as("j"),
|
||||
)
|
||||
.innerJoin(vidsI, eq(vidsI.path, sql`j.path`))
|
||||
.from(values(vidEntries).as("j"))
|
||||
.innerJoin(
|
||||
entriesQ,
|
||||
or(
|
||||
@ -279,11 +281,20 @@ export const videosH = new Elysia({ prefix: "/videos", tags: ["videos"] })
|
||||
.returning({
|
||||
slug: entryVideoJoin.slug,
|
||||
entryPk: entryVideoJoin.entryPk,
|
||||
id: vidsI.id,
|
||||
path: vidsI.path,
|
||||
videoPk: entryVideoJoin.videoPk,
|
||||
});
|
||||
return error(201, ret);
|
||||
// return error(201, ret.map(x => ({ id: x.id, slug: x.})));
|
||||
const entr = ret.reduce(
|
||||
(acc, x) => {
|
||||
acc[x.videoPk] ??= [];
|
||||
acc[x.videoPk].push({ slug: x.slug });
|
||||
return acc;
|
||||
},
|
||||
{} as Record<number, { slug: string }[]>,
|
||||
);
|
||||
return error(
|
||||
201,
|
||||
vids.map((x) => ({ id: x.id, path: x.path, entries: entr[x.pk] })),
|
||||
);
|
||||
},
|
||||
{
|
||||
detail: {
|
||||
|
@ -76,6 +76,7 @@ export function sqlarr(array: unknown[]) {
|
||||
// See https://github.com/drizzle-team/drizzle-orm/issues/4044
|
||||
// TODO: type values (everything is a `text` for now)
|
||||
export function values(items: Record<string, unknown>[]) {
|
||||
if (items[0] === undefined) throw new Error("Invalid values, expecting at least one items")
|
||||
const [firstProp, ...props] = Object.keys(items[0]);
|
||||
const values = items
|
||||
.map((x) => {
|
||||
|
@ -122,8 +122,11 @@ export const Video = t.Intersect([
|
||||
]);
|
||||
export type Video = Prettify<typeof Video.static>;
|
||||
|
||||
// type used in entry responses
|
||||
export const EmbeddedVideo = t.Omit(Video, ["guess", "createdAt", "updatedAt"]);
|
||||
// type used in entry responses (the slug comes from the entryVideoJoin)
|
||||
export const EmbeddedVideo = t.Intersect([
|
||||
t.Object({ slug: t.String({ format: "slug" }) }),
|
||||
t.Omit(Video, ["guess", "createdAt", "updatedAt"]),
|
||||
]);
|
||||
export type EmbeddedVideo = Prettify<typeof EmbeddedVideo.static>;
|
||||
|
||||
registerExamples(Video, bubbleVideo);
|
||||
|
@ -39,7 +39,7 @@ describe("Video seeding", () => {
|
||||
|
||||
expect(vid).not.toBeNil();
|
||||
expect(vid!.path).toBe("/video/unknown s1e13.mkv");
|
||||
expect(vid!.guess).toBe({ title: "unknown", from: "test" });
|
||||
expect(vid!.guess).toMatchObject({ title: "unknown", from: "test" });
|
||||
|
||||
expect(body[0].entries).toBeArrayOfSize(0);
|
||||
expect(vid!.evj).toBeArrayOfSize(0);
|
||||
@ -68,7 +68,7 @@ describe("Video seeding", () => {
|
||||
|
||||
expect(vid).not.toBeNil();
|
||||
expect(vid!.path).toBe("/video/mia s1e13.mkv");
|
||||
expect(vid!.guess).toBe({ title: "mia", from: "test" });
|
||||
expect(vid!.guess).toMatchObject({ title: "mia", from: "test" });
|
||||
|
||||
expect(body[0].entries).toBeArrayOfSize(1);
|
||||
expect(vid!.evj).toBeArrayOfSize(1);
|
||||
@ -106,7 +106,7 @@ describe("Video seeding", () => {
|
||||
|
||||
expect(vid).not.toBeNil();
|
||||
expect(vid!.path).toBe("/video/mia s1e13.mkv");
|
||||
expect(vid!.guess).toBe({ title: "mia", from: "test" });
|
||||
expect(vid!.guess).toMatchObject({ title: "mia", from: "test" });
|
||||
|
||||
expect(body[0].entries).toBeArrayOfSize(1);
|
||||
expect(vid!.evj).toBeArrayOfSize(1);
|
||||
|
Loading…
x
Reference in New Issue
Block a user