diff --git a/api/src/controllers/entries.ts b/api/src/controllers/entries.ts index c59fa656..74f54ce2 100644 --- a/api/src/controllers/entries.ts +++ b/api/src/controllers/entries.ts @@ -141,6 +141,17 @@ export const entryVideosQ = db .leftJoin(videos, eq(videos.pk, entryVideoJoin.videoPk)) .as("videos"); +export const getEntryTransQ = (languages: string[]) => { + return db + .selectDistinctOn([entryTranslations.pk]) + .from(entryTranslations) + .orderBy( + entryTranslations.pk, + sql`array_position(${sqlarr(languages)}, ${entryTranslations.language})`, + ) + .as("entry_t"); +}; + export const mapProgress = ({ aliased }: { aliased: boolean }) => { const { time, percent, playedDate, videoId } = getColumns(entryProgressQ); const ret = { @@ -174,15 +185,7 @@ export async function getEntries({ userId: string; progressQ?: typeof entryProgressQ; }): Promise<(Entry | Extra)[]> { - const transQ = db - .selectDistinctOn([entryTranslations.pk]) - .from(entryTranslations) - .orderBy( - entryTranslations.pk, - sql`array_position(${sqlarr(languages)}, ${entryTranslations.language})`, - ) - .as("t"); - const { pk, name, ...transCol } = getColumns(transQ); + const transQ = getEntryTransQ(languages); const { kind, @@ -196,7 +199,7 @@ export async function getEntries({ return await db .select({ ...entryCol, - ...transCol, + ...getColumns(transQ), videos: entryVideosQ.videos, progress: mapProgress({ aliased: true }), // specials don't have an `episodeNumber` but a `number` field. @@ -212,7 +215,7 @@ export async function getEntries({ order: sql`${order}`, seasonNumber: sql`${seasonNumber}`, episodeNumber: sql`${episodeNumber}`, - name: sql`${name}`, + name: sql`${transQ.name}`, }) .from(entries) .innerJoin(transQ, eq(entries.pk, transQ.pk)) diff --git a/api/src/controllers/profiles/nextup.ts b/api/src/controllers/profiles/nextup.ts index 78cd6433..bbf64686 100644 --- a/api/src/controllers/profiles/nextup.ts +++ b/api/src/controllers/profiles/nextup.ts @@ -22,6 +22,7 @@ import { entryFilters, entryProgressQ, entryVideosQ, + getEntryTransQ, mapProgress, } from "../entries"; @@ -73,16 +74,7 @@ export const nextup = new Elysia({ tags: ["profiles"] }) jwt: { sub }, }) => { const langs = processLanguages(languages); - - const transQ = db - .selectDistinctOn([entryTranslations.pk]) - .from(entryTranslations) - .orderBy( - entryTranslations.pk, - sql`array_position(${sqlarr(langs)}, ${entryTranslations.language})`, - ) - .as("t"); - const { pk, name, ...transCol } = getColumns(transQ); + const transQ = getEntryTransQ(langs); const { externalId, @@ -97,7 +89,7 @@ export const nextup = new Elysia({ tags: ["profiles"] }) const items = await db .select({ ...entryCol, - ...transCol, + ...getColumns(transQ), videos: entryVideosQ.videos, progress: mapProgress({ aliased: true }), // specials don't have an `episodeNumber` but a `number` field. diff --git a/api/src/controllers/shows/logic.ts b/api/src/controllers/shows/logic.ts index 4a789e0f..e2a498bb 100644 --- a/api/src/controllers/shows/logic.ts +++ b/api/src/controllers/shows/logic.ts @@ -36,7 +36,7 @@ import { } from "~/models/utils"; import type { EmbeddedVideo } from "~/models/video"; import { WatchlistStatus } from "~/models/watchlist"; -import { entryProgressQ, entryVideosQ, mapProgress } from "../entries"; +import { entryProgressQ, entryVideosQ, getEntryTransQ, mapProgress } from "../entries"; export const watchStatusQ = db .select({ @@ -147,7 +147,7 @@ const showRelations = { ).as("json"), }) .from(studios) - .leftJoin(studioTransQ, eq(studios.pk, studioTransQ.pk)) + .innerJoin(studioTransQ, eq(studios.pk, studioTransQ.pk)) .where( exists( db @@ -185,21 +185,13 @@ const showRelations = { .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 transQ = getEntryTransQ(languages); return db .select({ firstEntry: jsonbBuildObject({ ...getColumns(entries), - ...transCol, + ...getColumns(transQ), number: entries.episodeNumber, videos: entryVideosQ.videos, progress: mapProgress({ aliased: false }), @@ -217,21 +209,13 @@ const showRelations = { .as("firstEntry"); }, nextEntry: ({ 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 transQ = getEntryTransQ(languages); return db .select({ nextEntry: jsonbBuildObject({ ...getColumns(entries), - ...transCol, + ...getColumns(transQ), number: entries.episodeNumber, videos: entryVideosQ.videos, progress: mapProgress({ aliased: false }),