diff --git a/backend/src/main/java/edu/gcc/hallmonitor/Course.java b/backend/src/main/java/edu/gcc/hallmonitor/Course.java index 0b1c82a..cfaf157 100644 --- a/backend/src/main/java/edu/gcc/hallmonitor/Course.java +++ b/backend/src/main/java/edu/gcc/hallmonitor/Course.java @@ -5,6 +5,7 @@ import com.fasterxml.jackson.annotation.JsonProperty; public record Course( + int id, String name, @JsonProperty("faculty") List professor, diff --git a/backend/src/main/java/edu/gcc/hallmonitor/Schedule.java b/backend/src/main/java/edu/gcc/hallmonitor/Schedule.java index e2d84f7..e84816e 100644 --- a/backend/src/main/java/edu/gcc/hallmonitor/Schedule.java +++ b/backend/src/main/java/edu/gcc/hallmonitor/Schedule.java @@ -148,6 +148,7 @@ public List getCourses() { } Course copyCourse = new Course( + c.id(), c.name(), new ArrayList<>(c.professor()), c.department(), diff --git a/backend/src/main/java/edu/gcc/hallmonitor/Search.java b/backend/src/main/java/edu/gcc/hallmonitor/Search.java index 89e52f8..d38c589 100644 --- a/backend/src/main/java/edu/gcc/hallmonitor/Search.java +++ b/backend/src/main/java/edu/gcc/hallmonitor/Search.java @@ -51,6 +51,7 @@ private static void loadCourses() { List courseTimes = mapper.readerForListOf(CourseTime.class).readValue(time_json); Course c = new Course( + rs.getInt("id"), rs.getString("name"), facultyList, rs.getString("subject"), diff --git a/backend/src/main/java/edu/gcc/hallmonitor/User.java b/backend/src/main/java/edu/gcc/hallmonitor/User.java index b7f2bc2..1832efe 100644 --- a/backend/src/main/java/edu/gcc/hallmonitor/User.java +++ b/backend/src/main/java/edu/gcc/hallmonitor/User.java @@ -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 @@ -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() { @@ -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 = ?" ); @@ -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 = ?" ); @@ -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 = ?" ); @@ -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 (?, ?, ?)" ); @@ -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); diff --git a/backend/src/test/java/edu/gcc/hallmonitor/DaysTest.java b/backend/src/test/java/edu/gcc/hallmonitor/DaysTest.java index 2ae190d..78bd9f5 100644 --- a/backend/src/test/java/edu/gcc/hallmonitor/DaysTest.java +++ b/backend/src/test/java/edu/gcc/hallmonitor/DaysTest.java @@ -36,6 +36,7 @@ public void testMatchSingleDay() { public Course emptyOnDays(List days) { return new Course( + 1, "", List.of(""), "", diff --git a/backend/src/test/java/edu/gcc/hallmonitor/DepartmentTest.java b/backend/src/test/java/edu/gcc/hallmonitor/DepartmentTest.java index 3d30374..b7e26e2 100644 --- a/backend/src/test/java/edu/gcc/hallmonitor/DepartmentTest.java +++ b/backend/src/test/java/edu/gcc/hallmonitor/DepartmentTest.java @@ -20,6 +20,7 @@ public void testDepartmentFilter() { private static Course emptyWithDepartment(String department) { return new Course( + 1, "", List.of(""), department, diff --git a/backend/src/test/java/edu/gcc/hallmonitor/NumCreditsTest.java b/backend/src/test/java/edu/gcc/hallmonitor/NumCreditsTest.java index 99d50bc..2711105 100644 --- a/backend/src/test/java/edu/gcc/hallmonitor/NumCreditsTest.java +++ b/backend/src/test/java/edu/gcc/hallmonitor/NumCreditsTest.java @@ -20,6 +20,7 @@ public void testNumCreditsFilter() { private static Course emptyWithCredits(int credits) { return new Course( + 1, "", List.of(""), "", diff --git a/backend/src/test/java/edu/gcc/hallmonitor/ProfNameTest.java b/backend/src/test/java/edu/gcc/hallmonitor/ProfNameTest.java index 6b640fa..819cecb 100644 --- a/backend/src/test/java/edu/gcc/hallmonitor/ProfNameTest.java +++ b/backend/src/test/java/edu/gcc/hallmonitor/ProfNameTest.java @@ -20,6 +20,7 @@ public void testProfNameFilter() { private static Course emptyWithProf(String prof) { return new Course( + 1, "", List.of(prof), "", diff --git a/backend/src/test/java/edu/gcc/hallmonitor/ScheduleTest.java b/backend/src/test/java/edu/gcc/hallmonitor/ScheduleTest.java index 97e2441..c5a4a29 100644 --- a/backend/src/test/java/edu/gcc/hallmonitor/ScheduleTest.java +++ b/backend/src/test/java/edu/gcc/hallmonitor/ScheduleTest.java @@ -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++) { @@ -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", @@ -75,6 +77,7 @@ void loadSaveSchedule() { 32 ), new Course( + 2, "INTRO TO COMPUTER SCIENCE", List.of("Dickinson, Brian C"), "COMP", @@ -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 ); @@ -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 ); @@ -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 ); @@ -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 ); @@ -194,7 +197,7 @@ 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 ) @@ -202,7 +205,7 @@ void checkForOverlap_invalidTimeIgnored() { // 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 ); @@ -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 ); @@ -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 ); @@ -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 ); diff --git a/backend/src/test/java/edu/gcc/hallmonitor/SemesterTest.java b/backend/src/test/java/edu/gcc/hallmonitor/SemesterTest.java index 48da53b..83ab15d 100644 --- a/backend/src/test/java/edu/gcc/hallmonitor/SemesterTest.java +++ b/backend/src/test/java/edu/gcc/hallmonitor/SemesterTest.java @@ -21,6 +21,7 @@ public void testSemesterFilter() { private static Course emptyWithSemester(String semester) { return new Course( + 1, "", List.of(""), "", diff --git a/backend/src/test/java/edu/gcc/hallmonitor/TimeTest.java b/backend/src/test/java/edu/gcc/hallmonitor/TimeTest.java index 39786d1..a3d3b58 100644 --- a/backend/src/test/java/edu/gcc/hallmonitor/TimeTest.java +++ b/backend/src/test/java/edu/gcc/hallmonitor/TimeTest.java @@ -21,6 +21,7 @@ public void testMatchTime() { private static Course emptyAtTimes(List times) { return new Course( + 1, "", List.of(""), "",