mirror of
https://github.com/SamyRai/tercul-frontend.git
synced 2025-12-27 00:11:35 +00:00
- 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>
54 lines
1.2 KiB
TypeScript
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;
|