Files
forgetmenot-nextjs/web/pages/api/v1/bookmarks/[bookmarkId].js
2022-07-05 21:15:42 -05:00

47 lines
1.1 KiB
JavaScript

import db from "../../../../lib/db";
import { withErrorHandling, withSession } from "../../../../lib/apiHelpers";
const handler = withErrorHandling(
withSession(async (req, res, session) => {
const { method } = req;
const { user } = session;
switch (method) {
case "GET":
await getBookmark(req, res, user);
break;
default:
res.setHeader("Allow", ["GET", "POST"]);
res.status(405).end(`Method ${method} Not Allowed`);
}
})
);
const getBookmark = async (req, res, user) => {
const { bookmarkId } = req.query;
if (!bookmarkId) {
res.status(404).end();
return;
}
const params = {
TableName: process.env.FMN_DYNAMODB_TABLENAME,
KeyConditionExpression: "hk = :hk AND sk = :sk",
ExpressionAttributeValues: {
":hk": `user#${user.sub}`,
":sk": `bookmark#${bookmarkId}`,
},
};
let bookmarks = await db.query(params).promise();
if (bookmarks.Items.length == 1) {
res.status(200).json(bookmarks.Items[0]);
} else {
res.status(404).end();
}
};
export default handler;