Skip to content

Commit

Permalink
DB Migration Stage:1 (new rating system)
Browse files Browse the repository at this point in the history
  • Loading branch information
DCRepublic committed Nov 26, 2024
1 parent ac4790d commit 5d6a339
Show file tree
Hide file tree
Showing 13 changed files with 392 additions and 169 deletions.
34 changes: 34 additions & 0 deletions app/actions/getProfs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"use server";

import { cookies } from "next/headers";
import prisma from "../../lib/prisma";
import { Prisma } from "@prisma/client";
import { auth } from "@/lib/auth";
import { getPlanCookie } from "./actions";
import { Faculty } from "@prisma/client";

export async function getProfs() {
let profs: Faculty[] = [];
let AllProfs = await prisma.faculty.findMany({
orderBy: {
displayName: "desc",
},
});

console.log(AllProfs.length);

return AllProfs;
}

export async function getYears() {
let years: any = [];
let AllCourses = await prisma.course.findMany();

for (let i = 0; i < AllCourses.length; i++) {
if (!years.includes(AllCourses[i].year)) {
years.push(AllCourses[i].year);
}
}

return years;
}
22 changes: 21 additions & 1 deletion app/api/getProfClasses/route.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { clsx } from "clsx";
import { Faculty } from "./../../../node_modules/.prisma/client/index.d";
import { NextResponse, NextRequest } from "next/server";
import { Course } from "@prisma/client";

import prisma from "../../../lib/prisma";

export async function GET(request: NextRequest) {
const { searchParams } = await new URL(request.url);
const profID = parseInt((await searchParams.get("prof")) || "1");

const outputCourses: Course[] = [];

const courses = await prisma.course.findMany({
include: {
instructor: true,
Expand All @@ -18,5 +22,21 @@ export async function GET(request: NextRequest) {
},
});

return NextResponse.json(courses, { status: 200 });
for (let i = 0; i < courses.length; i++) {
let push = true;
for (const course of outputCourses) {
if (
course.courseNumber == courses[i].courseNumber &&
course.subject == courses[i].subject &&
!course.courseTitle.toLowerCase().includes("lab")
) {
push = false;
}
}
if (push) {
outputCourses.push(courses[i]);
}
}

return NextResponse.json(outputCourses, { status: 200 });
}
122 changes: 74 additions & 48 deletions app/api/submitReview/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,69 +15,95 @@ export async function POST(request: NextRequest) {
uuid: session?.user?.id,
},
});
if (user?.id) {
const theClass = await prisma.course.findUnique({
where: {
id: parseInt(data.courseID),
},
});
const profs = await prisma.faculty.findUnique({
select: {
displayName: true,
bannerId: true,
uid: true,
},

where: {
id: parseInt(data.facultyID),
},
});
if (user?.id && theClass && profs) {
const newReview = await prisma.rating.create({
data: {
userId: user?.id,
courseID: courseID,
facultyID: facultyID,
overallRating: data.overallRating,
difficulty: data.difficulty,
takeAgain: data.takeAgain,
forCredit: data.forCredit,
grade: data.grade,
review: data.review,
courseSubject: theClass.subject,
courseNumber: theClass.courseNumber,
courseName: theClass.courseTitle,
termTaken: data.termTaken,
yearTaken: parseInt(data.yearTaken),
profDisplayName: profs.displayName,
profBannerId: profs.bannerId,
profUid: profs.uid,
},
});
}
const profs = await prisma.faculty.findUnique({
select: { Rating: { select: { overallRating: true } } },
//include: { Rating: true },
where: { id: facultyID },
});

let count: any = { one: 0, two: 0, three: 0, four: 0, five: 0 };

if (profs) {
if (profs?.Rating?.length > 0) {
for (const rating of profs.Rating) {
if (rating.overallRating == 1) {
count.one += 1;
}
if (rating.overallRating == 2) {
count.two += 1;
}
if (rating.overallRating == 3) {
count.three += 1;
}
if (rating.overallRating == 4) {
count.four += 1;
}
if (rating.overallRating == 5) {
count.five += 1;
}
}

let avg = parseInt(
(
(count.one * 1 +
count.two * 2 +
count.three * 3 +
count.four * 4 +
count.five * 5) /
profs.Rating.length
).toFixed(1)
);

const newAvg = await prisma.faculty.update({
data: {
avgRating: avg,
},
if (newReview) {
const ratings = await prisma.rating.findMany({
where: {
id: facultyID,
profUid: {
equals: profs.uid,
},
},
});
return NextResponse.json(newAvg, { status: 200 });
let count: any = { one: 0, two: 0, three: 0, four: 0, five: 0 };

if (ratings) {
if (ratings?.length > 0) {
for (const rating of ratings) {
if (rating.overallRating == 1) {
count.one += 1;
}
if (rating.overallRating == 2) {
count.two += 1;
}
if (rating.overallRating == 3) {
count.three += 1;
}
if (rating.overallRating == 4) {
count.four += 1;
}
if (rating.overallRating == 5) {
count.five += 1;
}
}

let avg = parseInt(
(
(count.one * 1 +
count.two * 2 +
count.three * 3 +
count.four * 4 +
count.five * 5) /
ratings.length
).toFixed(1)
);

const newAvg = await prisma.faculty.update({
data: {
avgRating: avg,
},
where: {
id: facultyID,
},
});
return NextResponse.json(newAvg, { status: 200 });
}
}
}
}

Expand Down
Loading

0 comments on commit 5d6a339

Please sign in to comment.