forked from mickasmt/next-saas-stripe-starter
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from and-voila/srizvi/issue38
chore: migrate lib and api files
- Loading branch information
Showing
31 changed files
with
1,547 additions
and
6 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
app/api/courses/[courseId]/attachments/[attachmentId]/route.ts
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,40 @@ | ||
import { NextResponse } from 'next/server'; | ||
|
||
import { db } from '@/app/lib/db'; | ||
import { getCurrentUser } from '@/app/lib/session'; | ||
import { isTeacher } from '@/app/lib/teacher'; | ||
|
||
export async function DELETE( | ||
req: Request, | ||
{ params }: { params: { courseId: string; attachmentId: string } }, | ||
) { | ||
try { | ||
const user = await getCurrentUser(); | ||
const userId = user?.id; | ||
|
||
if (!isTeacher(userId)) { | ||
return new NextResponse('Unauthorized', { status: 401 }); | ||
} | ||
|
||
const course = await db.course.findUnique({ | ||
where: { | ||
id: params.courseId, | ||
}, | ||
}); | ||
|
||
if (!course) { | ||
return new NextResponse('Unauthorized', { status: 401 }); | ||
} | ||
|
||
const attachment = await db.attachment.delete({ | ||
where: { | ||
courseId: params.courseId, | ||
id: params.attachmentId, | ||
}, | ||
}); | ||
|
||
return NextResponse.json(attachment); | ||
} catch (error) { | ||
return new NextResponse('Internal Error', { status: 500 }); | ||
} | ||
} |
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,42 @@ | ||
import { NextResponse } from 'next/server'; | ||
|
||
import { db } from '@/app/lib/db'; | ||
import { getCurrentUser } from '@/app/lib/session'; | ||
import { isTeacher } from '@/app/lib/teacher'; | ||
|
||
export async function POST( | ||
req: Request, | ||
{ params }: { params: { courseId: string } }, | ||
) { | ||
try { | ||
const user = await getCurrentUser(); | ||
const userId = user?.id; | ||
const { url } = await req.json(); | ||
|
||
if (!isTeacher(userId)) { | ||
return new NextResponse('Unauthorized', { status: 401 }); | ||
} | ||
|
||
const course = await db.course.findUnique({ | ||
where: { | ||
id: params.courseId, | ||
}, | ||
}); | ||
|
||
if (!course) { | ||
return new NextResponse('Unauthorized', { status: 401 }); | ||
} | ||
|
||
const attachment = await db.attachment.create({ | ||
data: { | ||
url, | ||
name: url.split('/').pop(), | ||
courseId: params.courseId, | ||
}, | ||
}); | ||
|
||
return NextResponse.json(attachment); | ||
} catch (error) { | ||
return new NextResponse('Internal Error', { status: 500 }); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
app/api/courses/[courseId]/chapters/[chapterId]/progress/route.ts
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,40 @@ | ||
import { NextResponse } from 'next/server'; | ||
|
||
import { db } from '@/app/lib/db'; | ||
import { getCurrentUser } from '@/app/lib/session'; | ||
|
||
export async function PUT( | ||
req: Request, | ||
{ params }: { params: { courseId: string; chapterId: string } }, | ||
) { | ||
try { | ||
const user = await getCurrentUser(); | ||
const userId = user?.id; | ||
const { isCompleted } = await req.json(); | ||
|
||
if (!userId) { | ||
return new NextResponse('Unauthorized', { status: 401 }); | ||
} | ||
|
||
const userProgress = await db.userProgress.upsert({ | ||
where: { | ||
userId_chapterId: { | ||
userId, | ||
chapterId: params.chapterId, | ||
}, | ||
}, | ||
update: { | ||
isCompleted, | ||
}, | ||
create: { | ||
userId, | ||
chapterId: params.chapterId, | ||
isCompleted, | ||
}, | ||
}); | ||
|
||
return NextResponse.json(userProgress); | ||
} catch (error) { | ||
return new NextResponse('Internal Error', { status: 500 }); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
app/api/courses/[courseId]/chapters/[chapterId]/publish/route.ts
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,66 @@ | ||
import { NextResponse } from 'next/server'; | ||
|
||
import { db } from '@/app/lib/db'; | ||
import { getCurrentUser } from '@/app/lib/session'; | ||
import { isTeacher } from '@/app/lib/teacher'; | ||
|
||
export async function PATCH( | ||
req: Request, | ||
{ params }: { params: { courseId: string; chapterId: string } }, | ||
) { | ||
try { | ||
const user = await getCurrentUser(); | ||
const userId = user?.id; | ||
|
||
if (!isTeacher(userId)) { | ||
return new NextResponse('Unauthorized', { status: 401 }); | ||
} | ||
|
||
const course = await db.course.findUnique({ | ||
where: { | ||
id: params.courseId, | ||
}, | ||
}); | ||
|
||
if (!course) { | ||
return new NextResponse('Unauthorized', { status: 401 }); | ||
} | ||
|
||
const chapter = await db.chapter.findUnique({ | ||
where: { | ||
id: params.chapterId, | ||
courseId: params.courseId, | ||
}, | ||
}); | ||
|
||
const muxData = await db.muxData.findUnique({ | ||
where: { | ||
chapterId: params.chapterId, | ||
}, | ||
}); | ||
|
||
if ( | ||
!chapter || | ||
!muxData || | ||
!chapter.title || | ||
!chapter.description || | ||
!chapter.videoUrl | ||
) { | ||
return new NextResponse('Missing required fields', { status: 400 }); | ||
} | ||
|
||
const publishedChapter = await db.chapter.update({ | ||
where: { | ||
id: params.chapterId, | ||
courseId: params.courseId, | ||
}, | ||
data: { | ||
isPublished: true, | ||
}, | ||
}); | ||
|
||
return NextResponse.json(publishedChapter); | ||
} catch (error) { | ||
return new NextResponse('Internal Error', { status: 500 }); | ||
} | ||
} |
161 changes: 161 additions & 0 deletions
161
app/api/courses/[courseId]/chapters/[chapterId]/route.ts
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,161 @@ | ||
import { NextResponse } from 'next/server'; | ||
import Mux from '@mux/mux-node'; | ||
|
||
import { db } from '@/app/lib/db'; | ||
import { getCurrentUser } from '@/app/lib/session'; | ||
import { isTeacher } from '@/app/lib/teacher'; | ||
|
||
const { Video } = new Mux( | ||
process.env.MUX_TOKEN_ID!, | ||
process.env.MUX_TOKEN_SECRET!, | ||
); | ||
|
||
export async function DELETE( | ||
req: Request, | ||
{ params }: { params: { courseId: string; chapterId: string } }, | ||
) { | ||
try { | ||
const user = await getCurrentUser(); | ||
const userId = user?.id; | ||
|
||
if (!isTeacher(userId)) { | ||
return new NextResponse('Unauthorized', { status: 401 }); | ||
} | ||
|
||
const course = await db.course.findUnique({ | ||
where: { | ||
id: params.courseId, | ||
}, | ||
}); | ||
|
||
if (!course) { | ||
return new NextResponse('Unauthorized', { status: 401 }); | ||
} | ||
|
||
const chapter = await db.chapter.findUnique({ | ||
where: { | ||
id: params.chapterId, | ||
courseId: params.courseId, | ||
}, | ||
}); | ||
|
||
if (!chapter) { | ||
return new NextResponse('Not Found', { status: 404 }); | ||
} | ||
|
||
if (chapter.videoUrl) { | ||
const existingMuxData = await db.muxData.findFirst({ | ||
where: { | ||
chapterId: params.chapterId, | ||
}, | ||
}); | ||
|
||
if (existingMuxData) { | ||
await Video.Assets.del(existingMuxData.assetId); | ||
await db.muxData.delete({ | ||
where: { | ||
id: existingMuxData.id, | ||
}, | ||
}); | ||
} | ||
} | ||
|
||
const deletedChapter = await db.chapter.delete({ | ||
where: { | ||
id: params.chapterId, | ||
}, | ||
}); | ||
|
||
const publishedChaptersInCourse = await db.chapter.findMany({ | ||
where: { | ||
courseId: params.courseId, | ||
isPublished: true, | ||
}, | ||
}); | ||
|
||
if (!publishedChaptersInCourse.length) { | ||
await db.course.update({ | ||
where: { | ||
id: params.courseId, | ||
}, | ||
data: { | ||
isPublished: false, | ||
}, | ||
}); | ||
} | ||
|
||
return NextResponse.json(deletedChapter); | ||
} catch (error) { | ||
return new NextResponse('Internal Error', { status: 500 }); | ||
} | ||
} | ||
|
||
export async function PATCH( | ||
req: Request, | ||
{ params }: { params: { courseId: string; chapterId: string } }, | ||
) { | ||
try { | ||
const user = await getCurrentUser(); | ||
const userId = user?.id; | ||
const { isPublished, ...values } = await req.json(); | ||
|
||
if (!isTeacher(userId)) { | ||
return new NextResponse('Unauthorized', { status: 401 }); | ||
} | ||
|
||
const course = await db.course.findUnique({ | ||
where: { | ||
id: params.courseId, | ||
}, | ||
}); | ||
|
||
if (!course) { | ||
return new NextResponse('Unauthorized', { status: 401 }); | ||
} | ||
|
||
const chapter = await db.chapter.update({ | ||
where: { | ||
id: params.chapterId, | ||
courseId: params.courseId, | ||
}, | ||
data: { | ||
...values, | ||
}, | ||
}); | ||
|
||
if (values.videoUrl) { | ||
const existingMuxData = await db.muxData.findFirst({ | ||
where: { | ||
chapterId: params.chapterId, | ||
}, | ||
}); | ||
|
||
if (existingMuxData) { | ||
await Video.Assets.del(existingMuxData.assetId); | ||
await db.muxData.delete({ | ||
where: { | ||
id: existingMuxData.id, | ||
}, | ||
}); | ||
} | ||
|
||
const asset = await Video.Assets.create({ | ||
input: values.videoUrl, | ||
playback_policy: 'public', | ||
test: false, | ||
}); | ||
|
||
await db.muxData.create({ | ||
data: { | ||
chapterId: params.chapterId, | ||
assetId: asset.id, | ||
playbackId: asset.playback_ids?.[0]?.id, | ||
}, | ||
}); | ||
} | ||
|
||
return NextResponse.json(chapter); | ||
} catch (error) { | ||
return new NextResponse('Internal Error', { status: 500 }); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
app/api/courses/[courseId]/chapters/[chapterId]/started/route.ts
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,40 @@ | ||
import { NextResponse } from 'next/server'; | ||
|
||
import { db } from '@/app/lib/db'; | ||
import { getCurrentUser } from '@/app/lib/session'; | ||
|
||
export async function PUT( | ||
req: Request, | ||
{ params }: { params: { courseId: string; chapterId: string } }, | ||
) { | ||
try { | ||
const user = await getCurrentUser(); | ||
const userId = user?.id; | ||
const { isStarted } = await req.json(); | ||
|
||
if (!userId) { | ||
return new NextResponse('Unauthorized', { status: 401 }); | ||
} | ||
|
||
const userProgress = await db.userProgress.upsert({ | ||
where: { | ||
userId_chapterId: { | ||
userId, | ||
chapterId: params.chapterId, | ||
}, | ||
}, | ||
update: { | ||
isStarted, | ||
}, | ||
create: { | ||
userId, | ||
chapterId: params.chapterId, | ||
isStarted, | ||
}, | ||
}); | ||
|
||
return NextResponse.json(userProgress); | ||
} catch (error) { | ||
return new NextResponse('Internal Error', { status: 500 }); | ||
} | ||
} |
Oops, something went wrong.