47 lines
1.1 KiB
JavaScript
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;
|