Fix season get

This commit is contained in:
Zoe Roux 2025-03-01 19:48:17 +01:00
parent b5460682c9
commit 5483e34c5e
3 changed files with 37 additions and 24 deletions

View File

@ -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,
},
},

View File

@ -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);

View File

@ -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 },
},
],
});