|
| 1 | +import { defaultHook } from "$hooks"; |
| 2 | +import { productionCache } from "$middleware"; |
| 3 | +import { |
| 4 | + errorSchema, |
| 5 | + responseSchema, |
| 6 | + studyRoomSchema, |
| 7 | + studyRoomsPathSchema, |
| 8 | + studyRoomsQuerySchema, |
| 9 | +} from "$schema"; |
| 10 | +import { StudyRoomsService } from "$services"; |
| 11 | +import type { Bindings } from "$types/bindings"; |
| 12 | +import { OpenAPIHono, createRoute } from "@hono/zod-openapi"; |
| 13 | +import { database } from "@packages/db"; |
| 14 | + |
| 15 | +const studyRoomsRouter = new OpenAPIHono<{ Bindings: Bindings }>({ defaultHook }); |
| 16 | + |
| 17 | +const allStudyRoomsRoute = createRoute({ |
| 18 | + summary: "List all study rooms", |
| 19 | + operationId: "allStudyRooms", |
| 20 | + tags: ["Study Rooms"], |
| 21 | + method: "get", |
| 22 | + path: "/all", |
| 23 | + description: "Retrieves all available study rooms.", |
| 24 | + responses: { |
| 25 | + 200: { |
| 26 | + content: { |
| 27 | + "application/json": { schema: responseSchema(studyRoomSchema.array()) }, |
| 28 | + }, |
| 29 | + description: "Successful operation", |
| 30 | + }, |
| 31 | + 500: { |
| 32 | + content: { "application/json": { schema: errorSchema } }, |
| 33 | + description: "Server error occurred", |
| 34 | + }, |
| 35 | + }, |
| 36 | +}); |
| 37 | + |
| 38 | +const studyRoomByIdRoute = createRoute({ |
| 39 | + summary: "Retrieve a study room", |
| 40 | + operationId: "studyRoomById", |
| 41 | + tags: ["Study Rooms"], |
| 42 | + method: "get", |
| 43 | + path: "/{id}", |
| 44 | + request: { params: studyRoomsPathSchema }, |
| 45 | + description: "Retrieves a study room by its ID.", |
| 46 | + responses: { |
| 47 | + 200: { |
| 48 | + content: { "application/json": { schema: responseSchema(studyRoomSchema) } }, |
| 49 | + description: "Successful operation", |
| 50 | + }, |
| 51 | + 404: { |
| 52 | + content: { "application/json": { schema: errorSchema } }, |
| 53 | + description: "Study room not found", |
| 54 | + }, |
| 55 | + 422: { |
| 56 | + content: { "application/json": { schema: errorSchema } }, |
| 57 | + description: "Parameters failed validation", |
| 58 | + }, |
| 59 | + 500: { |
| 60 | + content: { "application/json": { schema: errorSchema } }, |
| 61 | + description: "Server error occurred", |
| 62 | + }, |
| 63 | + }, |
| 64 | +}); |
| 65 | + |
| 66 | +const studyRoomsByFiltersRoute = createRoute({ |
| 67 | + summary: "Filter study rooms", |
| 68 | + operationId: "studyRoomsByFilters", |
| 69 | + tags: ["Study Rooms"], |
| 70 | + method: "get", |
| 71 | + path: "/", |
| 72 | + request: { query: studyRoomsQuerySchema }, |
| 73 | + description: "Retrieves study rooms matching the given filters.", |
| 74 | + responses: { |
| 75 | + 200: { |
| 76 | + content: { |
| 77 | + "application/json": { schema: responseSchema(studyRoomSchema.array()) }, |
| 78 | + }, |
| 79 | + description: "Successful operation", |
| 80 | + }, |
| 81 | + 422: { |
| 82 | + content: { "application/json": { schema: errorSchema } }, |
| 83 | + description: "Parameters failed validation", |
| 84 | + }, |
| 85 | + 500: { |
| 86 | + content: { "application/json": { schema: errorSchema } }, |
| 87 | + description: "Server error occurred", |
| 88 | + }, |
| 89 | + }, |
| 90 | +}); |
| 91 | + |
| 92 | +studyRoomsRouter.get( |
| 93 | + "*", |
| 94 | + productionCache({ cacheName: "study-room-api", cacheControl: "max-age=86400" }), |
| 95 | +); |
| 96 | + |
| 97 | +studyRoomsRouter.openapi(allStudyRoomsRoute, async (c) => { |
| 98 | + const service = new StudyRoomsService(database(c.env.DB.connectionString)); |
| 99 | + return c.json( |
| 100 | + { |
| 101 | + ok: true, |
| 102 | + data: studyRoomSchema.array().parse(await service.getAllStudyRooms()), |
| 103 | + }, |
| 104 | + 200, |
| 105 | + ); |
| 106 | +}); |
| 107 | + |
| 108 | +studyRoomsRouter.openapi(studyRoomByIdRoute, async (c) => { |
| 109 | + const { id } = c.req.valid("param"); |
| 110 | + const service = new StudyRoomsService(database(c.env.DB.connectionString)); |
| 111 | + const res = await service.getStudyRoomById(id); |
| 112 | + return res |
| 113 | + ? c.json({ ok: true, data: studyRoomSchema.parse(res) }, 200) |
| 114 | + : c.json({ ok: false, message: `Study room ${id} not found` }, 404); |
| 115 | +}); |
| 116 | + |
| 117 | +studyRoomsRouter.openapi(studyRoomsByFiltersRoute, async (c) => { |
| 118 | + const query = c.req.valid("query"); |
| 119 | + const service = new StudyRoomsService(database(c.env.DB.connectionString)); |
| 120 | + return c.json( |
| 121 | + { |
| 122 | + ok: true, |
| 123 | + data: studyRoomSchema.array().parse(await service.getStudyRooms(query)), |
| 124 | + }, |
| 125 | + 200, |
| 126 | + ); |
| 127 | +}); |
| 128 | + |
| 129 | +export { studyRoomsRouter }; |
0 commit comments