Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions backend/src/main/java/edu/gcc/hallmonitor/Course.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.fasterxml.jackson.annotation.JsonProperty;

public record Course(
int id,
String name,
@JsonProperty("faculty")
List<String> professor,
Expand Down
1 change: 1 addition & 0 deletions backend/src/main/java/edu/gcc/hallmonitor/Schedule.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ public List<Course> getCourses() {
}

Course copyCourse = new Course(
c.id(),
c.name(),
new ArrayList<>(c.professor()),
c.department(),
Expand Down
1 change: 1 addition & 0 deletions backend/src/main/java/edu/gcc/hallmonitor/Search.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ private static void loadCourses() {
List<CourseTime> courseTimes = mapper.readerForListOf(CourseTime.class).readValue(time_json);

Course c = new Course(
rs.getInt("id"),
rs.getString("name"),
facultyList,
rs.getString("subject"),
Expand Down
21 changes: 14 additions & 7 deletions backend/src/main/java/edu/gcc/hallmonitor/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,15 @@ public class User {
private byte[] passwordHash;
private int id;
private int gradYear;
private Connection connection;
private static Connection CONNECTION;

static {
try {
CONNECTION = Database.getConnection();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}

/**
* Basic constructor that sets the username and password hash. NOTE: YOU SHOULD USE THE login() or signUp() METHOD
Expand All @@ -39,7 +47,6 @@ public User(String username, String password) throws IllegalArgumentException, S
throw new IllegalArgumentException("sha256 not found");
} // won't fail since sha256 is hardcoded

connection = Database.getConnection();
}

public String getUsername() {
Expand Down Expand Up @@ -87,7 +94,7 @@ public static User signup(String username, String password) throws SQLException
*/
public boolean isUser() throws SQLException {
// Get all the users that match the current user (should be at max 1)
PreparedStatement prepStatement = connection.prepareStatement(
PreparedStatement prepStatement = CONNECTION.prepareStatement(
"SELECT * FROM public.\"users\"" +
"WHERE username = ? AND password_hash = ?"
);
Expand All @@ -100,7 +107,7 @@ public boolean isUser() throws SQLException {
}

public int getIdFromDatabase() throws SQLException {
PreparedStatement prepStatement = connection.prepareStatement(
PreparedStatement prepStatement = CONNECTION.prepareStatement(
"SELECT id FROM public.\"users\"" +
"WHERE username = ? AND password_hash = ?"
);
Expand All @@ -118,7 +125,7 @@ public int getIdFromDatabase() throws SQLException {
* @throws SQLException if the connection fails
*/
public boolean isUsernameTaken() throws SQLException {
PreparedStatement prepStatement = connection.prepareStatement(
PreparedStatement prepStatement = CONNECTION.prepareStatement(
"SELECT * FROM public.\"users\"" +
"WHERE username = ?"
);
Expand All @@ -140,7 +147,7 @@ public boolean addUser() throws SQLException {
return false;
}

PreparedStatement prepStatement = connection.prepareStatement(
PreparedStatement prepStatement = CONNECTION.prepareStatement(
"INSERT INTO public.\"users\" (username, password_hash, grad_year)" +
"VALUES (?, ?, ?)"
);
Expand All @@ -162,7 +169,7 @@ public boolean deleteUser() throws SQLException {
if (!isUser()) {
return false;
}
PreparedStatement prepStatement = connection.prepareStatement(
PreparedStatement prepStatement = CONNECTION.prepareStatement(
"DELETE FROM public.\"users\" WHERE username = ? AND password_hash = ?"
);
prepStatement.setString(1, username);
Expand Down
1 change: 1 addition & 0 deletions backend/src/test/java/edu/gcc/hallmonitor/DaysTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public void testMatchSingleDay() {

public Course emptyOnDays(List<String> days) {
return new Course(
1,
"",
List.of(""),
"",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public void testDepartmentFilter() {

private static Course emptyWithDepartment(String department) {
return new Course(
1,
"",
List.of(""),
department,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public void testNumCreditsFilter() {

private static Course emptyWithCredits(int credits) {
return new Course(
1,
"",
List.of(""),
"",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public void testProfNameFilter() {

private static Course emptyWithProf(String prof) {
return new Course(
1,
"",
List.of(prof),
"",
Expand Down
35 changes: 19 additions & 16 deletions backend/src/test/java/edu/gcc/hallmonitor/ScheduleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ void assertSchedulesEqual(Schedule expected, Schedule actual) {
Course expectedCourse = expectedCourses.get(i);
Course actualCourse = actualCourses.get(i);

assertEquals(expectedCourse.id(), actualCourse.id());
assertEquals(expectedCourse.name(), actualCourse.name());
assertEquals(expectedCourse.professor().size(), actualCourse.professor().size());
for (int j = 0; j < actualCourse.professor().size(); j++) {
Expand All @@ -44,6 +45,7 @@ void assertSchedulesEqual(Schedule expected, Schedule actual) {
void loadSaveSchedule() {
Schedule expectedSchedule = new Schedule(List.of(
new Course(
1,
"COMP PROGRAMMING I",
List.of("Wolfe, Britton D."),
"COMP",
Expand Down Expand Up @@ -75,6 +77,7 @@ void loadSaveSchedule() {
32
),
new Course(
2,
"INTRO TO COMPUTER SCIENCE",
List.of("Dickinson, Brian C"),
"COMP",
Expand Down Expand Up @@ -121,14 +124,14 @@ void loadSaveSchedule() {
void checkForOverlap_noOverlap() {
Schedule schedule = new Schedule(List.of(
new Course(
"Course1", List.of("Prof"), "COMP", 101, 'A', "Room", 3, "2023_Fall",
1, "Course1", List.of("Prof"), "COMP", 101, 'A', "Room", 3, "2023_Fall",
List.of(new CourseTime("M", LocalTime.of(10, 0), LocalTime.of(11, 0))),
false, false, 0, 30
)
));

Course newCourse = new Course(
"Course2", List.of("Prof"), "COMP", 102, 'A', "Room", 3, "2023_Fall",
2, "Course2", List.of("Prof"), "COMP", 102, 'A', "Room", 3, "2023_Fall",
List.of(new CourseTime("M", LocalTime.of(11, 0), LocalTime.of(12, 0))),
false, false, 0, 30
);
Expand All @@ -140,14 +143,14 @@ void checkForOverlap_noOverlap() {
void checkForOverlap_overlapSameDay() {
Schedule schedule = new Schedule(List.of(
new Course(
"Course1", List.of("Prof"), "COMP", 101, 'A', "Room", 3, "2023_Fall",
1,"Course1", List.of("Prof"), "COMP", 101, 'A', "Room", 3, "2023_Fall",
List.of(new CourseTime("M", LocalTime.of(10, 0), LocalTime.of(11, 0))),
false, false, 0, 30
)
));

Course newCourse = new Course(
"Course2", List.of("Prof"), "COMP", 102, 'A', "Room", 3, "2023_Fall",
2, "Course2", List.of("Prof"), "COMP", 102, 'A', "Room", 3, "2023_Fall",
List.of(new CourseTime("M", LocalTime.of(10, 30), LocalTime.of(11, 30))),
false, false, 0, 30
);
Expand All @@ -158,14 +161,14 @@ void checkForOverlap_overlapSameDay() {
void checkForOverlap_differentDays() {
Schedule schedule = new Schedule(List.of(
new Course(
"Course1", List.of("Prof"), "COMP", 101, 'A', "Room", 3, "2023_Fall",
1, "Course1", List.of("Prof"), "COMP", 101, 'A', "Room", 3, "2023_Fall",
List.of(new CourseTime("M", LocalTime.of(10,0), LocalTime.of(11,0))),
false,false,0,30
)
));

Course newCourse = new Course(
"Course2", List.of("Prof"), "COMP", 102, 'A', "Room", 3, "2023_Fall",
2, "Course2", List.of("Prof"), "COMP", 102, 'A', "Room", 3, "2023_Fall",
List.of(new CourseTime("T", LocalTime.of(10,30), LocalTime.of(11,30))),
false,false,0,30
);
Expand All @@ -176,14 +179,14 @@ void checkForOverlap_differentDays() {
void checkForOverlap_touchingTimesNotOverlap() {
Schedule schedule = new Schedule(List.of(
new Course(
"Course1", List.of("Prof"), "COMP", 101, 'A', "Room", 3, "2023_Fall",
1, "Course1", List.of("Prof"), "COMP", 101, 'A', "Room", 3, "2023_Fall",
List.of(new CourseTime("M", LocalTime.of(10,0), LocalTime.of(11,0))),
false,false,0,30
)
));

Course newCourse = new Course(
"Course2", List.of("Prof"), "COMP", 102, 'A', "Room", 3, "2023_Fall",
2, "Course2", List.of("Prof"), "COMP", 102, 'A', "Room", 3, "2023_Fall",
List.of(new CourseTime("M", LocalTime.of(11,0), LocalTime.of(12,0))),
false,false,0,30
);
Expand All @@ -194,15 +197,15 @@ void checkForOverlap_touchingTimesNotOverlap() {
void checkForOverlap_invalidTimeIgnored() {
Schedule schedule = new Schedule(List.of(
new Course(
"Course1", List.of("Prof"), "COMP", 101, 'A', "Room", 3, "2023_Fall",
1, "Course1", List.of("Prof"), "COMP", 101, 'A', "Room", 3, "2023_Fall",
List.of(new CourseTime("M", LocalTime.of(10,0), LocalTime.of(11,0))),
false,false,0,30
)
));

// end before start → ignored
Course newCourse = new Course(
"Course2", List.of("Prof"), "COMP", 102, 'A', "Room", 3, "2023_Fall",
2, "Course2", List.of("Prof"), "COMP", 102, 'A', "Room", 3, "2023_Fall",
List.of(new CourseTime("M", LocalTime.of(12,0), LocalTime.of(11,0))),
false,false,0,30
);
Expand All @@ -215,13 +218,13 @@ void checkForOverlap_invalidTimeIgnored() {
void hasDifferentSection_trueWhenDifferentSection() {
Schedule schedule = new Schedule(List.of(
new Course(
"Course1", List.of("Prof"), "COMP", 101, 'A', "Room", 3, "2023_Fall",
1, "Course1", List.of("Prof"), "COMP", 101, 'A', "Room", 3, "2023_Fall",
List.of(), false,false,0,30
)
));

Course newCourse = new Course(
"Course1", List.of("Prof"), "COMP", 101, 'B', "Room", 3, "2023_Fall",
1, "Course1", List.of("Prof"), "COMP", 101, 'B', "Room", 3, "2023_Fall",
List.of(), false,false,0,30
);

Expand All @@ -232,13 +235,13 @@ void hasDifferentSection_trueWhenDifferentSection() {
void hasDifferentSection_falseSameSection() {
Schedule schedule = new Schedule(List.of(
new Course(
"Course1", List.of("Prof"), "COMP", 101, 'A', "Room", 3, "2023_Fall",
1, "Course1", List.of("Prof"), "COMP", 101, 'A', "Room", 3, "2023_Fall",
List.of(), false,false,0,30
)
));

Course newCourse = new Course(
"Course1", List.of("Prof"), "COMP", 101, 'A', "Room", 3, "2023_Fall",
1, "Course1", List.of("Prof"), "COMP", 101, 'A', "Room", 3, "2023_Fall",
List.of(), false,false,0,30
);

Expand All @@ -249,13 +252,13 @@ void hasDifferentSection_falseSameSection() {
void hasDifferentSection_falseDifferentCourse() {
Schedule schedule = new Schedule(List.of(
new Course(
"Course1", List.of("Prof"), "COMP", 101, 'A', "Room", 3, "2023_Fall",
1, "Course1", List.of("Prof"), "COMP", 101, 'A', "Room", 3, "2023_Fall",
List.of(), false,false,0,30
)
));

Course newCourse = new Course(
"Course2", List.of("Prof"), "COMP", 102, 'A', "Room", 3, "2023_Fall",
2, "Course2", List.of("Prof"), "COMP", 102, 'A', "Room", 3, "2023_Fall",
List.of(), false,false,0,30
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public void testSemesterFilter() {

private static Course emptyWithSemester(String semester) {
return new Course(
1,
"",
List.of(""),
"",
Expand Down
1 change: 1 addition & 0 deletions backend/src/test/java/edu/gcc/hallmonitor/TimeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public void testMatchTime() {

private static Course emptyAtTimes(List<LocalTime> times) {
return new Course(
1,
"",
List.of(""),
"",
Expand Down
Loading