diff --git a/api/src/controllers/movies.ts b/api/src/controllers/movies.ts index de20518b..c6e3485f 100644 --- a/api/src/controllers/movies.ts +++ b/api/src/controllers/movies.ts @@ -155,18 +155,18 @@ export const movies = new Elysia({ prefix: "/movies", tags: ["movies"] }) .get( "random", async ({ error, redirect }) => { - const [id] = await db + const [movie] = await db .select({ id: shows.id }) .from(shows) .where(eq(shows.kind, "movie")) .orderBy(sql`random()`) .limit(1); - if (!id) + if (!movie) return error(404, { status: 404, message: "No movies in the database", }); - return redirect(`/movies/${id}`); + return redirect(`/movies/${movie.id}`); }, { detail: { @@ -175,7 +175,7 @@ export const movies = new Elysia({ prefix: "/movies", tags: ["movies"] }) response: { 302: t.Void({ description: - "Redirected to the [/movies/id](#tag/movies/GET/movies/{id}) route.", + "Redirected to the [/movies/{id}](#tag/movies/GET/movies/{id}) route.", }), 404: { ...KError, diff --git a/api/tests/movies/get-all-movies.test.ts b/api/tests/movies/get-all-movies.test.ts index e91c7be8..d7f28a4e 100644 --- a/api/tests/movies/get-all-movies.test.ts +++ b/api/tests/movies/get-all-movies.test.ts @@ -6,8 +6,9 @@ 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 { getMovies, movieApp } from "./movies-helper"; +import { getMovie, getMovies, movieApp } from "./movies-helper"; import type { Movie } from "~/models/movie"; +import { isUuid } from "~/models/utils"; beforeAll(async () => { await db.delete(shows); @@ -186,5 +187,16 @@ describe("Get all movies", () => { body2.items[0].slug, ); }); + + it("Get /random", async () => { + const resp = await movieApp.handle( + new Request("http://localhost/movies/random"), + ); + expect(resp.status).toBe(302); + const location = resp.headers.get("location")!; + expect(location).toStartWith("/movies/"); + const id = location.substring("/movies/".length); + expect(isUuid(id)).toBe(true); + }); }); });