Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added course purchases and auth system #324

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"nextjs-toploader": "^1.6.11",
"node-fetch": "^3.3.2",
"notion-client": "^6.16.0",
"razorpay": "^2.9.2",
"pdf-lib": "^1.17.1",
"react": "^18",
"react-dom": "^18",
Expand Down
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;
132 changes: 82 additions & 50 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ datasource db {

model Course {
id Int @id @default(autoincrement())
price Int @default(1000)
appxCourseId Int
discordRoleId String
title String
Expand All @@ -19,8 +20,10 @@ model Course {
slug String
content CourseContent[]
purchasedBy UserPurchases[]
purchases Purchase[]
receipts Receipt[]
certificate Certificate[]
certIssued Boolean @default(false)
certIssued Boolean @default(false)
}

model UserPurchases {
Expand Down Expand Up @@ -64,12 +67,12 @@ model CourseContent {
}

model Certificate {
id String @id @default(cuid())
slug String @default("certId")
user User @relation(fields: [userId], references: [id])
userId String
course Course @relation(fields: [courseId], references: [id])
courseId Int
id String @id @default(cuid())
slug String @default("certId")
user User @relation(fields: [userId], references: [id])
userId String
course Course @relation(fields: [courseId], references: [id])
courseId Int

@@unique([userId, courseId])
}
Expand Down Expand Up @@ -153,6 +156,33 @@ model User {
questions Question[]
answers Answer[]
certificate Certificate[]
transactions Purchase[]
receipts Receipt[]
}

model Receipt {
id String @id @default(cuid())
user User @relation(fields: [userId], references: [id])
userId String
course Course @relation(fields: [courseId], references: [id])
courseId Int @default(0)
razorpayOrderId String? @unique
purchase Purchase?
}

model Purchase {
id String @id @default(cuid())
receipt Receipt @relation(fields: [receiptId], references: [id])
receiptId String @unique
razorpay_payment_id String @unique
razorpay_order_id String @unique
razorpay_signature String
purchasedAt DateTime @default(now())
paymentVerified Boolean @default(false)
purchasedCourse Course @relation(fields: [purchasedCourseId], references: [id])
purchasedCourseId Int
purchasedBy User @relation(fields: [purchasedById], references: [id])
purchasedById String
}

model DiscordConnect {
Expand Down Expand Up @@ -212,74 +242,76 @@ model Comment {
votes Vote[]
isPinned Boolean @default(false)
}

model Question {
id Int @id @default(autoincrement())
title String
content String
slug String @unique
createdAt DateTime @default(now())
author User @relation(fields: [authorId], references: [id])
authorId String
upvotes Int @default(0)
downvotes Int @default(0)
id Int @id @default(autoincrement())
title String
content String
slug String @unique
createdAt DateTime @default(now())
author User @relation(fields: [authorId], references: [id])
authorId String
upvotes Int @default(0)
downvotes Int @default(0)
totalanswers Int @default(0)
answers Answer[]
votes Vote[]
tags String[]
updatedAt DateTime @updatedAt
answers Answer[]
votes Vote[]
tags String[]
updatedAt DateTime @updatedAt

@@index([authorId])
}

model Answer {
id Int @id @default(autoincrement())
content String
createdAt DateTime @default(now())
question Question @relation(fields: [questionId], references: [id])
questionId Int
author User @relation(fields: [authorId], references: [id])
authorId String
votes Vote[]
upvotes Int @default(0)
downvotes Int @default(0)
totalanswers Int @default(0)
parentId Int?
responses Answer[] @relation("AnswerToAnswer")
parent Answer? @relation("AnswerToAnswer", fields: [parentId], references: [id])
updatedAt DateTime @updatedAt
id Int @id @default(autoincrement())
content String
createdAt DateTime @default(now())
question Question @relation(fields: [questionId], references: [id])
questionId Int
author User @relation(fields: [authorId], references: [id])
authorId String
votes Vote[]
upvotes Int @default(0)
downvotes Int @default(0)
totalanswers Int @default(0)
parentId Int?
responses Answer[] @relation("AnswerToAnswer")
parent Answer? @relation("AnswerToAnswer", fields: [parentId], references: [id])
updatedAt DateTime @updatedAt

@@index([questionId])
@@index([authorId])
@@index([parentId])
}


model Vote {
id Int @id @default(autoincrement())
questionId Int?
question Question? @relation(fields: [questionId], references: [id])
answerId Int?
answer Answer? @relation(fields: [answerId], references: [id])
commentId Int?
comment Comment? @relation(fields: [commentId], references: [id])
userId String
user User @relation(fields: [userId], references: [id])
voteType VoteType
createdAt DateTime @default(now())
id Int @id @default(autoincrement())
questionId Int?
question Question? @relation(fields: [questionId], references: [id])
answerId Int?
answer Answer? @relation(fields: [answerId], references: [id])
commentId Int?
comment Comment? @relation(fields: [commentId], references: [id])
userId String
user User @relation(fields: [userId], references: [id])
voteType VoteType
createdAt DateTime @default(now())

@@unique([questionId, userId])
@@unique([answerId, userId])
@@unique([commentId, userId])
@@unique([questionId, userId])
@@unique([answerId, userId])
@@unique([commentId, userId])
}

enum VoteType {
UPVOTE
DOWNVOTE
}

enum PostType {
QUESTION
ANSWER
}

enum CommentType {
INTRO
DEFAULT
Expand Down
36 changes: 36 additions & 0 deletions src/app/api/auth/register/route.ts
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 },
);
}
}
Loading
Loading