From f143511e14d4756d5b81512e31f4d367248101fa Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Sun, 2 Mar 2025 18:12:29 +0100 Subject: [PATCH] Add /series/random --- api/src/controllers/shows/movies.ts | 4 ++-- api/src/controllers/shows/series.ts | 35 ++++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/api/src/controllers/shows/movies.ts b/api/src/controllers/shows/movies.ts index 2b528a78..4ad04fb9 100644 --- a/api/src/controllers/shows/movies.ts +++ b/api/src/controllers/shows/movies.ts @@ -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.", }, }, }, diff --git a/api/src/controllers/shows/series.ts b/api/src/controllers/shows/series.ts index 0347991c..0d3c8c16 100644 --- a/api/src/controllers/shows/series.ts +++ b/api/src/controllers/shows/series.ts @@ -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 ({