Skip to content

Commit

Permalink
Add /movies/random route
Browse files Browse the repository at this point in the history
  • Loading branch information
zoriya committed Jan 13, 2025
1 parent 8b7e109 commit 9a54266
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion api/src/controllers/movies.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { and, eq, sql } from "drizzle-orm";
import { Elysia, t } from "elysia";
import { Elysia, redirect, t } from "elysia";
import { KError } from "~/models/error";
import { comment } from "~/utils";
import { db } from "../db";
Expand Down Expand Up @@ -154,6 +154,41 @@ export const movies = new Elysia({ prefix: "/movies", tags: ["movies"] })
},
},
)
.get(
"random",
async ({ error, redirect }) => {
const [id] = await db
.select({ id: shows.id })
.from(shows)
.where(eq(shows.kind, "movie"))
.orderBy(sql`random()`)
.limit(1);
if (!id)
return error(404, {
status: 404,
message: "No movies in the database",
});
return redirect(`/movies/${id}`);
},
{
detail: {
description: "Get a random movie",
},
response: {
302: t.Void({
description:
"Redirected to the [/movies/id](#tag/movies/GET/movies/{id}) route.",
}),
404: {
...KError,
description: "No movie found with the given id or slug.",
examples: [
{ status: 404, message: "Movie not found", details: undefined },
],
},
},
},
)
.get(
"",
async ({
Expand Down

0 comments on commit 9a54266

Please sign in to comment.