From 8d7d5f3e7e35eb4adde3e6e47505dd2959840507 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Sun, 9 Mar 2025 19:26:47 +0100 Subject: [PATCH] Add /staff routes --- api/src/controllers/staff.ts | 94 ++++++++++++++++++++++++++++++++++ api/src/controllers/studios.ts | 4 +- api/src/elysia.ts | 2 + api/src/index.ts | 1 + 4 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 api/src/controllers/staff.ts diff --git a/api/src/controllers/staff.ts b/api/src/controllers/staff.ts new file mode 100644 index 00000000..b43817ce --- /dev/null +++ b/api/src/controllers/staff.ts @@ -0,0 +1,94 @@ +import { sql } from "drizzle-orm"; +import Elysia, { t } from "elysia"; +import { db } from "~/db"; +import { staff } from "~/db/schema/staff"; +import { KError } from "~/models/error"; +import { Role, Staff } from "~/models/staff"; +import { Filter, Page, Sort } from "~/models/utils"; +import { desc } from "~/models/utils/descriptions"; + +export const staffH = new Elysia({ tags: ["staff"] }) + .model({ + staff: Staff, + role: Role, + }) + .get( + "/staff/:id", + async ({ params: { id }, error, set }) => { + throw new Error(); + }, + { + detail: { + description: "Get a staff member by id or slug.", + }, + params: t.Object({ + id: t.String({ + description: "The id or slug of the staff to retrieve.", + example: "hiroyuki-sawano", + }), + }), + response: { + 200: "staff", + 404: { + ...KError, + description: "No staff found with the given id or slug.", + }, + }, + }, + ) + .get( + "/staff/random", + async ({ error, redirect }) => { + const [member] = await db + .select({ slug: staff.slug }) + .from(staff) + .orderBy(sql`random()`) + .limit(1); + if (!member) + return error(404, { + status: 404, + message: "No staff in the database.", + }); + return redirect(`/staff/${member.slug}`); + }, + { + detail: { + description: "Get a random staff member.", + }, + response: { + 302: t.Void({ + description: + "Redirected to the [/staff/{id}](#tag/staff/GET/staff/{id}) route.", + }), + 404: { + ...KError, + description: "No staff in the database.", + }, + }, + }, + ) + .get( + "/staff", + async ({ query: { limit, after, query }, request: { url } }) => { + throw new Error(); + }, + { + detail: { + description: "Get all staff members known by kyoo.", + }, + query: t.Object({ + query: t.Optional(t.String({ description: desc.query })), + limit: t.Integer({ + minimum: 1, + maximum: 250, + default: 50, + description: "Max page size.", + }), + after: t.Optional(t.String({ description: desc.after })), + }), + response: { + 200: Page(Staff), + 422: KError, + }, + }, + ); diff --git a/api/src/controllers/studios.ts b/api/src/controllers/studios.ts index 68b3a3d3..66eb483b 100644 --- a/api/src/controllers/studios.ts +++ b/api/src/controllers/studios.ts @@ -156,7 +156,7 @@ export const studiosH = new Elysia({ prefix: "/studios", tags: ["studios"] }) }, params: t.Object({ id: t.String({ - description: "The id or slug of the collection to retrieve.", + description: "The id or slug of the studio to retrieve.", example: "mappa", }), }), @@ -173,7 +173,7 @@ export const studiosH = new Elysia({ prefix: "/studios", tags: ["studios"] }) 200: "studio", 404: { ...KError, - description: "No collection found with the given id or slug.", + description: "No studio found with the given id or slug.", }, 422: KError, }, diff --git a/api/src/elysia.ts b/api/src/elysia.ts index 561b23a9..c5c5f59e 100644 --- a/api/src/elysia.ts +++ b/api/src/elysia.ts @@ -6,6 +6,7 @@ import { collections } from "./controllers/shows/collections"; import { movies } from "./controllers/shows/movies"; import { series } from "./controllers/shows/series"; import { showsH } from "./controllers/shows/shows"; +import { staffH } from "./controllers/staff"; import { studiosH } from "./controllers/studios"; import { videosH } from "./controllers/videos"; import type { KError } from "./models/error"; @@ -54,4 +55,5 @@ export const app = new Elysia() .use(seasonsH) .use(videosH) .use(studiosH) + .use(staffH) .use(seed); diff --git a/api/src/index.ts b/api/src/index.ts index 65e74f53..4c9a076e 100644 --- a/api/src/index.ts +++ b/api/src/index.ts @@ -64,6 +64,7 @@ app `, }, { name: "studios", description: "Routes about studios" }, + { name: "staff", description: "Routes about staff & roles" }, ], }, }),