mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-05-31 04:04:21 -04:00
Add seasons controller
This commit is contained in:
parent
a6ac5dfb75
commit
bb2630431a
@ -176,7 +176,7 @@ export const movies = new Elysia({ prefix: "/movies", tags: ["movies"] })
|
|||||||
params: t.Object({
|
params: t.Object({
|
||||||
id: t.String({
|
id: t.String({
|
||||||
description: "The id or slug of the movie to retrieve.",
|
description: "The id or slug of the movie to retrieve.",
|
||||||
example: bubble.slug,
|
examples: [bubble.slug],
|
||||||
}),
|
}),
|
||||||
}),
|
}),
|
||||||
query: t.Object({
|
query: t.Object({
|
||||||
|
@ -1,11 +1,123 @@
|
|||||||
import { Elysia, t } from "elysia";
|
import { Elysia, t } from "elysia";
|
||||||
import { Season } from "../models/season";
|
import { Season, SeasonTranslation } from "../models/season";
|
||||||
|
import {
|
||||||
|
AcceptLanguage,
|
||||||
|
createPage,
|
||||||
|
Filter,
|
||||||
|
FilterDef,
|
||||||
|
isUuid,
|
||||||
|
keysetPaginate,
|
||||||
|
Page,
|
||||||
|
processLanguages,
|
||||||
|
Sort,
|
||||||
|
sortToSql,
|
||||||
|
} from "~/models/utils";
|
||||||
|
import { KError } from "~/models/error";
|
||||||
|
import { and, eq, sql } from "drizzle-orm";
|
||||||
|
import { desc } from "~/models/utils/descriptions";
|
||||||
|
import { madeInAbyss } from "~/models/examples";
|
||||||
|
import { seasons, seasonTranslations, shows } from "~/db/schema";
|
||||||
|
import { db } from "~/db";
|
||||||
|
import { getColumns, sqlarr } from "~/db/utils";
|
||||||
|
|
||||||
export const seasons = new Elysia({ prefix: "/seasons" })
|
const seasonFilters: FilterDef = {
|
||||||
|
seasonNumber: { column: seasons.seasonNumber, type: "int" },
|
||||||
|
startAir: { column: seasons.startAir, type: "date" },
|
||||||
|
endAir: { column: seasons.endAir, type: "date" },
|
||||||
|
};
|
||||||
|
|
||||||
|
export const seasonsH = new Elysia({ tags: ["series"] })
|
||||||
.model({
|
.model({
|
||||||
season: Season,
|
season: Season,
|
||||||
error: t.Object({}),
|
"season-translation": SeasonTranslation,
|
||||||
})
|
})
|
||||||
.get("/:id", () => "hello" as unknown as Season, {
|
.get(
|
||||||
response: { 200: "season" },
|
"/series/:id/seasons",
|
||||||
});
|
async ({
|
||||||
|
params: { id },
|
||||||
|
query: { limit, after, query, sort, filter },
|
||||||
|
headers: { "accept-language": languages },
|
||||||
|
request: { url },
|
||||||
|
}) => {
|
||||||
|
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 transQ = db
|
||||||
|
.selectDistinctOn([seasonTranslations.pk])
|
||||||
|
.from(seasonTranslations)
|
||||||
|
.orderBy(
|
||||||
|
seasonTranslations.pk,
|
||||||
|
sql`array_position(${sqlarr(langs)}, ${seasonTranslations.language})`,
|
||||||
|
)
|
||||||
|
.as("t");
|
||||||
|
const { pk, ...transCol } = getColumns(transQ);
|
||||||
|
|
||||||
|
const items = await db
|
||||||
|
.with(show)
|
||||||
|
.select({
|
||||||
|
...getColumns(seasons),
|
||||||
|
...transCol,
|
||||||
|
})
|
||||||
|
.from(seasons)
|
||||||
|
.innerJoin(transQ, eq(seasons.pk, transQ.pk))
|
||||||
|
.where(
|
||||||
|
and(
|
||||||
|
eq(seasons.showPk, show.pk),
|
||||||
|
filter,
|
||||||
|
query ? sql`${transQ.name} %> ${query}::text` : undefined,
|
||||||
|
keysetPaginate({ table: seasons, after, sort }),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.orderBy(
|
||||||
|
...(query
|
||||||
|
? [sql`word_similarity(${query}::text, ${transQ.name})`]
|
||||||
|
: sortToSql(sort, seasons)),
|
||||||
|
seasons.pk,
|
||||||
|
)
|
||||||
|
.limit(limit);
|
||||||
|
|
||||||
|
return createPage(items, { url, sort, limit });
|
||||||
|
},
|
||||||
|
{
|
||||||
|
detail: { description: "Get seasons of a serie" },
|
||||||
|
path: t.Object({
|
||||||
|
id: t.String({
|
||||||
|
description: "The id or slug of the serie.",
|
||||||
|
examples: [madeInAbyss.slug],
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
query: t.Object({
|
||||||
|
sort: Sort(["seasonNumber", "startAir", "endAir", "nextRefresh"], {
|
||||||
|
default: ["seasonNumber"],
|
||||||
|
}),
|
||||||
|
filter: t.Optional(Filter({ def: seasonFilters })),
|
||||||
|
query: t.Optional(t.String({ description: desc.query })),
|
||||||
|
limit: t.Integer({
|
||||||
|
minimum: 1,
|
||||||
|
maximum: 250,
|
||||||
|
default: 50,
|
||||||
|
description: "Max page size.",
|
||||||
|
}),
|
||||||
|
after: t.Optional(t.String({ description: desc.after })),
|
||||||
|
}),
|
||||||
|
headers: t.Object({
|
||||||
|
"accept-language": AcceptLanguage({ autoFallback: true }),
|
||||||
|
}),
|
||||||
|
response: {
|
||||||
|
200: Page(Season),
|
||||||
|
422: KError,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
||||||
|
@ -4,7 +4,7 @@ import { Elysia } from "elysia";
|
|||||||
import { base } from "./base";
|
import { base } from "./base";
|
||||||
import { entries } from "./controllers/entries";
|
import { entries } from "./controllers/entries";
|
||||||
import { movies } from "./controllers/movies";
|
import { movies } from "./controllers/movies";
|
||||||
import { seasons } from "./controllers/seasons";
|
import { seasonsH } from "./controllers/seasons";
|
||||||
import { seed } from "./controllers/seed";
|
import { seed } from "./controllers/seed";
|
||||||
import { series } from "./controllers/series";
|
import { series } from "./controllers/series";
|
||||||
import { videos } from "./controllers/videos";
|
import { videos } from "./controllers/videos";
|
||||||
@ -58,6 +58,7 @@ const app = new Elysia()
|
|||||||
],
|
],
|
||||||
tags: [
|
tags: [
|
||||||
{ name: "movies", description: "Routes about movies" },
|
{ name: "movies", description: "Routes about movies" },
|
||||||
|
{ name: "series", description: "Routes about series" },
|
||||||
{
|
{
|
||||||
name: "videos",
|
name: "videos",
|
||||||
description: comment`
|
description: comment`
|
||||||
@ -73,7 +74,7 @@ const app = new Elysia()
|
|||||||
.use(movies)
|
.use(movies)
|
||||||
.use(series)
|
.use(series)
|
||||||
.use(entries)
|
.use(entries)
|
||||||
.use(seasons)
|
.use(seasonsH)
|
||||||
.use(videos)
|
.use(videos)
|
||||||
.use(seed)
|
.use(seed)
|
||||||
.listen(3000);
|
.listen(3000);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user