feat: Show recent searches (#2868)
* stash * root hookup * recent searches UI * feat: Add search query deletion * simplify no results state * lint
This commit is contained in:
45
server/routes/api/searches.ts
Normal file
45
server/routes/api/searches.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import Router from "koa-router";
|
||||
import auth from "@server/middlewares/authentication";
|
||||
import { SearchQuery } from "@server/models";
|
||||
import { presentSearchQuery } from "@server/presenters";
|
||||
import { assertPresent } from "@server/validation";
|
||||
import pagination from "./middlewares/pagination";
|
||||
|
||||
const router = new Router();
|
||||
|
||||
router.post("searches.list", auth(), pagination(), async (ctx) => {
|
||||
const user = ctx.state.user;
|
||||
|
||||
const searches = await SearchQuery.findAll({
|
||||
where: {
|
||||
userId: user.id,
|
||||
},
|
||||
order: [["createdAt", "DESC"]],
|
||||
offset: ctx.state.pagination.offset,
|
||||
limit: ctx.state.pagination.limit,
|
||||
});
|
||||
|
||||
ctx.body = {
|
||||
pagination: ctx.state.pagination,
|
||||
data: searches.map(presentSearchQuery),
|
||||
};
|
||||
});
|
||||
|
||||
router.post("searches.delete", auth(), async (ctx) => {
|
||||
const { id, query } = ctx.body;
|
||||
assertPresent(id || query, "id or query is required");
|
||||
|
||||
const { user } = ctx.state;
|
||||
await SearchQuery.destroy({
|
||||
where: {
|
||||
...(id ? { id } : { query }),
|
||||
userId: user.id,
|
||||
},
|
||||
});
|
||||
|
||||
ctx.body = {
|
||||
success: true,
|
||||
};
|
||||
});
|
||||
|
||||
export default router;
|
||||
Reference in New Issue
Block a user