From 5483e34c5e9a37f7e13c27d07d5b610d0c6bb06f Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Sat, 1 Mar 2025 19:48:17 +0100 Subject: [PATCH] Fix season get --- api/src/controllers/seasons.ts | 41 +++++++++++++++++----------- api/tests/movies/seed-movies.test.ts | 12 ++++---- api/tests/series/seed-serie.test.ts | 8 +++++- 3 files changed, 37 insertions(+), 24 deletions(-) diff --git a/api/src/controllers/seasons.ts b/api/src/controllers/seasons.ts index d74e54cd..f578bb44 100644 --- a/api/src/controllers/seasons.ts +++ b/api/src/controllers/seasons.ts @@ -38,21 +38,27 @@ export const seasonsH = new Elysia({ tags: ["series"] }) query: { limit, after, query, sort, filter }, headers: { "accept-language": languages }, request: { url }, + error, }) => { const langs = processLanguages(languages); - const show = db.$with("serie").as( - db - .select({ pk: shows.pk }) - .from(shows) - .where( - and( - eq(shows.kind, "serie"), - isUuid(id) ? eq(shows.id, id) : eq(shows.slug, id), - ), - ) - .limit(1), - ); + const [serie] = await db + .select({ pk: shows.pk }) + .from(shows) + .where( + and( + eq(shows.kind, "serie"), + isUuid(id) ? eq(shows.id, id) : eq(shows.slug, id), + ), + ) + .limit(1); + + if (!serie) { + return error(404, { + status: 404, + message: `No serie with the id or slug: '${id}'.`, + }); + } const transQ = db .selectDistinctOn([seasonTranslations.pk]) @@ -65,7 +71,6 @@ export const seasonsH = new Elysia({ tags: ["series"] }) const { pk, ...transCol } = getColumns(transQ); const items = await db - .with(show) .select({ ...getColumns(seasons), ...transCol, @@ -74,7 +79,7 @@ export const seasonsH = new Elysia({ tags: ["series"] }) .innerJoin(transQ, eq(seasons.pk, transQ.pk)) .where( and( - eq(seasons.showPk, show.pk), + eq(seasons.showPk, serie.pk), filter, query ? sql`${transQ.name} %> ${query}::text` : undefined, keysetPaginate({ table: seasons, after, sort }), @@ -92,10 +97,10 @@ export const seasonsH = new Elysia({ tags: ["series"] }) }, { detail: { description: "Get seasons of a serie" }, - path: t.Object({ + params: t.Object({ id: t.String({ description: "The id or slug of the serie.", - examples: [madeInAbyss.slug], + example: madeInAbyss.slug, }), }), query: t.Object({ @@ -117,6 +122,10 @@ export const seasonsH = new Elysia({ tags: ["series"] }) }), response: { 200: Page(Season), + 404: { + ...KError, + description: "No serie found with the given id or slug.", + }, 422: KError, }, }, diff --git a/api/tests/movies/seed-movies.test.ts b/api/tests/movies/seed-movies.test.ts index 34e721de..bbc8b4d9 100644 --- a/api/tests/movies/seed-movies.test.ts +++ b/api/tests/movies/seed-movies.test.ts @@ -7,6 +7,11 @@ import { bubble } from "~/models/examples"; import { dune, duneVideo } from "~/models/examples/dune-2021"; import { createMovie, createVideo } from "../helpers"; +beforeAll(async () => { + await db.delete(shows); + await db.delete(videos); +}); + describe("Movie seeding", () => { it("Can create a movie", async () => { // create video beforehand to test linking @@ -404,10 +409,3 @@ describe("Movie seeding", () => { ]); }); }); - -const cleanup = async () => { - await db.delete(shows); - await db.delete(videos); -}; -// cleanup db beforehand to unsure tests are consistent -beforeAll(cleanup); diff --git a/api/tests/series/seed-serie.test.ts b/api/tests/series/seed-serie.test.ts index d3a0587e..2d5baf56 100644 --- a/api/tests/series/seed-serie.test.ts +++ b/api/tests/series/seed-serie.test.ts @@ -1,4 +1,4 @@ -import { describe, expect, it } from "bun:test"; +import { beforeAll, describe, expect, it } from "bun:test"; import { eq } from "drizzle-orm"; import { expectStatus } from "tests/utils"; import { db } from "~/db"; @@ -6,6 +6,11 @@ import { seasons, shows, videos } from "~/db/schema"; import { madeInAbyss, madeInAbyssVideo } from "~/models/examples"; import { createSerie } from "../helpers"; +beforeAll(async () => { + await db.delete(shows); + await db.delete(videos); +}); + describe("Serie seeding", () => { it("Can create a serie with seasons and episodes", async () => { // create video beforehand to test linking @@ -69,6 +74,7 @@ describe("Serie seeding", () => { { language: "en", ...movie.translations.en, + poster: { source: movie.translations.en.poster }, }, ], });