CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/next/pages/api/v2/bookmarks/add.ts
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2024 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
import { Request } from "express";
7
8
import { getLogger } from "@cocalc/backend/logger";
9
import { saveStarredFilesBookmarks } from "@cocalc/server/bookmarks/starred";
10
import { STARRED_FILES } from "@cocalc/util/consts/bookmarks";
11
import { apiRoute, apiRouteOperation } from "lib/api";
12
import { processSetRequest } from "lib/api/bookmarks";
13
import {
14
BookmarkAddInputSchema,
15
BookmarkAddOutputSchema,
16
BookmarkSetOutputType,
17
} from "lib/api/schema/bookmarks";
18
19
const L = getLogger("api:v2:bookmark:add");
20
21
async function handle(req, res) {
22
try {
23
res.json(await add(req));
24
} catch (err) {
25
res.json({ error: `${err.message}` });
26
return;
27
}
28
}
29
30
async function add(req: Request): Promise<BookmarkSetOutputType> {
31
const { project_id, account_id, type, stars } = await processSetRequest(req);
32
33
switch (type) {
34
case STARRED_FILES: {
35
L.debug("set", { project_id, stars });
36
await saveStarredFilesBookmarks({
37
project_id,
38
stars,
39
account_id,
40
mode: "add",
41
});
42
43
return { status: "success", project_id, type };
44
}
45
46
default:
47
return { status: "error", error: `cannot handle type '${type}'` };
48
}
49
}
50
51
export default apiRoute({
52
addBookmarks: apiRouteOperation({
53
method: "POST",
54
openApiOperation: {
55
tags: ["Projects"],
56
},
57
})
58
.input({
59
contentType: "application/json",
60
body: BookmarkAddInputSchema,
61
})
62
.outputs([
63
{
64
status: 200,
65
contentType: "application/json",
66
body: BookmarkAddOutputSchema,
67
},
68
])
69
.handler(handle),
70
});
71
72