Add with=firstEntry

This commit is contained in:
Zoe Roux 2025-03-10 19:32:09 +01:00
parent 458eb2c387
commit 5ae04c6dac
No known key found for this signature in database
4 changed files with 65 additions and 3 deletions

View File

@ -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 {
entries,
entryTranslations,
entryVideoJoin,
showStudioJoin,
showTranslations,
@ -18,6 +19,7 @@ import {
jsonbObjectAgg,
sqlarr,
} from "~/db/utils";
import type { Entry } from "~/models/entry";
import type { MovieStatus } from "~/models/movie";
import { SerieStatus, type SerieTranslation } from "~/models/serie";
import type { Studio } from "~/models/studio";
@ -142,6 +144,52 @@ const showRelations = {
.leftJoin(videos, eq(videos.pk, entryVideoJoin.videoPk))
.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({

View File

@ -71,7 +71,7 @@ export const series = new Elysia({ prefix: "/series", tags: ["series"] })
preferOriginal: t.Optional(
t.Boolean({ description: desc.preferOriginal }),
),
with: t.Array(t.UnionEnum(["translations", "studios"]), {
with: t.Array(t.UnionEnum(["translations", "studios", "firstEntry"]), {
default: [],
description: "Include related resources in the response.",
}),

View File

@ -1,7 +1,7 @@
import { t } from "elysia";
import type { Prettify } from "~/utils";
import { SeedCollection } from "./collections";
import { SeedEntry, SeedExtra } from "./entry";
import { Entry, SeedEntry, SeedExtra } from "./entry";
import { bubbleImages, madeInAbyss, registerExamples } from "./examples";
import { SeedSeason } from "./season";
import { SeedStaff } from "./staff";
@ -79,6 +79,7 @@ export const FullSerie = t.Intersect([
t.Object({
translations: t.Optional(TranslationRecord(SerieTranslation)),
studios: t.Optional(t.Array(Studio)),
firstEntry: t.Optional(Entry),
}),
]);
export type FullMovie = Prettify<typeof FullSerie.static>;

View File

@ -29,4 +29,17 @@ describe("Get series", () => {
expect(body.entriesCount).toBe(madeInAbyss.entries.length);
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);
});
});