Add /series/random

This commit is contained in:
Zoe Roux 2025-03-02 18:12:29 +01:00
parent cc221c560d
commit f143511e14
No known key found for this signature in database
2 changed files with 36 additions and 3 deletions

View File

@ -184,7 +184,7 @@ export const movies = new Elysia({ prefix: "/movies", tags: ["movies"] })
if (!movie)
return error(404, {
status: 404,
message: "No movies in the database",
message: "No movies in the database.",
});
return redirect(`/movies/${movie.id}`);
},
@ -199,7 +199,7 @@ export const movies = new Elysia({ prefix: "/movies", tags: ["movies"] })
}),
404: {
...KError,
description: "No movie found with the given id or slug.",
description: "No movies in the database.",
},
},
},

View File

@ -1,5 +1,6 @@
import { and, eq } from "drizzle-orm";
import { and, eq, sql } from "drizzle-orm";
import { Elysia, t } from "elysia";
import { db } from "~/db";
import { shows } from "~/db/schema";
import { KError } from "~/models/error";
import { Serie, SerieTranslation } from "~/models/serie";
@ -18,6 +19,38 @@ export const series = new Elysia({ prefix: "/series", tags: ["series"] })
serie: Serie,
"serie-translation": SerieTranslation,
})
.get(
"random",
async ({ error, redirect }) => {
const [serie] = await db
.select({ id: shows.id })
.from(shows)
.where(eq(shows.kind, "serie"))
.orderBy(sql`random()`)
.limit(1);
if (!serie)
return error(404, {
status: 404,
message: "No series in the database.",
});
return redirect(`/series/${serie.id}`);
},
{
detail: {
description: "Get a random serie",
},
response: {
302: t.Void({
description:
"Redirected to the [/series/{id}](#tag/series/GET/series/{id}) route.",
}),
404: {
...KError,
description: "No series in the database.",
},
},
},
)
.get(
"",
async ({