diff --git a/api/src/controllers/entries.ts b/api/src/controllers/entries.ts deleted file mode 100644 index 45941576..00000000 --- a/api/src/controllers/entries.ts +++ /dev/null @@ -1,4 +0,0 @@ -import { Elysia } from "elysia"; - -export const EntriesController = new Elysia() - .get('/entries', () => "hello"); diff --git a/api/src/controllers/movies.ts b/api/src/controllers/movies.ts new file mode 100644 index 00000000..5c3649c5 --- /dev/null +++ b/api/src/controllers/movies.ts @@ -0,0 +1,58 @@ +import { Elysia, t } from "elysia"; +import { Movie } from "../models/movie"; +import { db } from "../db"; +import { shows, showTranslations } from "../db/schema/shows"; +import { eq, and, sql, or, inArray } from "drizzle-orm"; + +const findMovie = db + .select() + .from(shows) + .innerJoin( + db + .selectDistinctOn([showTranslations.language]) + .from(showTranslations) + .where( + or( + inArray(showTranslations.language, sql.placeholder("langs")), + eq(showTranslations.language, shows.originalLanguage), + ), + ) + .orderBy( + sql`array_position(${showTranslations.language}, ${sql.placeholder("langs")})`, + ) + .as("t"), + eq(shows.pk, showTranslations.pk), + ) + .where( + and( + eq(shows.kind, "movie"), + or( + eq(shows.id, sql.placeholder("id")), + eq(shows.slug, sql.placeholder("id")), + ), + ), + ) + .orderBy() + .limit(1) + .prepare("findMovie"); + +export const movies = new Elysia({ prefix: "/movies" }) + .model({ + movie: Movie, + error: t.Object({}), + }) + .guard({ + params: t.Object({ + id: t.String(), + }), + response: { 200: "movie", 404: "error" }, + }) + .get("/:id", async ({ params: { id }, error }) => { + const ret = await findMovie.execute({ id }); + if (ret.length !== 1) return error(404, {}); + return { + ...ret[0].shows, + ...ret[0].t, + airDate: ret[0].shows.startAir, + }; + }); diff --git a/api/src/db/index.ts b/api/src/db/index.ts index e336c6ad..7a0ab7f1 100644 --- a/api/src/db/index.ts +++ b/api/src/db/index.ts @@ -1,6 +1,12 @@ +import * as entries from "./schema/entries"; +import * as shows from "./schema/shows"; import { drizzle } from "drizzle-orm/node-postgres"; export const db = drizzle({ + schema: { + ...entries, + ...shows, + }, connection: { user: process.env.POSTGRES_USER ?? "kyoo", password: process.env.POSTGRES_PASSWORD ?? "password", diff --git a/api/src/index.ts b/api/src/index.ts index 240bb828..bac98e54 100644 --- a/api/src/index.ts +++ b/api/src/index.ts @@ -2,12 +2,14 @@ import { Elysia } from "elysia"; import { swagger } from "@elysiajs/swagger"; import { db } from "./db"; import { migrate } from "drizzle-orm/node-postgres/migrator"; +import { movies } from "./controllers/movies"; await migrate(db, { migrationsFolder: "" }); const app = new Elysia() .use(swagger()) .get("/", () => "Hello Elysia") + .use(movies) .listen(3000); console.log(`Api running at ${app.server?.hostname}:${app.server?.port}`);