Skip to content

Commit

Permalink
DB:Allow scrapper to add all atributes
Browse files Browse the repository at this point in the history
  • Loading branch information
DCRepublic committed Nov 8, 2024
1 parent 54cc6c1 commit de85238
Show file tree
Hide file tree
Showing 7 changed files with 208 additions and 40 deletions.
17 changes: 16 additions & 1 deletion app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { auth } from "../lib/auth";
import prisma from "../lib/prisma";
import { Course, CoursePlan } from "@prisma/client";
import { getPlanCookie } from "../app/actions";
import { dA } from "@fullcalendar/core/internal-common";

async function getCourses() {
const courses = await prisma.course.findMany();
Expand Down Expand Up @@ -47,6 +48,19 @@ async function getUniqueStartEndTimes() {
return times;
}

async function getUniquCodes() {
const codes = await prisma.sectionAttribute.findMany();
let daCodes: any = [];

for (let i = 0; i < codes.length; i++) {
if (!daCodes.includes(codes[i].code)) {
daCodes.push(codes[i].code);
}
}
console.log(daCodes);
return daCodes;
}

export default async function Page(props: {
searchParams?: Promise<{
query?: string;
Expand Down Expand Up @@ -88,14 +102,15 @@ export default async function Page(props: {
async function Home(props: any) {
const terms = await getCourses();
const uniqueTimes = await getUniqueStartEndTimes();
const codes = await getUniquCodes();

return (
<>
<div className="grid grid-cols-3 p-4 -mt-10 ">
<div className="col-span-2 col-start-1">
<div className="grid grid-rows-subgrid grid-cols-1 gap-5 ">
<div className="row-start-1">
<Search terms={terms} times={uniqueTimes} />
<Search terms={terms} times={uniqueTimes} codes={codes} />
</div>
<div className="row-start-2 h-[62vh] overflow-y-scroll overflow-x-clip">
{props.fullCourseList}
Expand Down
106 changes: 71 additions & 35 deletions components/FullCourseList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ async function getCourses(
) {
//let DOTW: Array<String> = dotw.split(",");

//query = query.trim().replace(/^[a-zA-Z0-9:]+$/g, "");

let startTime = stime.toString().split(",").filter(Number);

console.log(startTime);
return await prisma.course.findMany({
relationLoadStrategy: "join", // or 'query'
include: {
Expand All @@ -36,46 +37,81 @@ async function getCourses(
},
],
where: {
...(term
...(query
? {
year: term,
}
: {}),
//year: term,

OR: [
{ courseTitle: { search: query.trim().split(" ").join(" & ") } },
{ subject: { search: query.trim().split(" ").join(" & ") } },
{ courseNumber: { search: query.trim().split(" ").join(" & ") } },
],
...(term
? {
year: term,
}
: {}),
//year: term,

...(startTime.length > 0
? {
facultyMeet: {
meetingTimes: {
beginTime: {
in: startTime,
OR: [
{
courseTitle: {
search: query.trim().split(" ").join(" | "),
mode: "insensitive",
},
},
},
}
: {}),

...(dotw.length > 0
? {
facultyMeet: {
meetingTimes: {
is: {
monday: dotw.includes("monday") ? true : Prisma.skip,
tuesday: dotw.includes("tuesday") ? true : Prisma.skip,
wednesday: dotw.includes("wednesday") ? true : Prisma.skip,
thursday: dotw.includes("thursday") ? true : Prisma.skip,
friday: dotw.includes("friday") ? true : Prisma.skip,
saturday: dotw.includes("saturday") ? true : Prisma.skip,
sunday: dotw.includes("sunday") ? true : Prisma.skip,
{
sectionAttributes: {
some: {
code: {
search: query.trim().split(" ").join(" | "),
mode: "insensitive",
},
},
},
},
{
subject: {
search: query.trim().split(" ").join(" | "),
mode: "insensitive",
},
},
},
{
courseNumber: {
search: query.trim().split(" ").join(" | "),
mode: "insensitive",
},
},
],

...(startTime.length > 0
? {
facultyMeet: {
meetingTimes: {
beginTime: {
in: startTime,
},
},
},
}
: {}),

...(dotw.length > 0
? {
facultyMeet: {
meetingTimes: {
is: {
monday: dotw.includes("monday") ? true : Prisma.skip,
tuesday: dotw.includes("tuesday") ? true : Prisma.skip,
wednesday: dotw.includes("wednesday")
? true
: Prisma.skip,
thursday: dotw.includes("thursday")
? true
: Prisma.skip,
friday: dotw.includes("friday") ? true : Prisma.skip,
saturday: dotw.includes("saturday")
? true
: Prisma.skip,
sunday: dotw.includes("sunday") ? true : Prisma.skip,
},
},
},
}
: {}),
}
: {}),
},
Expand Down
60 changes: 57 additions & 3 deletions components/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,42 @@ import { useDebouncedCallback } from "use-debounce";
import { Select, SelectItem } from "@nextui-org/react";
import { useEffect, useState } from "react";
import moment from "moment";
import { Autocomplete, TextField, Chip } from "@mui/material";

export default function Search(props: any) {
let router = useRouter();
const searchParams = useSearchParams();
const [selectedTerm, setSelectedTerm]: any = useState([]);
const [selectedTerm, setSelectedTerm]: any = useState(["S2025"]);
const [selectedDOTW, setSelectedDOTW]: any = useState([]);
const [selectedCodes, setSelectedCodes]: any = useState([]);

const [selectedStartTime, setSelectedStartTime]: any = useState([]);
const [search, setSearch]: any = useState();
const pathname = usePathname();
const { replace } = useRouter();

const replaceText = (text: any) => {
setSearch(text?.replace(/\w+:/, <Chip>{text}</Chip>));
console.log(search);
};

const handleSearch = useDebouncedCallback((term: string) => {
//replaceText(term);
//setSearch(term);
//const term_regex = new RegExp(\w, "g");
let filtered_term = term.replace(/[^a-zA-Z0-9 ]+/gi, "");
let include_colons = term.replace(/[^a-zA-Z0-9: ]+/gi, "");
let term_list = include_colons.split(" ");

for (var i = 0; i < term_list.length; i++) {
if (/\w+:/.test(term_list[i])) {
console.log(term_list[i]);
}
}
const params = new URLSearchParams(searchParams);
if (term) {
params.set("query", term);
decodeURIComponent;
params.set("query", filtered_term);
} else {
params.delete("query");
}
Expand All @@ -28,6 +51,7 @@ export default function Search(props: any) {
const handleSelectionChange = (e: any) => {
setSelectedTerm([e.target.value]);
const params = new URLSearchParams(searchParams);

if (e.target.value) {
params.set("term", e.target.value);
} else {
Expand All @@ -40,6 +64,18 @@ export default function Search(props: any) {
//setPlanCookie(e.target.value);
};

const handleCodeChange = (e: any) => {
setSelectedCodes([e.target.value]);
const params = new URLSearchParams(searchParams);

if (e.target.value) {
params.set("codes", e.target.value);
} else {
params.delete("codes");
}
replace(`${pathname}?${params.toString()}`);
};

useEffect(() => {
// Update the document title using the browser API
setSelectedTerm(searchParams.get("term")?.toString().split(","));
Expand Down Expand Up @@ -102,6 +138,7 @@ export default function Search(props: any) {
label="Search"
labelPlacement="inside"
variant="bordered"
value={search}
onClear={() => {
handleSearch("");
}}
Expand Down Expand Up @@ -163,14 +200,31 @@ export default function Search(props: any) {
var time = startTime.slice(0, 2) + ":" + startTime.slice(2);
var daTime = moment(time, "HH:mm").format("hh:mm A");

console.log(daTime);
return (
<SelectItem key={startTime} value={startTime}>
{daTime}
</SelectItem>
);
})}
</Select>
{/*
<Select
label="Division"
className="max-w-xs"
selectedKeys={selectedCodes}
selectionMode={"multiple"}
//defaultSelectedKeys={searchParams.get("dotw")?.toString()}
onSelectionChange={handleCodeChange}
>
{props.codes.map((code: any) => {
return (
<SelectItem key={code} value={code}>
{code.toUpperCase()}
</SelectItem>
);
})}
</Select>
*/}
</div>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
Warnings:
- You are about to drop the column `courseId` on the `sectionAttribute` table. All the data in the column will be lost.
*/
-- DropForeignKey
ALTER TABLE "sectionAttribute" DROP CONSTRAINT "sectionAttribute_courseId_fkey";

-- AlterTable
ALTER TABLE "sectionAttribute" DROP COLUMN "courseId";

-- CreateTable
CREATE TABLE "_CourseTosectionAttribute" (
"A" INTEGER NOT NULL,
"B" INTEGER NOT NULL
);

-- CreateIndex
CREATE UNIQUE INDEX "_CourseTosectionAttribute_AB_unique" ON "_CourseTosectionAttribute"("A", "B");

-- CreateIndex
CREATE INDEX "_CourseTosectionAttribute_B_index" ON "_CourseTosectionAttribute"("B");

-- AddForeignKey
ALTER TABLE "_CourseTosectionAttribute" ADD CONSTRAINT "_CourseTosectionAttribute_A_fkey" FOREIGN KEY ("A") REFERENCES "Course"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "_CourseTosectionAttribute" ADD CONSTRAINT "_CourseTosectionAttribute_B_fkey" FOREIGN KEY ("B") REFERENCES "sectionAttribute"("id") ON DELETE CASCADE ON UPDATE CASCADE;
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
Warnings:
- You are about to drop the `_CourseTosectionAttribute` table. If the table is not empty, all the data it contains will be lost.
*/
-- DropForeignKey
ALTER TABLE "_CourseTosectionAttribute" DROP CONSTRAINT "_CourseTosectionAttribute_A_fkey";

-- DropForeignKey
ALTER TABLE "_CourseTosectionAttribute" DROP CONSTRAINT "_CourseTosectionAttribute_B_fkey";

-- AlterTable
ALTER TABLE "sectionAttribute" ADD COLUMN "courseId" INTEGER;

-- DropTable
DROP TABLE "_CourseTosectionAttribute";

-- AddForeignKey
ALTER TABLE "sectionAttribute" ADD CONSTRAINT "sectionAttribute_courseId_fkey" FOREIGN KEY ("courseId") REFERENCES "Course"("id") ON DELETE SET NULL ON UPDATE CASCADE;
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
Warnings:
- Made the column `courseId` on table `sectionAttribute` required. This step will fail if there are existing NULL values in that column.
*/
-- DropForeignKey
ALTER TABLE "sectionAttribute" DROP CONSTRAINT "sectionAttribute_courseId_fkey";

-- AlterTable
ALTER TABLE "sectionAttribute" ALTER COLUMN "courseId" SET NOT NULL;

-- AddForeignKey
ALTER TABLE "sectionAttribute" ADD CONSTRAINT "sectionAttribute_courseId_fkey" FOREIGN KEY ("courseId") REFERENCES "Course"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
2 changes: 1 addition & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ model sectionAttribute {
code String
description String
courseReferenceNumber String @unique
courseId Int?
courseId Int
Course Course? @relation(fields: [courseId], references: [id])
year String
}

0 comments on commit de85238

Please sign in to comment.