-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
6e0bc8a
commit 7619534
Showing
15 changed files
with
846 additions
and
55 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
54 changes: 54 additions & 0 deletions
54
prisma/migrations/20240521013022_add_user_course_purchase/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,54 @@ | ||
-- AlterTable | ||
ALTER TABLE "Course" ADD COLUMN "price" INTEGER NOT NULL DEFAULT 1000; | ||
|
||
-- CreateTable | ||
CREATE TABLE "Receipt" ( | ||
"id" TEXT NOT NULL, | ||
"userId" TEXT NOT NULL, | ||
"courseId" INTEGER NOT NULL DEFAULT 0, | ||
"razorpayOrderId" TEXT, | ||
|
||
CONSTRAINT "Receipt_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateTable | ||
CREATE TABLE "Purchase" ( | ||
"id" TEXT NOT NULL, | ||
"receiptId" TEXT NOT NULL, | ||
"razorpay_payment_id" TEXT NOT NULL, | ||
"razorpay_order_id" TEXT NOT NULL, | ||
"razorpay_signature" TEXT NOT NULL, | ||
"purchasedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"paymentVerified" BOOLEAN NOT NULL DEFAULT false, | ||
"purchasedCourseId" INTEGER NOT NULL, | ||
"purchasedById" TEXT NOT NULL, | ||
|
||
CONSTRAINT "Purchase_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "Receipt_razorpayOrderId_key" ON "Receipt"("razorpayOrderId"); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "Purchase_receiptId_key" ON "Purchase"("receiptId"); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "Purchase_razorpay_payment_id_key" ON "Purchase"("razorpay_payment_id"); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "Purchase_razorpay_order_id_key" ON "Purchase"("razorpay_order_id"); | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "Receipt" ADD CONSTRAINT "Receipt_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "Receipt" ADD CONSTRAINT "Receipt_courseId_fkey" FOREIGN KEY ("courseId") REFERENCES "Course"("id") ON DELETE RESTRICT ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "Purchase" ADD CONSTRAINT "Purchase_receiptId_fkey" FOREIGN KEY ("receiptId") REFERENCES "Receipt"("id") ON DELETE RESTRICT ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "Purchase" ADD CONSTRAINT "Purchase_purchasedCourseId_fkey" FOREIGN KEY ("purchasedCourseId") REFERENCES "Course"("id") ON DELETE RESTRICT ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "Purchase" ADD CONSTRAINT "Purchase_purchasedById_fkey" FOREIGN KEY ("purchasedById") 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,36 @@ | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
import db from '@/db'; | ||
import bcrypt from 'bcrypt'; | ||
import { PrismaClientKnownRequestError } from '@prisma/client/runtime/library'; | ||
|
||
export async function POST(req: NextRequest) { | ||
try { | ||
const { name, email, password } = await req.json(); | ||
|
||
const hashedPassword = await bcrypt.hash(password, 10); | ||
await db.user.create({ | ||
data: { | ||
email, | ||
name, | ||
password: hashedPassword, | ||
}, | ||
}); | ||
|
||
return NextResponse.json( | ||
{ message: 'Account created sucessfully' }, | ||
{ status: 201 }, | ||
); | ||
} catch (e) { | ||
if (e instanceof PrismaClientKnownRequestError) { | ||
return NextResponse.json( | ||
{ error: 'Email already taken.' }, | ||
{ status: 400 }, | ||
); | ||
} | ||
console.log(e); | ||
return NextResponse.json( | ||
{ error: 'Failed to parse JSON input' }, | ||
{ status: 400 }, | ||
); | ||
} | ||
} |
Oops, something went wrong.