Skip to content

Commit

Permalink
bkmark
Browse files Browse the repository at this point in the history
  • Loading branch information
rahulsainlll committed Aug 10, 2024
1 parent d9aab86 commit a29ea3a
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 0 deletions.
62 changes: 62 additions & 0 deletions app/api/bookmarks/[id]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// checks if the bookmark exists and is owned by the authenticated user before updating.
// updates the bookmark with the new data or returns an error if something goes wrong.
// deletes the bookmark if it exists and is owned by the authenticated user.
// returns a success message or an error if something goes wrong.

import { NextApiRequest, NextApiResponse } from "next";
import prisma from "@/lib/prisma";
import { getAuth } from "@clerk/nextjs/server";

export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
const { userId } = getAuth(req);

if (!userId) {
return res.status(401).json({ error: "Unauthorized" });
}

const { id } = req.query;

if (req.method === "DELETE") {
try {
const bookmark = await prisma.bookmark.deleteMany({
where: {
id: id as string,
userId,
},
});

if (!bookmark) {
return res.status(404).json({ error: "Bookmark not found" });
}

res.status(200).json({ message: "Bookmark deleted successfully" });
} catch (error) {
res.status(500).json({ error: "Failed to delete bookmark" });
}
} else if (req.method === "PUT") {
try {
const { name, url, description } = req.body;

const bookmark = await prisma.bookmark.updateMany({
where: {
id: id as string,
userId,
},
data: { name, url, description },
});

if (!bookmark) {
return res.status(404).json({ error: "Bookmark not found" });
}

res.status(200).json(bookmark);
} catch (error) {
res.status(500).json({ error: "Failed to update bookmark" });
}
} else {
res.status(405).json({ error: "Method not allowed" });
}
}
55 changes: 55 additions & 0 deletions app/api/bookmarks/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// post :
// Checks if the request method is POST.
// Retrieves the user’s ID using Clerk’s getAuth function.
// Creates a new bookmark associated with the logged-in user using Prisma’s create method.
// Returns the created bookmark or an error if something goes wrong.

import { NextApiRequest, NextApiResponse } from "next";
import prisma from "@/lib/prisma";
import { getAuth } from "@clerk/nextjs/server";

export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
const { userId } = getAuth(req);

if (!userId) {
return res.status(401).json({ error: "Unauthorized" });
}

if (req.method === "GET") {
try {
const bookmarks = await prisma.bookmark.findMany({
where: { userId },
});
res.status(200).json(bookmarks);
} catch (error) {
res.status(500).json({ error: "Failed to fetch bookmarks" });
}
} else if (req.method === "POST") {
try {
const { name, url, description } = req.body;
const { userId } = getAuth(req);

if (!userId) {
return res.status(401).json({ error: "Unauthorized" });
}

const bookmark = await prisma.bookmark.create({
data: {
name,
url,
description,
userId,
},
});

res.status(201).json(bookmark);
} catch (error) {
res.status(500).json({ error: "Failed to create bookmark" });
}
} else {
res.status(405).json({ error: "Method not allowed" });
}
}
17 changes: 17 additions & 0 deletions lib/prisma.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { PrismaClient } from "@prisma/client";

const prismaClientSingleton = () => {
return new PrismaClient();
};

type PrismaClientSingleton = ReturnType<typeof prismaClientSingleton>;

const globalForPrisma = globalThis as unknown as {
prisma: PrismaClientSingleton | undefined;
};

const prisma = globalForPrisma.prisma ?? prismaClientSingleton();

export default prisma;

if (process.env.NODE_ENV !== "production") globalForPrisma.prisma = prisma;

0 comments on commit a29ea3a

Please sign in to comment.