33 lines
1.2 KiB
TypeScript
33 lines
1.2 KiB
TypeScript
import type Database from "bun:sqlite";
|
|
|
|
import type { RuntimeMode } from "../../../shared/api";
|
|
import type { Logger } from "../../logger";
|
|
|
|
import { listProjects } from "../../db/projects";
|
|
import { createApiError, jsonResponse } from "../../helpers";
|
|
import { validatePagination } from "../../middleware";
|
|
|
|
export function handleListProjects(req: Request, db: Database, mode: RuntimeMode, _logger: Logger): Response {
|
|
const url = new URL(req.url);
|
|
const pageParam = url.searchParams.get("page");
|
|
const pageSizeParam = url.searchParams.get("pageSize");
|
|
const keyword = url.searchParams.get("keyword");
|
|
const statusParam = url.searchParams.get("status");
|
|
|
|
const pagination = validatePagination(pageParam, pageSizeParam, mode);
|
|
if (pagination instanceof Response) return pagination;
|
|
|
|
if (statusParam && statusParam !== "active" && statusParam !== "archived") {
|
|
return jsonResponse(createApiError("Invalid status parameter", 400), { mode, status: 400 });
|
|
}
|
|
|
|
const result = listProjects(db, {
|
|
keyword: keyword ?? undefined,
|
|
page: pagination.page,
|
|
pageSize: pagination.pageSize,
|
|
status: (statusParam as "active" | "archived") ?? undefined,
|
|
});
|
|
|
|
return jsonResponse(result, { mode });
|
|
}
|