Public
Script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import { set } from "https://esm.town/v/std/set?v=11";
let { likesData } = await import("https://esm.town/v/merlin/likesData");
import { validatePostExists } from "https://esm.town/v/merlin/validatePostExists";
import { auth } from "https://esm.town/v/merlin/auth";
export const POSTLikes = async (req: express.Request, res: express.Response) => {
auth(req, res);
if (req.method !== "POST") {
res.status(400).send("Bad request");
}
const postSlug = req?.body?.postSlug;
const deviceId = req?.get("Authorization").replace("Bearer ", "");
const validPostSlug = await validatePostExists(postSlug);
if (!validPostSlug) {
return res.status(404).send("Cannot find post");
}
const state = likesData;
likesData = {
...state,
[postSlug]: state[postSlug]
? [
...new Set([...state[postSlug], deviceId]),
]
: [deviceId],
};
await set("likesData", likesData);
return res.send("Success!");
};
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
October 23, 2023