tercul-frontend/server/routes/search.ts
google-labs-jules[bot] 48c4c91d05
Implement advanced search filters UI and backend support (#14)
- Add Author, Language, Work Type, Date Range filters
- Implement Save Search Preferences
- Add backend routes for search and filtering with client-side pagination support

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
2025-12-01 11:39:24 +01:00

54 lines
1.2 KiB
TypeScript

import { Router } from "express";
import type { Request } from "express";
import { graphqlClient } from "../lib/graphqlClient";
import { respondWithError } from "../lib/error";
import {
WorksDocument,
AuthorsDocument,
type WorksQuery,
type AuthorsQuery,
} from "../../shared/generated/graphql";
const router = Router();
interface GqlRequest extends Request {
gql?: typeof graphqlClient;
}
// GET /api/search
router.get("/", async (req: GqlRequest, res) => {
try {
const q = req.query.q as string;
if (!q) {
return res.json({ works: [], authors: [] });
}
const client = req.gql || graphqlClient;
// Fetch works matching the query
const { works } = await client.request<WorksQuery>(
WorksDocument,
{
search: q,
limit: 10 // Limit results for general search
}
);
// Fetch authors matching the query
const { authors } = await client.request<AuthorsQuery>(
AuthorsDocument,
{
search: q,
limit: 10
}
);
res.json({ works, authors });
} catch (error) {
respondWithError(res, error, "Failed to perform search");
}
});
export default router;