Skip to content

Commit

Permalink
bookmark api
Browse files Browse the repository at this point in the history
  • Loading branch information
rahulsainlll committed Aug 12, 2024
1 parent 318b9d3 commit c90000b
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 100 deletions.
23 changes: 0 additions & 23 deletions actions/saveBookmark.tsx

This file was deleted.

36 changes: 27 additions & 9 deletions app/api/bookmarks/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,35 @@
// 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 { NextResponse } from "next/server";
import prisma from "@/lib/prisma";
import { auth } from "@clerk/nextjs/server";
import { getServerSession } from "next-auth/next";
import { authOptions } from "../../auth/[...nextauth]/route";

export async function DELETE(req: Request) {
const { userId } = auth();
const session = await getServerSession(authOptions);

if (!userId) {
if (!session?.user?.email) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}

const url = new URL(req.url);
const id = url.pathname.split("/").pop();

try {
// Find the user by email to get their ID
const user = await prisma.user.findUnique({
where: { email: session.user.email },
});

if (!user) {
return NextResponse.json({ error: "User not found" }, { status: 404 });
}

const result = await prisma.bookmark.deleteMany({
where: {
id: id as string,
userId,
userId: user.id,
},
});

Expand All @@ -42,9 +51,9 @@ export async function DELETE(req: Request) {
}

export async function PUT(req: Request) {
const { userId } = auth();
const session = await getServerSession(authOptions);

if (!userId) {
if (!session?.user?.email) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}

Expand All @@ -54,10 +63,19 @@ export async function PUT(req: Request) {
const { name, url: bookmarkUrl, description } = body;

try {
// Find the user by email to get their ID
const user = await prisma.user.findUnique({
where: { email: session.user.email },
});

if (!user) {
return NextResponse.json({ error: "User not found" }, { status: 404 });
}

const result = await prisma.bookmark.updateMany({
where: {
id: id as string,
userId,
userId: user.id,
},
data: { name, url: bookmarkUrl, description },
});
Expand All @@ -76,4 +94,4 @@ export async function PUT(req: Request) {
{ status: 500 }
);
}
}
}
132 changes: 66 additions & 66 deletions app/api/bookmarks/route.ts
Original file line number Diff line number Diff line change
@@ -1,78 +1,78 @@
// // 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 { NextResponse } from "next/server";
import prisma from "@/lib/prisma";
import { getServerSession } from "next-auth/next";
import { authOptions } from "../auth/[...nextauth]/route";

// import { NextResponse } from "next/server";
// import prisma from "@/lib/prisma";
// import { auth } from "@clerk/nextjs/server";
export async function GET(req: Request) {
const session = await getServerSession(authOptions);

// export async function GET(req: Request) {
// const { userId } = auth();
if (!session?.user?.email) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}

// if (!userId) {
// return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
// }
try {
const user = await prisma.user.findUnique({
where: { email: session.user.email },
});

// try {
// const bookmarks = await prisma.bookmark.findMany({
// where: { userId },
// });
// return NextResponse.json(bookmarks);
// } catch (error) {
// return NextResponse.json(
// { error: "Failed to fetch bookmarks" },
// { status: 500 }
// );
// }
// }
if (!user) {
return NextResponse.json({ error: "User not found" }, { status: 404 });
}

// export async function POST(req: Request) {
// const { userId } = auth();
const bookmarks = await prisma.bookmark.findMany({
where: { userId: user.id },
});

// if (!userId) {
// return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
// }
return NextResponse.json(bookmarks);
} catch (error) {
return NextResponse.json(
{ error: "Failed to fetch bookmarks" },
{ status: 500 }
);
}
}

// try {
// // Check if the user exists
// const userExists = await prisma.user.findUnique({
// where: { id: userId },
// });
export async function POST(req: Request) {
const session = await getServerSession(authOptions);

// if (!userExists) {
// return NextResponse.json(
// { error: "User does not exist" },
// { status: 404 }
// );
// }
if (!session?.user?.email) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}

// const body = await req.json();
// const { name, url, description } = body;
try {
const user = await prisma.user.findUnique({
where: { email: session.user.email },
});

// if (!name || !url) {
// return NextResponse.json(
// { error: "Name and URL are required" },
// { status: 400 }
// );
// }
if (!user) {
return NextResponse.json({ error: "User not found" }, { status: 404 });
}

// const bookmark = await prisma.bookmark.create({
// data: {
// name,
// url,
// description: description || null,
// userId,
// },
// });
const body = await req.json();
const { name, url, description } = body;

// return NextResponse.json(bookmark, { status: 201 });
// } catch (error) {
// console.error("Failed to create bookmark:", error); // Added logging for better debugging
// return NextResponse.json(
// { error: "Failed to create bookmark" },
// { status: 500 }
// );
// }
// }
if (!name || !url) {
return NextResponse.json(
{ error: "Name and URL are required" },
{ status: 400 }
);
}

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

return NextResponse.json(bookmark, { status: 201 });
} catch (error) {
console.error("Failed to create bookmark:", error);
return NextResponse.json(
{ error: "Failed to create bookmark" },
{ status: 500 }
);
}
}
5 changes: 3 additions & 2 deletions app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Input } from "@/components/ui/input";
import { Button } from "@/components/ui/button";
import NewBookmarkBtn from "@/components/new-bookmark";


import {
Table,
TableBody,
Expand Down Expand Up @@ -53,7 +54,7 @@ export default function Home() {
Required fields are marked with an asterisk (*).
</p>

<div className="max-w-xl flex gap-4 mb-8">
<div className="max-w-xl flex items-center gap-4 mb-8">
<div>
<p>Owner Name *</p>
<Input
Expand All @@ -71,7 +72,7 @@ export default function Home() {
placeholder="Repository Name"
/>
</div>
<Button onClick={handleSearchRepos} className="btn btn-primary">
<Button onClick={handleSearchRepos} className="ml-2">
Search
</Button>
</div>
Expand Down
16 changes: 16 additions & 0 deletions types/next-auth.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import NextAuth from "next-auth";

declare module "next-auth" {
interface Session {
user: {
id: string;
name?: string | null;
email?: string | null;
image?: string | null;
};
}

interface User {
id: string;
}
}

0 comments on commit c90000b

Please sign in to comment.