mirror of
				https://github.com/zoriya/Kyoo.git
				synced 2025-11-04 03:27:14 -05:00 
			
		
		
		
	Add with=firstEntry
				
					
				
			This commit is contained in:
		
							parent
							
								
									458eb2c387
								
							
						
					
					
						commit
						5ae04c6dac
					
				@ -1,7 +1,8 @@
 | 
				
			|||||||
import { type SQL, and, eq, exists, sql } from "drizzle-orm";
 | 
					import { type SQL, and, eq, exists, ne, sql } from "drizzle-orm";
 | 
				
			||||||
import { db } from "~/db";
 | 
					import { db } from "~/db";
 | 
				
			||||||
import {
 | 
					import {
 | 
				
			||||||
	entries,
 | 
						entries,
 | 
				
			||||||
 | 
						entryTranslations,
 | 
				
			||||||
	entryVideoJoin,
 | 
						entryVideoJoin,
 | 
				
			||||||
	showStudioJoin,
 | 
						showStudioJoin,
 | 
				
			||||||
	showTranslations,
 | 
						showTranslations,
 | 
				
			||||||
@ -18,6 +19,7 @@ import {
 | 
				
			|||||||
	jsonbObjectAgg,
 | 
						jsonbObjectAgg,
 | 
				
			||||||
	sqlarr,
 | 
						sqlarr,
 | 
				
			||||||
} from "~/db/utils";
 | 
					} from "~/db/utils";
 | 
				
			||||||
 | 
					import type { Entry } from "~/models/entry";
 | 
				
			||||||
import type { MovieStatus } from "~/models/movie";
 | 
					import type { MovieStatus } from "~/models/movie";
 | 
				
			||||||
import { SerieStatus, type SerieTranslation } from "~/models/serie";
 | 
					import { SerieStatus, type SerieTranslation } from "~/models/serie";
 | 
				
			||||||
import type { Studio } from "~/models/studio";
 | 
					import type { Studio } from "~/models/studio";
 | 
				
			||||||
@ -142,6 +144,52 @@ const showRelations = {
 | 
				
			|||||||
			.leftJoin(videos, eq(videos.pk, entryVideoJoin.videoPk))
 | 
								.leftJoin(videos, eq(videos.pk, entryVideoJoin.videoPk))
 | 
				
			||||||
			.as("videos");
 | 
								.as("videos");
 | 
				
			||||||
	},
 | 
						},
 | 
				
			||||||
 | 
						firstEntry: ({ languages }: { languages: string[] }) => {
 | 
				
			||||||
 | 
							const transQ = db
 | 
				
			||||||
 | 
								.selectDistinctOn([entryTranslations.pk])
 | 
				
			||||||
 | 
								.from(entryTranslations)
 | 
				
			||||||
 | 
								.orderBy(
 | 
				
			||||||
 | 
									entryTranslations.pk,
 | 
				
			||||||
 | 
									sql`array_position(${sqlarr(languages)}, ${entryTranslations.language})`,
 | 
				
			||||||
 | 
								)
 | 
				
			||||||
 | 
								.as("t");
 | 
				
			||||||
 | 
							const { pk, ...transCol } = getColumns(transQ);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							const { guess, createdAt, updatedAt, ...videosCol } = getColumns(videos);
 | 
				
			||||||
 | 
							const videosQ = db
 | 
				
			||||||
 | 
								.select({
 | 
				
			||||||
 | 
									videos: coalesce(
 | 
				
			||||||
 | 
										jsonbAgg(
 | 
				
			||||||
 | 
											jsonbBuildObject<EmbeddedVideo>({
 | 
				
			||||||
 | 
												slug: entryVideoJoin.slug,
 | 
				
			||||||
 | 
												...videosCol,
 | 
				
			||||||
 | 
											}),
 | 
				
			||||||
 | 
										),
 | 
				
			||||||
 | 
										sql`'[]'::jsonb`,
 | 
				
			||||||
 | 
									).as("videos"),
 | 
				
			||||||
 | 
								})
 | 
				
			||||||
 | 
								.from(entryVideoJoin)
 | 
				
			||||||
 | 
								.where(eq(entryVideoJoin.entryPk, entries.pk))
 | 
				
			||||||
 | 
								.leftJoin(videos, eq(videos.pk, entryVideoJoin.videoPk))
 | 
				
			||||||
 | 
								.as("videos");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							return db
 | 
				
			||||||
 | 
								.select({
 | 
				
			||||||
 | 
									firstEntry: jsonbBuildObject<Entry>({
 | 
				
			||||||
 | 
										...getColumns(entries),
 | 
				
			||||||
 | 
										...transCol,
 | 
				
			||||||
 | 
										number: entries.episodeNumber,
 | 
				
			||||||
 | 
										videos: videosQ.videos,
 | 
				
			||||||
 | 
									}).as("firstEntry"),
 | 
				
			||||||
 | 
								})
 | 
				
			||||||
 | 
								.from(entries)
 | 
				
			||||||
 | 
								.innerJoin(transQ, eq(entries.pk, transQ.pk))
 | 
				
			||||||
 | 
								.leftJoinLateral(videosQ, sql`true`)
 | 
				
			||||||
 | 
								.where(and(eq(entries.showPk, shows.pk), ne(entries.kind, "extra")))
 | 
				
			||||||
 | 
								.orderBy(entries.order)
 | 
				
			||||||
 | 
								.limit(1)
 | 
				
			||||||
 | 
								.as("firstEntry");
 | 
				
			||||||
 | 
						},
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export async function getShows({
 | 
					export async function getShows({
 | 
				
			||||||
 | 
				
			|||||||
@ -71,7 +71,7 @@ export const series = new Elysia({ prefix: "/series", tags: ["series"] })
 | 
				
			|||||||
				preferOriginal: t.Optional(
 | 
									preferOriginal: t.Optional(
 | 
				
			||||||
					t.Boolean({ description: desc.preferOriginal }),
 | 
										t.Boolean({ description: desc.preferOriginal }),
 | 
				
			||||||
				),
 | 
									),
 | 
				
			||||||
				with: t.Array(t.UnionEnum(["translations", "studios"]), {
 | 
									with: t.Array(t.UnionEnum(["translations", "studios", "firstEntry"]), {
 | 
				
			||||||
					default: [],
 | 
										default: [],
 | 
				
			||||||
					description: "Include related resources in the response.",
 | 
										description: "Include related resources in the response.",
 | 
				
			||||||
				}),
 | 
									}),
 | 
				
			||||||
 | 
				
			|||||||
@ -1,7 +1,7 @@
 | 
				
			|||||||
import { t } from "elysia";
 | 
					import { t } from "elysia";
 | 
				
			||||||
import type { Prettify } from "~/utils";
 | 
					import type { Prettify } from "~/utils";
 | 
				
			||||||
import { SeedCollection } from "./collections";
 | 
					import { SeedCollection } from "./collections";
 | 
				
			||||||
import { SeedEntry, SeedExtra } from "./entry";
 | 
					import { Entry, SeedEntry, SeedExtra } from "./entry";
 | 
				
			||||||
import { bubbleImages, madeInAbyss, registerExamples } from "./examples";
 | 
					import { bubbleImages, madeInAbyss, registerExamples } from "./examples";
 | 
				
			||||||
import { SeedSeason } from "./season";
 | 
					import { SeedSeason } from "./season";
 | 
				
			||||||
import { SeedStaff } from "./staff";
 | 
					import { SeedStaff } from "./staff";
 | 
				
			||||||
@ -79,6 +79,7 @@ export const FullSerie = t.Intersect([
 | 
				
			|||||||
	t.Object({
 | 
						t.Object({
 | 
				
			||||||
		translations: t.Optional(TranslationRecord(SerieTranslation)),
 | 
							translations: t.Optional(TranslationRecord(SerieTranslation)),
 | 
				
			||||||
		studios: t.Optional(t.Array(Studio)),
 | 
							studios: t.Optional(t.Array(Studio)),
 | 
				
			||||||
 | 
							firstEntry: t.Optional(Entry),
 | 
				
			||||||
	}),
 | 
						}),
 | 
				
			||||||
]);
 | 
					]);
 | 
				
			||||||
export type FullMovie = Prettify<typeof FullSerie.static>;
 | 
					export type FullMovie = Prettify<typeof FullSerie.static>;
 | 
				
			||||||
 | 
				
			|||||||
@ -29,4 +29,17 @@ describe("Get series", () => {
 | 
				
			|||||||
		expect(body.entriesCount).toBe(madeInAbyss.entries.length);
 | 
							expect(body.entriesCount).toBe(madeInAbyss.entries.length);
 | 
				
			||||||
		expect(body.availableCount).toBe(1);
 | 
							expect(body.availableCount).toBe(1);
 | 
				
			||||||
	});
 | 
						});
 | 
				
			||||||
 | 
						it("With firstEntry", async () => {
 | 
				
			||||||
 | 
							const [resp, body] = await getSerie(madeInAbyss.slug, {
 | 
				
			||||||
 | 
								langs: "en",
 | 
				
			||||||
 | 
								with: ["firstEntry"],
 | 
				
			||||||
 | 
							});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							expectStatus(resp, body).toBe(200);
 | 
				
			||||||
 | 
							expect(body.firstEntry.slug).toBe("made-in-abyss-s1e13");
 | 
				
			||||||
 | 
							expect(body.firstEntry.name).toBe(
 | 
				
			||||||
 | 
								madeInAbyss.entries[0].translations.en.name,
 | 
				
			||||||
 | 
							);
 | 
				
			||||||
 | 
							expect(body.firstEntry.videos).toBeArrayOfSize(1);
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user