Skip to content

Commit

Permalink
CoursePlan: add plan support
Browse files Browse the repository at this point in the history
  • Loading branch information
DCRepublic committed Oct 31, 2024
1 parent 9767948 commit f3735b4
Show file tree
Hide file tree
Showing 28 changed files with 865 additions and 215 deletions.
18 changes: 18 additions & 0 deletions app/actions.ts
Original file line number Diff line number Diff line change
@@ -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;
}
32 changes: 0 additions & 32 deletions app/api/course/route.ts

This file was deleted.

78 changes: 78 additions & 0 deletions app/api/createplan/route.ts
Original file line number Diff line number Diff line change
@@ -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 });
}
*/
31 changes: 31 additions & 0 deletions app/api/getcourseplans/route.ts
Original file line number Diff line number Diff line change
@@ -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 });
}
27 changes: 27 additions & 0 deletions app/api/getplancourses/route.ts
Original file line number Diff line number Diff line change
@@ -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 });
}
77 changes: 77 additions & 0 deletions app/api/updatePlan/route.ts
Original file line number Diff line number Diff line change
@@ -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 });
}
*/
50 changes: 50 additions & 0 deletions app/api/user/route.ts
Original file line number Diff line number Diff line change
@@ -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 });
}
*/
13 changes: 0 additions & 13 deletions app/blog/layout.tsx

This file was deleted.

9 changes: 0 additions & 9 deletions app/blog/page.tsx

This file was deleted.

Loading

0 comments on commit f3735b4

Please sign in to comment.