mirror of
https://github.com/zoriya/Kyoo.git
synced 2026-06-05 05:45:16 -04:00
Fix existing get movie & add test
This commit is contained in:
@@ -2,21 +2,24 @@ import { Elysia, t } from "elysia";
|
||||
import { Movie, MovieTranslation } from "../models/movie";
|
||||
import { db } from "../db";
|
||||
import { shows, showTranslations } from "../db/schema/shows";
|
||||
import { eq, and, sql, or, inArray } from "drizzle-orm";
|
||||
import { eq, and, sql, or } from "drizzle-orm";
|
||||
import { getColumns } from "../db/schema/utils";
|
||||
import { bubble } from "../models/examples";
|
||||
import { comment } from "~/utils";
|
||||
import { processLanguages } from "~/models/utils";
|
||||
|
||||
const translations = db
|
||||
.selectDistinctOn([showTranslations.language])
|
||||
.selectDistinctOn([showTranslations.pk])
|
||||
.from(showTranslations)
|
||||
.where(
|
||||
or(
|
||||
inArray(showTranslations.language, sql.placeholder("langs")),
|
||||
eq(showTranslations.language, shows.originalLanguage),
|
||||
),
|
||||
)
|
||||
// .where(
|
||||
// or(
|
||||
// eq(showTranslations.language, sql`any(${sql.placeholder("langs")})`),
|
||||
// eq(showTranslations.language, shows.originalLanguage),
|
||||
// ),
|
||||
// )
|
||||
.orderBy(
|
||||
sql`array_position(${showTranslations.language}, ${sql.placeholder("langs")})`,
|
||||
showTranslations.pk,
|
||||
sql`array_position(${sql.placeholder("langs")}, ${showTranslations.language})`,
|
||||
)
|
||||
.as("t");
|
||||
|
||||
@@ -26,21 +29,21 @@ const { pk, language, ...translationsCol } = getColumns(translations);
|
||||
const findMovie = db
|
||||
.select({
|
||||
...moviesCol,
|
||||
...translationsCol,
|
||||
airDate: startAir,
|
||||
translations: translationsCol,
|
||||
})
|
||||
.from(shows)
|
||||
.innerJoin(translations, eq(shows.pk, translations.pk))
|
||||
.where(
|
||||
and(
|
||||
eq(shows.kind, "movie"),
|
||||
or(
|
||||
eq(shows.id, sql.placeholder("id")),
|
||||
// or(
|
||||
// eq(shows.id, sql.placeholder("id")),
|
||||
eq(shows.slug, sql.placeholder("id")),
|
||||
),
|
||||
// ),
|
||||
),
|
||||
)
|
||||
.orderBy()
|
||||
// .orderBy()
|
||||
.limit(1)
|
||||
.prepare("findMovie");
|
||||
|
||||
@@ -57,12 +60,31 @@ export const movies = new Elysia({ prefix: "/movies", tags: ["movies"] })
|
||||
examples: [bubble.slug],
|
||||
}),
|
||||
}),
|
||||
headers: t.Object({
|
||||
"Accept-Language": t.String({
|
||||
default: "*",
|
||||
examples: "en-us, ja;q=0.5",
|
||||
description: comment`
|
||||
List of languages you want the data in.
|
||||
This follows the Accept-Language offical specification
|
||||
(https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Language)
|
||||
`,
|
||||
}),
|
||||
}),
|
||||
response: { 200: "movie", 404: "error" },
|
||||
})
|
||||
.get(
|
||||
"/:id",
|
||||
async ({ params: { id }, error }) => {
|
||||
const ret = await findMovie.execute({ id });
|
||||
async ({
|
||||
params: { id },
|
||||
headers: { "Accept-Language": languages },
|
||||
error,
|
||||
}) => {
|
||||
const langs = processLanguages(languages);
|
||||
console.log(langs);
|
||||
console.log(findMovie.getQuery());
|
||||
const ret = await findMovie.execute({ id, langs });
|
||||
console.log(ret);
|
||||
if (ret.length !== 1) return error(404, {});
|
||||
return ret[0];
|
||||
},
|
||||
@@ -71,4 +93,4 @@ export const movies = new Elysia({ prefix: "/movies", tags: ["movies"] })
|
||||
description: "Get a movie by id or slug",
|
||||
},
|
||||
},
|
||||
);
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user