52 lines
1.3 KiB
TypeScript
52 lines
1.3 KiB
TypeScript
import { Router } from "@oak/oak";
|
|
import { parseOptionalAuth } from "../lib/auth.ts";
|
|
import { parsePagination } from "../lib/pagination.ts";
|
|
import { searchDumps } from "../services/dump-service.ts";
|
|
import { searchUsersGlobal } from "../services/user-service.ts";
|
|
import { searchPlaylists } from "../services/playlist-service.ts";
|
|
|
|
const router = new Router({ prefix: "/api/search" });
|
|
|
|
router.get("/", async (ctx) => {
|
|
const params = ctx.request.url.searchParams;
|
|
const q = (params.get("q") ?? "").trim();
|
|
const { page, limit } = parsePagination(params);
|
|
const requestingUserId = (await parseOptionalAuth(ctx)) ?? undefined;
|
|
|
|
if (!q) {
|
|
ctx.response.body = {
|
|
success: true,
|
|
data: {
|
|
dumps: { items: [], total: 0, hasMore: false },
|
|
users: [],
|
|
playlists: [],
|
|
},
|
|
};
|
|
return;
|
|
}
|
|
|
|
const { items: dumpItems, total: dumpTotal } = searchDumps(
|
|
q,
|
|
page,
|
|
limit,
|
|
requestingUserId,
|
|
);
|
|
const users = searchUsersGlobal(q, 20);
|
|
const playlists = searchPlaylists(q, 20, requestingUserId);
|
|
|
|
ctx.response.body = {
|
|
success: true,
|
|
data: {
|
|
dumps: {
|
|
items: dumpItems,
|
|
total: dumpTotal,
|
|
hasMore: page * limit < dumpTotal,
|
|
},
|
|
users,
|
|
playlists,
|
|
},
|
|
};
|
|
});
|
|
|
|
export default router;
|