Skip to content

Commit

Permalink
Merge pull request #40 from and-voila/srizvi/issue38
Browse files Browse the repository at this point in the history
chore: migrate lib and api files
  • Loading branch information
srizvi authored Nov 10, 2023
2 parents edb29ff + 68080de commit b91639a
Show file tree
Hide file tree
Showing 31 changed files with 1,547 additions and 6 deletions.
40 changes: 40 additions & 0 deletions app/api/courses/[courseId]/attachments/[attachmentId]/route.ts
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 });
}
}
42 changes: 42 additions & 0 deletions app/api/courses/[courseId]/attachments/route.ts
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 app/api/courses/[courseId]/chapters/[chapterId]/progress/route.ts
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 app/api/courses/[courseId]/chapters/[chapterId]/publish/route.ts
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 app/api/courses/[courseId]/chapters/[chapterId]/route.ts
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 app/api/courses/[courseId]/chapters/[chapterId]/started/route.ts
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 });
}
}
Loading

0 comments on commit b91639a

Please sign in to comment.