diff --git a/api/src/controllers/videos.ts b/api/src/controllers/videos.ts index 4761e5d1..a51766cd 100644 --- a/api/src/controllers/videos.ts +++ b/api/src/controllers/videos.ts @@ -21,12 +21,13 @@ export const videos = new Elysia({ prefix: "/videos", tags: ["videos"] }) }) .post( "", - async ({ body }) => { - return await db + async ({ body, error }) => { + const ret = await db .insert(videosT) .values(body) .onConflictDoNothing() .returning({ id: videosT.id, path: videosT.path }); + return error(201, ret); }, { body: t.Array(SeedVideo), diff --git a/api/src/models/video.ts b/api/src/models/video.ts index 0583a14a..2987557f 100644 --- a/api/src/models/video.ts +++ b/api/src/models/video.ts @@ -36,3 +36,4 @@ export type Video = typeof Video.static; registerExamples(Video, bubbleVideo); export const SeedVideo = t.Omit(Video, ["id", "slug", "createdAt"]); +export type SeedVideo = typeof SeedVideo.static; diff --git a/api/tests/movies/movies-helper.ts b/api/tests/helper.ts similarity index 64% rename from api/tests/movies/movies-helper.ts rename to api/tests/helper.ts index c5ca4830..089570ac 100644 --- a/api/tests/movies/movies-helper.ts +++ b/api/tests/helper.ts @@ -3,15 +3,23 @@ import { buildUrl } from "tests/utils"; import { base } from "~/base"; import { movies } from "~/controllers/movies"; import { seed } from "~/controllers/seed"; +import { series } from "~/controllers/series"; +import { videos } from "~/controllers/videos"; import type { SeedMovie } from "~/models/movie"; +import type { SeedVideo } from "~/models/video"; -export const movieApp = new Elysia().use(base).use(movies).use(seed); +export const app = new Elysia() + .use(base) + .use(movies) + .use(series) + .use(videos) + .use(seed); export const getMovie = async ( id: string, { langs, ...query }: { langs?: string; preferOriginal?: boolean }, ) => { - const resp = await movieApp.handle( + const resp = await app.handle( new Request(buildUrl(`movies/${id}`, query), { method: "GET", headers: langs @@ -37,7 +45,7 @@ export const getMovies = async ({ langs?: string; preferOriginal?: boolean; }) => { - const resp = await movieApp.handle( + const resp = await app.handle( new Request(buildUrl("movies", query), { method: "GET", headers: langs @@ -52,7 +60,7 @@ export const getMovies = async ({ }; export const createMovie = async (movie: SeedMovie) => { - const resp = await movieApp.handle( + const resp = await app.handle( new Request(buildUrl("movies"), { method: "POST", body: JSON.stringify(movie), @@ -64,3 +72,17 @@ export const createMovie = async (movie: SeedMovie) => { const body = await resp.json(); return [resp, body] as const; }; + +export const createVideo = async (video: SeedVideo | SeedVideo[]) => { + const resp = await app.handle( + new Request(buildUrl("videos"), { + method: "POST", + body: JSON.stringify(Array.isArray(video) ? video : [video]), + headers: { + "Content-Type": "application/json", + }, + }), + ); + const body = await resp.json(); + return [resp, body] as const; +}; diff --git a/api/tests/movies/get-all-movies-with-null.test.ts b/api/tests/movies/get-all-movies-with-null.test.ts index 8f4ca9f7..1abeed9c 100644 --- a/api/tests/movies/get-all-movies-with-null.test.ts +++ b/api/tests/movies/get-all-movies-with-null.test.ts @@ -7,7 +7,7 @@ import { shows } from "~/db/schema"; import { bubble } from "~/models/examples"; import { dune1984 } from "~/models/examples/dune-1984"; import { dune } from "~/models/examples/dune-2021"; -import { createMovie, getMovies, movieApp } from "./movies-helper"; +import { createMovie, getMovies, app } from "../helper"; beforeAll(async () => { await db.delete(shows); @@ -77,7 +77,7 @@ describe("with a null value", () => { ), }); - resp = await movieApp.handle(new Request(next)); + resp = await app.handle(new Request(next)); body = await resp.json(); expectStatus(resp, body).toBe(200); @@ -124,7 +124,7 @@ describe("with a null value", () => { ), }); - resp = await movieApp.handle(new Request(next)); + resp = await app.handle(new Request(next)); body = await resp.json(); expectStatus(resp, body).toBe(200); diff --git a/api/tests/movies/get-all-movies.test.ts b/api/tests/movies/get-all-movies.test.ts index 0694aeed..1a875dc6 100644 --- a/api/tests/movies/get-all-movies.test.ts +++ b/api/tests/movies/get-all-movies.test.ts @@ -8,7 +8,7 @@ import { dune1984 } from "~/models/examples/dune-1984"; import { dune } from "~/models/examples/dune-2021"; import type { Movie } from "~/models/movie"; import { isUuid } from "~/models/utils"; -import { getMovies, movieApp } from "./movies-helper"; +import { getMovies, app } from "../helper"; beforeAll(async () => { await db.delete(shows); @@ -73,7 +73,7 @@ describe("Get all movies", () => { }); expectStatus(resp, body).toBe(200); - resp = await movieApp.handle(new Request(body.next)); + resp = await app.handle(new Request(body.next)); body = await resp.json(); expectStatus(resp, body).toBe(200); @@ -106,7 +106,7 @@ describe("Get all movies", () => { ), }); - resp = await movieApp.handle(new Request(next)); + resp = await app.handle(new Request(next)); body = await resp.json(); expectStatus(resp, body).toBe(200); @@ -162,7 +162,7 @@ describe("Get all movies", () => { expect(items.length).toBe(1); expect(items[0].id).toBe(expectedIds[0]); // Get Second Page - resp = await movieApp.handle(new Request(body.next)); + resp = await app.handle(new Request(body.next)); body = await resp.json(); expectStatus(resp, body).toBe(200); @@ -177,7 +177,7 @@ describe("Get all movies", () => { }); expectStatus(resp, body).toBe(200); - const resp2 = await movieApp.handle(new Request(body.next)); + const resp2 = await app.handle(new Request(body.next)); const body2 = await resp2.json(); expectStatus(resp2, body).toBe(200); @@ -188,7 +188,7 @@ describe("Get all movies", () => { }); it("Get /random", async () => { - const resp = await movieApp.handle( + const resp = await app.handle( new Request("http://localhost/movies/random"), ); expect(resp.status).toBe(302); diff --git a/api/tests/movies/get-movie.test.ts b/api/tests/movies/get-movie.test.ts index 607f8cec..eddf6664 100644 --- a/api/tests/movies/get-movie.test.ts +++ b/api/tests/movies/get-movie.test.ts @@ -2,7 +2,7 @@ import { beforeAll, describe, expect, it } from "bun:test"; import { expectStatus } from "tests/utils"; import { seedMovie } from "~/controllers/seed/movies"; import { bubble } from "~/models/examples"; -import { getMovie } from "./movies-helper"; +import { getMovie } from "../helper"; let bubbleId = "";