A few more api call basics
This commit is contained in:
46
web/pages/api/v1/bookmarks/[bookmarkId].js
Normal file
46
web/pages/api/v1/bookmarks/[bookmarkId].js
Normal 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;
|
||||
Reference in New Issue
Block a user