v3: search engine, responsive header with compact user menu
This commit is contained in:
47
api/routes/search.ts
Normal file
47
api/routes/search.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
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;
|
||||
Reference in New Issue
Block a user