From f3735b47d388624488df5639a3edc5e448289a25 Mon Sep 17 00:00:00 2001
From: Damian <37555910+DCRepublic@users.noreply.github.com>
Date: Thu, 31 Oct 2024 14:12:06 -0400
Subject: [PATCH] CoursePlan: add plan support
---
app/actions.ts | 18 ++
app/api/course/route.ts | 32 ---
app/api/createplan/route.ts | 78 ++++++
app/api/getcourseplans/route.ts | 31 +++
app/api/getplancourses/route.ts | 27 ++
app/api/updatePlan/route.ts | 77 ++++++
app/api/user/route.ts | 50 ++++
app/blog/layout.tsx | 13 -
app/blog/page.tsx | 9 -
app/layout.tsx | 99 +++----
app/page.tsx | 34 ++-
app/pricing/layout.tsx | 13 -
app/pricing/page.tsx | 9 -
components/CourseCard.tsx | 163 ++++++------
components/CreatePlan.tsx | 241 ++++++++++++++++++
components/FullCourseList.tsx | 6 +-
components/PlanCard.tsx | 37 +++
components/PlanCardList.tsx | 40 +++
components/Search.tsx | 1 -
components/navbar.tsx | 19 +-
lib/auth.ts | 2 +
package-lock.json | 23 ++
package.json | 1 +
.../migration.sql | 3 +
.../migration.sql | 15 ++
.../migration.sql | 2 +
.../migration.sql | 29 +++
prisma/schema.prisma | 8 +-
28 files changed, 865 insertions(+), 215 deletions(-)
create mode 100644 app/actions.ts
delete mode 100644 app/api/course/route.ts
create mode 100644 app/api/createplan/route.ts
create mode 100644 app/api/getcourseplans/route.ts
create mode 100644 app/api/getplancourses/route.ts
create mode 100644 app/api/updatePlan/route.ts
create mode 100644 app/api/user/route.ts
delete mode 100644 app/blog/layout.tsx
delete mode 100644 app/blog/page.tsx
delete mode 100644 app/pricing/layout.tsx
delete mode 100644 app/pricing/page.tsx
create mode 100644 components/CreatePlan.tsx
create mode 100644 components/PlanCard.tsx
create mode 100644 components/PlanCardList.tsx
create mode 100644 prisma/migrations/20241030150608_course_schema3/migration.sql
create mode 100644 prisma/migrations/20241030154811_course_schema2/migration.sql
create mode 100644 prisma/migrations/20241030155140_course_schema2/migration.sql
create mode 100644 prisma/migrations/20241030180417_course_schema2/migration.sql
diff --git a/app/actions.ts b/app/actions.ts
new file mode 100644
index 0000000..af32f63
--- /dev/null
+++ b/app/actions.ts
@@ -0,0 +1,18 @@
+"use server";
+
+import { cookies } from "next/headers";
+
+export async function setPlanCookie(plan: string) {
+ //@ts-ignore
+ (await cookies()).set("plan", plan);
+}
+
+export async function getPlanCookie() {
+ const cookieStore = await cookies();
+ //@ts-ignore
+ const plan = await cookieStore.get("plan");
+
+ const out = plan ? plan.value : undefined;
+
+ return out;
+}
diff --git a/app/api/course/route.ts b/app/api/course/route.ts
deleted file mode 100644
index f041c7c..0000000
--- a/app/api/course/route.ts
+++ /dev/null
@@ -1,32 +0,0 @@
-// api/test.ts
-
-import { NextResponse, NextRequest } from "next/server";
-
-import prisma from "@/lib/prisma";
-
-export async function GET(request: NextRequest) {
- const courses = await prisma.course.findMany({
- include: {
- sectionAttributes: true,
- facultyMeet: {
- include: {
- meetingTimes: true,
- },
- },
- instructor: true,
- },
- orderBy: {
- courseTitle: "asc",
- },
- });
-
- return NextResponse.json(courses, { status: 200 });
-}
-
-// To handle a POST request to /api
-/*
-export async function POST(request: NextRequest) {
-
- return NextResponse.json(output, { status: 200 });
-}
-*/
diff --git a/app/api/createplan/route.ts b/app/api/createplan/route.ts
new file mode 100644
index 0000000..b8708f3
--- /dev/null
+++ b/app/api/createplan/route.ts
@@ -0,0 +1,78 @@
+// api/test.ts
+import { NextResponse, NextRequest } from "next/server";
+import prisma from "@/lib/prisma";
+import { getServerSession } from "next-auth/next";
+import { auth } from "@/lib/auth";
+
+export async function POST(request: NextRequest) {
+ const data = await request.json();
+ const session = await auth();
+
+ //@ts-ignore
+ const user = await prisma.user.findUnique({
+ where: {
+ //@ts-ignore
+ uuid: session?.user?.id,
+ },
+ });
+
+ const plan = await prisma.coursePlan.create({
+ data: {
+ //@ts-ignore
+ name: data.planName,
+ year: "F2024",
+ User: {
+ connect: {
+ id: user?.id,
+ },
+ },
+ },
+ });
+
+ return NextResponse.json(plan, { status: 200 });
+}
+
+export async function GET(request: NextRequest) {
+ const session = await auth();
+ const user = await prisma.user.findUnique({
+ where: {
+ //@ts-ignore
+ uuid: session?.user?.id,
+ },
+ });
+ const courses = await prisma.coursePlan.findMany({
+ where: {
+ User: {
+ //@ts-ignore
+ id: user.id,
+ },
+ },
+ include: {
+ courses: true,
+ },
+ });
+ //console.log(plans);
+ return NextResponse.json(courses, { status: 200 });
+}
+
+export async function DELETE(request: NextRequest) {
+ const data = await request.json();
+
+ //@ts-ignore
+ const plan = await prisma.coursePlan.delete({
+ where: {
+ //@ts-ignore
+ id: parseInt(data.planId),
+ },
+ });
+
+ return NextResponse.json(plan, { status: 200 });
+}
+
+// To handle a POST request to /api
+/*
+export async function POST(request: NextRequest) {
+
+ return NextResponse.json(output, { status: 200 });
+}
+*/
diff --git a/app/api/getcourseplans/route.ts b/app/api/getcourseplans/route.ts
new file mode 100644
index 0000000..663d62f
--- /dev/null
+++ b/app/api/getcourseplans/route.ts
@@ -0,0 +1,31 @@
+import { NextResponse, NextRequest } from "next/server";
+import prisma from "@/lib/prisma";
+import { auth } from "@/lib/auth";
+
+export async function GET(request: NextRequest) {
+ const session = await auth();
+ const user = await prisma.user.findUnique({
+ where: {
+ //@ts-ignore
+ uuid: session?.user?.id,
+ },
+ });
+ let courses: any;
+ if (user) {
+ courses = await prisma.coursePlan.findMany({
+ where: {
+ User: {
+ //@ts-ignore
+ id: user.id,
+ },
+ },
+ include: {
+ courses: true,
+ },
+ });
+ } else {
+ courses = null;
+ }
+ //console.log(plans);
+ return NextResponse.json(courses, { status: 200 });
+}
diff --git a/app/api/getplancourses/route.ts b/app/api/getplancourses/route.ts
new file mode 100644
index 0000000..684e5de
--- /dev/null
+++ b/app/api/getplancourses/route.ts
@@ -0,0 +1,27 @@
+// api/test.ts
+import { NextResponse, NextRequest } from "next/server";
+import prisma from "@/lib/prisma";
+import { getServerSession } from "next-auth/next";
+import { auth } from "@/lib/auth";
+import { getPlanCookie } from "@/app/actions";
+
+export async function GET(request: NextRequest) {
+ let planCookie: any = await getPlanCookie();
+ const session = await auth();
+
+ let courses = await prisma.coursePlan.findMany({
+ where: {
+ AND: {
+ User: {
+ //@ts-ignore
+ uuid: session?.user.id,
+ },
+ id: parseInt(planCookie),
+ },
+ },
+ include: {
+ courses: true,
+ },
+ });
+ return NextResponse.json(courses, { status: 200 });
+}
diff --git a/app/api/updatePlan/route.ts b/app/api/updatePlan/route.ts
new file mode 100644
index 0000000..4daa4c9
--- /dev/null
+++ b/app/api/updatePlan/route.ts
@@ -0,0 +1,77 @@
+// api/test.ts
+import { NextResponse, NextRequest } from "next/server";
+import prisma from "@/lib/prisma";
+import { getServerSession } from "next-auth/next";
+import { auth } from "@/lib/auth";
+import { getPlanCookie } from "@/app/actions";
+
+export async function POST(request: NextRequest) {
+ const course = await request.json();
+ const planId = await getPlanCookie();
+
+ if (planId) {
+ let plan: any;
+
+ if (planId) {
+ plan = await prisma.coursePlan.update({
+ where: {
+ id: parseInt(planId),
+ },
+ data: {
+ courses: {
+ connect: {
+ id: course.course.id,
+ },
+ },
+ },
+ });
+ }
+ }
+
+ return NextResponse.json(planId, { status: 200 });
+}
+
+export async function GET(request: NextRequest) {
+ const session = await auth();
+ const user = await prisma.user.findUnique({
+ where: {
+ //@ts-ignore
+ uuid: session?.user?.id,
+ },
+ });
+ const courses = await prisma.coursePlan.findMany({
+ where: {
+ User: {
+ //@ts-ignore
+ id: user.id,
+ },
+ },
+ include: {
+ courses: true,
+ },
+ });
+ console.log(courses);
+ return NextResponse.json(courses, { status: 200 });
+}
+
+export async function DELETE(request: NextRequest) {
+ const data = await request.json();
+
+ //@ts-ignore
+ const plan = await prisma.coursePlan.delete({
+ where: {
+ //@ts-ignore
+ id: parseInt(data.planId),
+ },
+ });
+
+ return NextResponse.json(plan, { status: 200 });
+}
+
+// To handle a POST request to /api
+/*
+export async function POST(request: NextRequest) {
+
+ return NextResponse.json(output, { status: 200 });
+}
+*/
diff --git a/app/api/user/route.ts b/app/api/user/route.ts
new file mode 100644
index 0000000..97f400a
--- /dev/null
+++ b/app/api/user/route.ts
@@ -0,0 +1,50 @@
+// api/test.ts
+import { NextResponse, NextRequest } from "next/server";
+import prisma from "@/lib/prisma";
+
+export async function POST(request: NextRequest) {
+ const data = await request.json();
+ //@ts-ignore
+ const session = data?.session;
+
+ const user = await prisma.user.upsert({
+ where: {
+ //@ts-ignore
+ uuid: session?.user?.id,
+ },
+ update: {
+ //@ts-ignore
+ uuid: session?.user?.id,
+ email: session?.user?.email,
+ name: session?.user?.name,
+ },
+ create: {
+ //@ts-ignore
+ uuid: session?.user?.id,
+ email: session?.user?.email,
+ name: session?.user?.name,
+ },
+ });
+
+ return NextResponse.json(user, { status: 200 });
+}
+
+export async function GET(request: NextRequest) {
+ /*
+ const session = data.session;
+ const user = await prisma.user.findUnique({
+ where: {
+ //@ts-ignore
+ id: session?.user?.id,
+ },
+ });
+*/
+ return NextResponse.json("HI", { status: 200 });
+}
+// To handle a POST request to /api
+/*
+export async function POST(request: NextRequest) {
+
+ return NextResponse.json(output, { status: 200 });
+}
+*/
diff --git a/app/blog/layout.tsx b/app/blog/layout.tsx
deleted file mode 100644
index 911d0bc..0000000
--- a/app/blog/layout.tsx
+++ /dev/null
@@ -1,13 +0,0 @@
-export default function BlogLayout({
- children,
-}: {
- children: React.ReactNode;
-}) {
- return (
-