This repository has been archived on 2024-10-12. You can view files and clone it, but cannot push or open issues or pull requests.
wikipedia-game-solver/apps/api/app/controllers/wikipedia/pages/get_wikipedia_pages.ts

46 lines
1.2 KiB
TypeScript
Raw Normal View History

2024-08-16 02:50:11 +02:00
import Page, { type PageRaw } from "#app/models/page.ts"
2024-08-15 15:14:21 +02:00
import { throttle } from "#start/limiter.ts"
import type { HttpContext } from "@adonisjs/core/http"
import router from "@adonisjs/core/services/router"
2024-08-16 02:50:11 +02:00
import { sanitizePageTitle } from "@repo/wikipedia"
import vine from "@vinejs/vine"
2024-08-16 02:50:11 +02:00
export const get_wikipedia_pages_validator = vine.compile(
vine.object({
title: vine
.string()
.minLength(1)
.maxLength(255)
.transform((value) => {
return sanitizePageTitle(value)
}),
limit: vine
.number()
.parse((value) => {
return value ?? 5
})
.withoutDecimals()
.range([1, 100]),
}),
)
2024-08-16 02:50:11 +02:00
export default class get_wikipedia_pages {
public async handle(context: HttpContext): Promise<PageRaw[]> {
const payload = await context.request.validateUsing(
get_wikipedia_pages_validator,
)
const pages = await Page.query()
.whereLike("title", `${payload.title}%`)
.limit(payload.limit)
return pages
}
}
2024-08-16 02:50:11 +02:00
router
.get("/wikipedia/pages", [get_wikipedia_pages])
.use(throttle)
.openapi({
description: "Search Wikipedia pages by title.",
tags: ["wikipedia"],
})