37 lines
956 B
TypeScript
37 lines
956 B
TypeScript
|
import Page from "#app/models/page.js"
|
||
|
import type { HttpContext } from "@adonisjs/core/http"
|
||
|
import router from "@adonisjs/core/services/router"
|
||
|
import { sanitizePageTitle } from "@repo/wikipedia-game-solver/wikipedia-utils"
|
||
|
import vine from "@vinejs/vine"
|
||
|
|
||
|
const requestValidator = 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]),
|
||
|
}),
|
||
|
)
|
||
|
|
||
|
class Controller {
|
||
|
public async handle(context: HttpContext): Promise<Page[]> {
|
||
|
const payload = await context.request.validateUsing(requestValidator)
|
||
|
const pages = await Page.query()
|
||
|
.whereLike("title", `${payload.title}%`)
|
||
|
.limit(payload.limit)
|
||
|
return pages
|
||
|
}
|
||
|
}
|
||
|
|
||
|
router.get("/wikipedia/pages", [Controller])
|