Skip to content

Commit 37dab05

Browse files
dawangkgithub-actions[bot]
authored andcommitted
Auto-formatted the code using Prettier
1 parent f8cf8d9 commit 37dab05

File tree

5 files changed

+308
-291
lines changed

5 files changed

+308
-291
lines changed

course-matrix/backend/src/controllers/generatorController.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
import exp from "constants";
22
import { Request, Response } from "express";
33

4-
import {Offering, OfferingList, GroupedOfferingList} from "../types/generatorTypes"
5-
import {getMaxDays, groupOfferings, getValidOfferings, categorizeValidOfferings, trim} from "../utils/generatorHelpers"
4+
import {
5+
Offering,
6+
OfferingList,
7+
GroupedOfferingList,
8+
} from "../types/generatorTypes";
9+
import {
10+
getMaxDays,
11+
groupOfferings,
12+
getValidOfferings,
13+
categorizeValidOfferings,
14+
trim,
15+
} from "../utils/generatorHelpers";
616
import getOfferings from "../services/getOfferings";
717
import { getValidSchedules } from "../services/getValidSchedules";
818

@@ -13,7 +23,7 @@ export default {
1323
generateTimetable: asyncHandler(async (req: Request, res: Response) => {
1424
try {
1525
// Extract event details and course information from the request
16-
const {semester, courses, restrictions } = req.body;
26+
const { semester, courses, restrictions } = req.body;
1727
const courseOfferingsList: OfferingList[] = [];
1828
const validCourseOfferingsList: GroupedOfferingList[] = [];
1929
const maxdays = await getMaxDays(restrictions);
@@ -62,12 +72,10 @@ export default {
6272
return res.status(404).json({ error: "No valid schedules found." });
6373
}
6474
// Return the valid schedules
65-
return res
66-
.status(200)
67-
.json({
68-
amount: validSchedules.length,
69-
schedules: trim(validSchedules),
70-
});
75+
return res.status(200).json({
76+
amount: validSchedules.length,
77+
schedules: trim(validSchedules),
78+
});
7179
} catch (error) {
7280
// Catch any error and return the error message
7381
const errorMessage =
Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import { supabase } from "../db/setupDb";
22

33
// Function to fetch offerings from the database for a given course and semester
4-
export default async function getOfferings(course_id: number, semester: string) {
5-
let { data: offeringData, error: offeringError } = await supabase
6-
.schema("course")
7-
.from("offerings")
8-
.select(
9-
`
4+
export default async function getOfferings(
5+
course_id: number,
6+
semester: string,
7+
) {
8+
let { data: offeringData, error: offeringError } = await supabase
9+
.schema("course")
10+
.from("offerings")
11+
.select(
12+
`
1013
id,
1114
course_id,
1215
meeting_section,
@@ -23,9 +26,9 @@ export default async function getOfferings(course_id: number, semester: string)
2326
notes,
2427
code
2528
`,
26-
)
27-
.eq("course_id", course_id)
28-
.eq("offering", semester);
29-
30-
return offeringData;
31-
}
29+
)
30+
.eq("course_id", course_id)
31+
.eq("offering", semester);
32+
33+
return offeringData;
34+
}
Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,49 @@
1-
import {Offering, CategorizedOfferingList} from "../types/generatorTypes"
2-
import {getFrequencyTable, canInsertList} from "../utils/generatorHelpers"
1+
import { Offering, CategorizedOfferingList } from "../types/generatorTypes";
2+
import { getFrequencyTable, canInsertList } from "../utils/generatorHelpers";
33
// Function to generate all valid schedules based on offerings and restrictions
44

55
export async function getValidSchedules(
6-
validSchedules: Offering[][],
7-
courseOfferingsList: CategorizedOfferingList[],
8-
curList: Offering[],
9-
cur: number,
10-
len: number,
11-
maxdays: number,
12-
) {
13-
// Base case: if all courses have been considered
14-
if (cur == len) {
15-
const freq: Map<string, number> = getFrequencyTable(curList);
16-
17-
// If the number of unique days is within the allowed limit, add the current
18-
// schedule to the list
19-
if (freq.size <= maxdays) {
20-
validSchedules.push([...curList]); // Push a copy of the current list
21-
}
22-
return;
6+
validSchedules: Offering[][],
7+
courseOfferingsList: CategorizedOfferingList[],
8+
curList: Offering[],
9+
cur: number,
10+
len: number,
11+
maxdays: number,
12+
) {
13+
// Base case: if all courses have been considered
14+
if (cur == len) {
15+
const freq: Map<string, number> = getFrequencyTable(curList);
16+
17+
// If the number of unique days is within the allowed limit, add the current
18+
// schedule to the list
19+
if (freq.size <= maxdays) {
20+
validSchedules.push([...curList]); // Push a copy of the current list
2321
}
24-
25-
const offeringsForCourse = courseOfferingsList[cur];
26-
27-
// Recursively attempt to add offerings for the current course
28-
for (const [groupKey, offerings] of Object.entries(
29-
offeringsForCourse.offerings,
30-
)) {
31-
if (await canInsertList(offerings, curList)) {
32-
const count = offerings.length;
33-
curList.push(...offerings); // Add offering to the current list
34-
35-
// Recursively generate schedules for the next course
36-
await getValidSchedules(
37-
validSchedules,
38-
courseOfferingsList,
39-
curList,
40-
cur + 1,
41-
len,
42-
maxdays,
43-
);
44-
45-
// Backtrack: remove the last offering if no valid schedule was found
46-
for (let i = 0; i < count; i++) curList.pop();
47-
}
22+
return;
23+
}
24+
25+
const offeringsForCourse = courseOfferingsList[cur];
26+
27+
// Recursively attempt to add offerings for the current course
28+
for (const [groupKey, offerings] of Object.entries(
29+
offeringsForCourse.offerings,
30+
)) {
31+
if (await canInsertList(offerings, curList)) {
32+
const count = offerings.length;
33+
curList.push(...offerings); // Add offering to the current list
34+
35+
// Recursively generate schedules for the next course
36+
await getValidSchedules(
37+
validSchedules,
38+
courseOfferingsList,
39+
curList,
40+
cur + 1,
41+
len,
42+
maxdays,
43+
);
44+
45+
// Backtrack: remove the last offering if no valid schedule was found
46+
for (let i = 0; i < count; i++) curList.pop();
4847
}
49-
}
48+
}
49+
}
Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,57 @@
1-
21
// Interface to define the structure of an Offering
32
export interface Offering {
4-
id: number;
5-
course_id: number;
6-
meeting_section: string;
7-
offering: string;
8-
day: string;
9-
start: string;
10-
end: string;
11-
location: string;
12-
current: number;
13-
max: number;
14-
is_waitlisted: boolean;
15-
delivery_mode: string;
16-
instructor: string;
17-
notes: string;
18-
code: string;
3+
id: number;
4+
course_id: number;
5+
meeting_section: string;
6+
offering: string;
7+
day: string;
8+
start: string;
9+
end: string;
10+
location: string;
11+
current: number;
12+
max: number;
13+
is_waitlisted: boolean;
14+
delivery_mode: string;
15+
instructor: string;
16+
notes: string;
17+
code: string;
1918
}
2019

2120
// Enum to define different types of restrictions for offerings
2221
export enum RestrictionType {
23-
RestrictBefore = "Restrict Before",
24-
RestrictAfter = "Restrict After",
25-
RestrictBetween = "Restrict Between",
26-
RestrictDay = "Restrict Day",
27-
RestrictDaysOff = "Days Off",
22+
RestrictBefore = "Restrict Before",
23+
RestrictAfter = "Restrict After",
24+
RestrictBetween = "Restrict Between",
25+
RestrictDay = "Restrict Day",
26+
RestrictDaysOff = "Days Off",
2827
}
29-
28+
3029
// Interface for the restriction object
3130
export interface Restriction {
32-
type: RestrictionType;
33-
days: string[];
34-
startTime: string;
35-
endTime: string;
36-
disabled: boolean;
37-
numDays: number;
31+
type: RestrictionType;
32+
days: string[];
33+
startTime: string;
34+
endTime: string;
35+
disabled: boolean;
36+
numDays: number;
3837
}
3938

4039
// Interface for organizing offerings with the same meeting_section together
4140
export interface GroupedOfferingList {
42-
course_id: number;
43-
groups: Record<string, Offering[]>;
41+
course_id: number;
42+
groups: Record<string, Offering[]>;
4443
}
45-
44+
4645
// Interface for organizing offerings by course ID
4746
export interface OfferingList {
48-
course_id: number;
49-
offerings: Offering[];
47+
course_id: number;
48+
offerings: Offering[];
5049
}
51-
50+
5251
// Interface for organizing offerings by course ID and the category of the
5352
// course (LEC, TUT, PRA)
5453
export interface CategorizedOfferingList {
55-
course_id: number;
56-
category: "LEC" | "TUT" | "PRA";
57-
offerings: Record<string, Offering[]>;
58-
}
54+
course_id: number;
55+
category: "LEC" | "TUT" | "PRA";
56+
offerings: Record<string, Offering[]>;
57+
}

0 commit comments

Comments
 (0)