-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding diary page to perform crud operation
- Loading branch information
Showing
7 changed files
with
420 additions
and
9 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
14 changes: 14 additions & 0 deletions
14
prisma/migrations/20241012022822_added_schema/migration.sql
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,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; |
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
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 @@ | ||
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" }); | ||
} | ||
} |
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,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 }); | ||
} | ||
} |
Oops, something went wrong.