forked from rahulsainlll/git-trace
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d9aab86
commit a29ea3a
Showing
3 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |