A few more api call basics

This commit is contained in:
2022-07-05 21:15:42 -05:00
parent 2fa58305cb
commit d1495df5a1
4 changed files with 108 additions and 39 deletions

View File

@@ -0,0 +1,46 @@
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;