Skip to content

Commit

Permalink
Adding diary page to perform crud operation
Browse files Browse the repository at this point in the history
  • Loading branch information
Abhi0049k committed Oct 12, 2024
1 parent ea2b82c commit 887aa23
Show file tree
Hide file tree
Showing 7 changed files with 420 additions and 9 deletions.
46 changes: 45 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"@clerk/clerk-sdk-node": "^5.0.48",
"@clerk/nextjs": "^5.7.1",
"@prisma/client": "^5.20.0",
"@radix-ui/react-label": "^2.1.0",
"@radix-ui/react-slot": "^1.1.0",
"axios": "^1.7.7",
"class-variance-authority": "^0.7.0",
Expand Down
14 changes: 14 additions & 0 deletions prisma/migrations/20241012022822_added_schema/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- CreateTable
CREATE TABLE "Diary" (
"id" TEXT NOT NULL,
"title" TEXT NOT NULL,
"content" TEXT NOT NULL,
"userId" TEXT NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,

CONSTRAINT "Diary_pkey" PRIMARY KEY ("id")
);

-- AddForeignKey
ALTER TABLE "Diary" ADD CONSTRAINT "Diary_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
27 changes: 19 additions & 8 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,28 @@ datasource db {
}

model User {
id String @id @default(uuid())
clerkId String @unique
name String
email String @unique
id String @id @default(uuid())
clerkId String @unique
name String
email String @unique
createdAt DateTime @default(now())
Diary Diary[]
}

model Guestbook {
id Int @id @default(autoincrement())
email String
created_by String
body String
id Int @id @default(autoincrement())
email String
created_by String
body String
last_modified DateTime @default(now())
}

model Diary {
id String @id @default(uuid())
title String
content String
user User @relation(fields: [userId], references: [id])
userId String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
62 changes: 62 additions & 0 deletions src/app/api/diary/[...id]/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import prisma from "@/lib/db";
import { getAuth } from "@clerk/nextjs/server";

export async function DELETE(req, { params }) {
const { userId } = getAuth(req);
if (!userId) {
return new Response(JSON.stringify({ error: "User not authenticated" }), { status: 401 });
}
const { id } = params;
try {
const user = await prisma.user.findUnique({
where: {
clerkId: userId,
}, select: {
id: true
}
});
await prisma.diary.delete({
where: {
id: id[0],
userId: user.id
},
});
return Response.json({ message: "diary deleted successfully" });
} catch (err) {
console.log(err);
return Response.json({ error: "Error deleting diary" });
}
}

export async function PATCH(req, { params }) {
const { userId } = getAuth(req);
if (!userId) {
return new Response(JSON.stringify({ error: "User not authenticated" }), { status: 401 });
}
const { id } = params;
const body = await req.json();
const { title, content } = body;
try {
const user = await prisma.user.findUnique({
where: {
clerkId: userId,
}, select: {
id: true
}
});
await prisma.diary.update({
where: {
id: id[0],
userId: user.id
},
data: {
title,
content
},
});
return Response.json({ message: "diary updated successfully" });
} catch (err) {
console.log(err);
return Response.json({ error: "Error updating diary" });
}
}
61 changes: 61 additions & 0 deletions src/app/api/diary/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { getAuth, clerkClient } from "@clerk/nextjs/server";
import prisma from "../../../lib/db";

export async function GET(req) {
const { userId } = getAuth(req);
if (!userId) {
return new Response(JSON.stringify({ error: "User not authenticated" }), { status: 401 });
}
let user = await prisma.user.findUnique({
where: { clerkId: userId },
})
let diaries = await prisma.diary.findMany({
where: {
userId: user.id,
},
});

return Response.json({ diaries });
}

export async function POST(req) {
const { userId } = getAuth(req);

const body = await req.json();

if (!userId) {
return new Response(JSON.stringify({ error: "User not authenticated" }), { status: 401 });
}

try {
let clerkUser = await clerkClient.users.getUser(userId)
clerkUser = {
id: userId,
name: clerkUser.firstName + " " + clerkUser.lastName,
email: clerkUser.primaryEmailAddress.emailAddress,
}
let user = await prisma.user.findUnique({
where: { clerkId: clerkUser.id },
})
if (!user) {
user = await prisma.user.create({
data: {
clerkId: clerkUser.id,
name: clerkUser.name || '', // Use fullName if available
email: clerkUser.email || '', // Use emailAddress if available
},
})
}
const diary = await prisma.diary.create({
data: {
title: body.title,
content: body.content,
userId: user.id,
},
});
return new Response(JSON.stringify(diary), { status: 200 });
} catch (error) {
console.error(error);
return new Response(JSON.stringify({ error: error.message }), { status: 500 });
}
}
Loading

0 comments on commit 887aa23

Please sign in to comment.