From cc59d0e28a19c6c7d423a7b5ce835ec04cb7c4d3 Mon Sep 17 00:00:00 2001 From: Julio Date: Fri, 9 Aug 2024 14:19:07 -0400 Subject: [PATCH 1/5] hi --- src/main/java/sba/sms/test.java | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 src/main/java/sba/sms/test.java diff --git a/src/main/java/sba/sms/test.java b/src/main/java/sba/sms/test.java new file mode 100644 index 0000000..24fbb02 --- /dev/null +++ b/src/main/java/sba/sms/test.java @@ -0,0 +1,4 @@ +package sba.sms; + +public class test { +} From b0ffc14e0561acd9c1dd604fc5b3fc788e667a0f Mon Sep 17 00:00:00 2001 From: Julio Date: Mon, 12 Aug 2024 03:02:55 -0400 Subject: [PATCH 2/5] Almost done --- src/main/java/sba/sms/models/Course.java | 54 ++++++++- src/main/java/sba/sms/models/Student.java | 66 +++++++++- .../java/sba/sms/services/CourseService.java | 58 ++++++++- .../java/sba/sms/services/StudentService.java | 114 +++++++++++++++++- src/main/java/sba/sms/test.java | 4 - src/main/java/sba/sms/utils/CommandLine.java | 4 +- .../java/sba/sms/utils/HibernateUtil.java | 9 +- src/main/resources/hibernate.cfg.xml | 15 ++- .../sba/sms/services/StudentServiceTest.java | 23 +++- 9 files changed, 327 insertions(+), 20 deletions(-) delete mode 100644 src/main/java/sba/sms/test.java diff --git a/src/main/java/sba/sms/models/Course.java b/src/main/java/sba/sms/models/Course.java index be91a50..c3db5fc 100644 --- a/src/main/java/sba/sms/models/Course.java +++ b/src/main/java/sba/sms/models/Course.java @@ -2,19 +2,67 @@ import jakarta.persistence.*; import lombok.*; -import lombok.experimental.FieldDefaults; -import java.util.LinkedHashSet; +import java.util.HashSet; import java.util.Objects; import java.util.Set; /** * Course is a POJO, configured as a persistent class that represents (or maps to) a table * name 'course' in the database. A Course object contains fields that represent course - * information and a mapping of 'courses' that indicate an inverse or referencing side + * information and a mapping of 'cours`es' that indicate an inverse or referencing side * of the relationship. Implement Lombok annotations to eliminate boilerplate code. */ +@NoArgsConstructor +@AllArgsConstructor + + +@Setter +@Getter +@Entity public class Course { + // Getter Setter methods + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "id", nullable = false) + private int id; + + @Column(name = "name", length = 50, nullable = false) + private String name; + + @Column(name = "instructor", length = 50, nullable = false) + private String instructor; + + @ManyToMany(mappedBy = "courses", fetch = FetchType.EAGER, cascade = {CascadeType.DETACH, CascadeType.REMOVE, CascadeType.MERGE, CascadeType.PERSIST}) + private Set students = new HashSet<>(); + + // Required-args constructor + public Course(String name, String instructor){ + this.name = name; + this.instructor = instructor; + } + + // toString method + @Override + public String toString(){ + return "Course{" + "id=" + id + ", name=" + name + "\\" + ", instructor=" + instructor + "\\" + "}"; + } + + + // hash code method + @Override + public int hashCode(){ + return Objects.hash(id, name ,instructor); + } + + //Helper method + public boolean hasStudent(Student student){ + return this.students.contains(student); + } + + + } + diff --git a/src/main/java/sba/sms/models/Student.java b/src/main/java/sba/sms/models/Student.java index db769c3..4fc5519 100644 --- a/src/main/java/sba/sms/models/Student.java +++ b/src/main/java/sba/sms/models/Student.java @@ -2,9 +2,8 @@ import jakarta.persistence.*; import lombok.*; -import lombok.experimental.FieldDefaults; -import java.util.LinkedHashSet; +import java.util.HashSet; import java.util.Objects; import java.util.Set; @@ -16,9 +15,72 @@ * data. The Student class can be viewed as the owner of the bi-directional relationship. * Implement Lombok annotations to eliminate boilerplate code. */ +@NoArgsConstructor +@AllArgsConstructor +@Setter +@Getter +@Table (name = "student") +@Entity public class Student { + // Getter Setters + @Id + @Column(name = "email", length = 50, nullable = false) + private String email; + @Column(name = "name", length = 50, nullable = false) + private String name; + + @Column(name = "password", length = 50, nullable = false) + private String password; + + @ManyToMany(fetch = FetchType.EAGER, cascade = {CascadeType.DETACH, CascadeType.REMOVE, CascadeType.MERGE, CascadeType.PERSIST}) + @JoinTable( + name = "student_courses", + joinColumns = @JoinColumn(name = "student_email"), + inverseJoinColumns = @JoinColumn(name = "course_id") + ) + private Set courses = new HashSet<>(); + + // + public Student(String email, String name, String password){ + this.email = email; + this.name = name; + this.password = password; + } + + public static void addCourse(Course courseById) { + + } + + + @Override + public String toString(){ + return "Student{" + + "email='" + email + '\'' + + ", name='" + name + '\'' + + ", password='" + password + '\'' + + '}'; + } + + @Override + public boolean equals(Object o){ + if(this == o) return true; + if(o == null || getClass() != o.getClass()) return false; + Student student = (Student) o; + return Objects.equals(email, student.email) && + Objects.equals(name, student.name) && + Objects.equals(password, student.password); + } + + @Override + public int hashCode(){ + return Objects.hash(email, name, password); + } + + public boolean isEnrolledInCourse(Course course){ + return this.courses.contains(course); + } } diff --git a/src/main/java/sba/sms/services/CourseService.java b/src/main/java/sba/sms/services/CourseService.java index ad01eed..d528820 100644 --- a/src/main/java/sba/sms/services/CourseService.java +++ b/src/main/java/sba/sms/services/CourseService.java @@ -16,6 +16,62 @@ * CourseI interface, overrides all abstract service methods and * provides implementation for each method. */ -public class CourseService { +public class CourseService implements CourseI{ + @Override + public void createCourse(Course course) { + Session s = HibernateUtil.getSessionFactory().openSession(); + Transaction tx = null; + + try{ + tx = s.beginTransaction(); + s.persist(course); + tx.commit(); + } catch (HibernateException exception){ + if(tx != null) tx.rollback(); + } finally { + s.close(); + } + } + + @Override + public Course getCourseById(int courseId) { + Session s = HibernateUtil.getSessionFactory().openSession(); + Transaction tx = null; + Course course = new Course(); + + try{ + tx = s.beginTransaction(); + Query q = s.createQuery("from Course where id = :id", Course.class); + q.setParameter("id", courseId); + course = q.getSingleResult(); + tx.commit(); + } catch (HibernateException exception){ + if(tx != null) tx.rollback(); + exception.printStackTrace(); + } finally { + s.close(); + } + return course; + } + + @Override + public List getAllCourses() { + Session s = HibernateUtil.getSessionFactory().openSession(); + Transaction tx = null; + List courseList = new ArrayList<>(); + + try{ + tx = s.beginTransaction(); + Query q = s.createQuery("from Course", Course.class); + courseList = q.getResultList(); + tx.commit(); + } catch (HibernateException exception){ + if(tx != null) tx.rollback(); + exception.printStackTrace(); + } finally { + s.close(); + } + return courseList; + } } diff --git a/src/main/java/sba/sms/services/StudentService.java b/src/main/java/sba/sms/services/StudentService.java index 3bd2e2a..9cbc520 100644 --- a/src/main/java/sba/sms/services/StudentService.java +++ b/src/main/java/sba/sms/services/StudentService.java @@ -13,6 +13,7 @@ import java.util.ArrayList; import java.util.List; +import java.util.Objects; /** * StudentService is a concrete class. This class implements the @@ -21,6 +22,117 @@ * generate a logger file. */ -public class StudentService { +@Log +public class StudentService implements StudentI { + private static final CourseService courseService = new CourseService(); + @Override + public List getAllStudents() { + Session s = HibernateUtil.getSessionFactory().openSession(); + Transaction tx = null; + List studentList = new ArrayList<>(); + + try{ + tx = s.beginTransaction(); + Query q = s.createQuery("from Student", Student.class); + studentList = q.getResultList(); + tx.commit(); + } catch (HibernateException exception){ + if(tx != null) tx.rollback(); + exception.printStackTrace(); + } finally { + s.close(); + } + return studentList; + } + + @Override + public void createStudent(Student student) { + Session s = HibernateUtil.getSessionFactory().openSession(); + Transaction tx = null; + + try{ + tx = s.beginTransaction(); + s.persist(student); + tx.commit(); + } catch (HibernateException exception){ + if(tx != null) tx.rollback(); + exception.printStackTrace(); + } finally { + s.close(); + } + } + + @Override + public Student getStudentByEmail(String email) { + Session s = HibernateUtil.getSessionFactory().openSession(); + Transaction tx = null; + Student student = null; + try{ + tx = s.beginTransaction(); + Query q = s.createQuery("from Student where email = :email", Student.class); + q.setParameter("email", email); + student = q.getSingleResult(); + tx.commit(); + } catch (Exception exception){ + if(tx != null) tx.rollback(); + exception.printStackTrace(); + } finally { + s.close(); + } + return student; + } + + @Override + public boolean validateStudent(String email, String password) { + Student s = getStudentByEmail(email); + return s != null && s.getPassword().equals(password); + } + + @Override + public void registerStudentToCourse(String email, int courseId) { + Session s = HibernateUtil.getSessionFactory().openSession(); + Transaction tx = null; + + try{ + tx = s.beginTransaction(); + Student student = getStudentByEmail(email); + Student.addCourse(courseService.getCourseById(courseId)); + s.merge(student); + tx.commit(); + } catch (HibernateException exception){ + if(tx != null) tx.rollback(); + exception.printStackTrace(); + } finally { + s.close(); + } + } + + @Override + public List getStudentCourses(String email) { + Session s = HibernateUtil.getSessionFactory().openSession(); + Transaction tx = null; + List courseList = new ArrayList<>(); + + try{ + tx = s.beginTransaction(); + String nativeGetStudentCourses = "SELECT c.id, c.name, c.instructor " + + "FROM course AS c " + + "JOIN student_courses AS sc " + + "ON c.id = sc.course_id " + + "JOIN student AS s " + + "ON s.email = sc.student_email " + + "WHERE s.email = ?"; + NativeQuery studentCourses = s.createNativeQuery(nativeGetStudentCourses, Course.class); + studentCourses.setParameter("email", email); + courseList = studentCourses.getResultList(); + tx.commit(); + } catch (HibernateException exception){ + if(tx != null) tx.rollback(); + exception.printStackTrace(); + } finally { + s.close(); + } + return courseList; + } } diff --git a/src/main/java/sba/sms/test.java b/src/main/java/sba/sms/test.java deleted file mode 100644 index 24fbb02..0000000 --- a/src/main/java/sba/sms/test.java +++ /dev/null @@ -1,4 +0,0 @@ -package sba.sms; - -public class test { -} diff --git a/src/main/java/sba/sms/utils/CommandLine.java b/src/main/java/sba/sms/utils/CommandLine.java index 2b9cd57..f8f91d7 100644 --- a/src/main/java/sba/sms/utils/CommandLine.java +++ b/src/main/java/sba/sms/utils/CommandLine.java @@ -29,7 +29,7 @@ private CommandLine(){ * */ public static void addData(){ - /* + StudentService studentService = new StudentService(); CourseService courseService = new CourseService(); @@ -49,6 +49,6 @@ public static void addData(){ courseService.createCourse(new Course("Web Services", "Raheem Abolfathzadeh")); courseService.createCourse(new Course("Microservices", "Eric Heilig")); - */ + } } diff --git a/src/main/java/sba/sms/utils/HibernateUtil.java b/src/main/java/sba/sms/utils/HibernateUtil.java index a54f60b..acb8439 100644 --- a/src/main/java/sba/sms/utils/HibernateUtil.java +++ b/src/main/java/sba/sms/utils/HibernateUtil.java @@ -1,5 +1,7 @@ package sba.sms.utils; + + import lombok.Getter; import org.hibernate.SessionFactory; import org.hibernate.boot.Metadata; @@ -22,16 +24,19 @@ private HibernateUtil() { } /** - * @Getter builds a standard getter method for the object + * Getter builds a standard getter method for the object * sessionFactory. */ + + @Getter private static SessionFactory sessionFactory = buildSessionFactory(); + /** * Method builds a session factory from the 'hibernate.cfg.xml' file * in the 'resources' folder and returns a sessionFactory object. - * @return + * return */ private static SessionFactory buildSessionFactory() { diff --git a/src/main/resources/hibernate.cfg.xml b/src/main/resources/hibernate.cfg.xml index 0d6cb98..79c4dbd 100644 --- a/src/main/resources/hibernate.cfg.xml +++ b/src/main/resources/hibernate.cfg.xml @@ -6,8 +6,9 @@ + - create-drop + create-drop com.mysql.cj.jdbc.Driver @@ -15,13 +16,19 @@ jdbc:mysql://localhost:3306/smsdb-ll?createDatabaseIfNotExist=true root - root + password org.hibernate.dialect.MySQLDialect - - + true + true + + 5 + 20 + 300 + 50 + 3000 diff --git a/src/test/java/sba/sms/services/StudentServiceTest.java b/src/test/java/sba/sms/services/StudentServiceTest.java index ec32ae3..823b23a 100644 --- a/src/test/java/sba/sms/services/StudentServiceTest.java +++ b/src/test/java/sba/sms/services/StudentServiceTest.java @@ -13,8 +13,29 @@ import static org.assertj.core.api.Assertions.*; - +@FieldDefaults(level = AccessLevel.PRIVATE) class StudentServiceTest { + static StudentService studentService; + + @BeforeAll + static void beforeAll(){ + studentService = new StudentService(); + CommandLine.addData(); + } + + @Test + void getAllStudents(){ + + List expected = new ArrayList<>(Arrays.asList( + new Student("reema@gmail.com", "reema brown", "password"), + new Student("annette@gmail.com", "annette allen", "password"), + new Student("anthony@gmail.com", "anthony gallegos", "password"), + new Student("ariadna@gmail.com", "ariadna ramirez", "password"), + new Student("bolaji@gmail.com", "bolaji saibu", "password") + )); + + assertThat(studentService.getAllStudents()).hasSameElementsAs(expected); + } } \ No newline at end of file From 391825c0238edbf9c8fac700f673492150641a75 Mon Sep 17 00:00:00 2001 From: Julio Date: Mon, 12 Aug 2024 13:26:24 -0400 Subject: [PATCH 3/5] Done --- src/main/java/sba/sms/models/Course.java | 70 ++++++++++++++++--- src/main/java/sba/sms/models/Student.java | 53 +++++++++++--- .../java/sba/sms/services/CourseService.java | 2 + .../java/sba/sms/services/StudentService.java | 7 +- src/main/java/sba/sms/utils/CommandLine.java | 1 - .../java/sba/sms/utils/HibernateUtil.java | 2 +- src/main/resources/hibernate.cfg.xml | 4 +- .../sba/sms/services/StudentServiceTest.java | 27 ++----- 8 files changed, 122 insertions(+), 44 deletions(-) diff --git a/src/main/java/sba/sms/models/Course.java b/src/main/java/sba/sms/models/Course.java index c3db5fc..52119b6 100644 --- a/src/main/java/sba/sms/models/Course.java +++ b/src/main/java/sba/sms/models/Course.java @@ -10,15 +10,10 @@ /** * Course is a POJO, configured as a persistent class that represents (or maps to) a table * name 'course' in the database. A Course object contains fields that represent course - * information and a mapping of 'cours`es' that indicate an inverse or referencing side + * information and a mapping of that indicate an inverse or referencing side * of the relationship. Implement Lombok annotations to eliminate boilerplate code. */ -@NoArgsConstructor -@AllArgsConstructor - -@Setter -@Getter @Entity public class Course { @@ -32,22 +27,79 @@ public class Course { @Column(name = "name", length = 50, nullable = false) private String name; + @Column(name = "instructor", length = 50, nullable = false) private String instructor; @ManyToMany(mappedBy = "courses", fetch = FetchType.EAGER, cascade = {CascadeType.DETACH, CascadeType.REMOVE, CascadeType.MERGE, CascadeType.PERSIST}) private Set students = new HashSet<>(); + public Course(){ + + } + + public Course(int id, String name, String instructor, Set students){ + this.id = id; + this.name = name; + this.instructor = instructor; + this.students = students; + } + // Required-args constructor public Course(String name, String instructor){ this.name = name; this.instructor = instructor; } + public int getId(){ + return id; + } + + public void setId(int id){ + this.id = id; + } + + public String getName(){ + return name; + } + + public void setName(String name){ + this.name = name; + } + + public String getInstructor(){ + return instructor; + } + + public void setInstructor(String instructor){ + this.instructor = instructor; + } + + public Set getStudents(){ + return students; + } + + public void setStudents(Set students){ + this.students = students; + } + // toString method @Override public String toString(){ - return "Course{" + "id=" + id + ", name=" + name + "\\" + ", instructor=" + instructor + "\\" + "}"; + return "Course{" + + "id=" + id + + ", name='" + name + '\'' + + ", instructor='" + instructor + '\'' + + '}'; + + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Course course = (Course) o; + return id == course.id && Objects.equals(name, course.name) && Objects.equals(instructor, course.instructor) && Objects.equals(students, course.students); } @@ -58,9 +110,9 @@ public int hashCode(){ } //Helper method - public boolean hasStudent(Student student){ +public boolean hasStudent(Student student){ return this.students.contains(student); - } +} diff --git a/src/main/java/sba/sms/models/Student.java b/src/main/java/sba/sms/models/Student.java index 4fc5519..37c0fba 100644 --- a/src/main/java/sba/sms/models/Student.java +++ b/src/main/java/sba/sms/models/Student.java @@ -12,14 +12,13 @@ * Student is a POJO, configured as a persistent class that represents (or maps to) a table * name 'student' in the database. A Student object contains fields that represent student * login credentials and a join table containing a registered student's email and course(s) - * data. The Student class can be viewed as the owner of the bi-directional relationship. + * data. The Student class can be viewed as the owner of the relationship. * Implement Lombok annotations to eliminate boilerplate code. */ @NoArgsConstructor -@AllArgsConstructor -@Setter -@Getter + + @Table (name = "student") @Entity public class Student { @@ -42,13 +41,53 @@ public class Student { ) private Set courses = new HashSet<>(); + // - public Student(String email, String name, String password){ + public Student(String email, String name, String password, Set courses){ + this.email = email; + this.name = name; + this.password = password; + this.courses = courses; + } + + public Student(String email, String name, String password) { this.email = email; this.name = name; this.password = password; } + public String getEmail(){ + return email; + } + + public void setEmail(String email){ + this.email = email; + } + + public String getName(){ + return name; + } + + public void setName(String name){ + this.name = name; + } + + public String getPassword(){ + return password; + } + + public void setPassword(String password){ + this.password = password; + } + + public Set getCourses(){ + return courses; + } + + public void setCourses(Set courses){ + this.courses = courses; + } + public static void addCourse(Course courseById) { } @@ -78,9 +117,7 @@ public int hashCode(){ return Objects.hash(email, name, password); } - public boolean isEnrolledInCourse(Course course){ - return this.courses.contains(course); - } + } diff --git a/src/main/java/sba/sms/services/CourseService.java b/src/main/java/sba/sms/services/CourseService.java index d528820..3db28b9 100644 --- a/src/main/java/sba/sms/services/CourseService.java +++ b/src/main/java/sba/sms/services/CourseService.java @@ -2,12 +2,14 @@ import org.hibernate.HibernateException; import org.hibernate.Session; +import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.query.Query; import sba.sms.dao.CourseI; import sba.sms.models.Course; import sba.sms.utils.HibernateUtil; +import javax.security.auth.login.Configuration; import java.util.ArrayList; import java.util.List; diff --git a/src/main/java/sba/sms/services/StudentService.java b/src/main/java/sba/sms/services/StudentService.java index 9cbc520..38de19e 100644 --- a/src/main/java/sba/sms/services/StudentService.java +++ b/src/main/java/sba/sms/services/StudentService.java @@ -13,7 +13,7 @@ import java.util.ArrayList; import java.util.List; -import java.util.Objects; + /** * StudentService is a concrete class. This class implements the @@ -57,7 +57,8 @@ public void createStudent(Student student) { tx.commit(); } catch (HibernateException exception){ if(tx != null) tx.rollback(); - exception.printStackTrace(); + exception. + printStackTrace(); } finally { s.close(); } @@ -124,7 +125,7 @@ public List getStudentCourses(String email) { "ON s.email = sc.student_email " + "WHERE s.email = ?"; NativeQuery studentCourses = s.createNativeQuery(nativeGetStudentCourses, Course.class); - studentCourses.setParameter("email", email); + studentCourses.setParameter(1, email); courseList = studentCourses.getResultList(); tx.commit(); } catch (HibernateException exception){ diff --git a/src/main/java/sba/sms/utils/CommandLine.java b/src/main/java/sba/sms/utils/CommandLine.java index f8f91d7..f78c36e 100644 --- a/src/main/java/sba/sms/utils/CommandLine.java +++ b/src/main/java/sba/sms/utils/CommandLine.java @@ -20,7 +20,6 @@ private CommandLine(){ /** * Creates and persist student object to the 'student' table and * course objects to the 'course' table - * * ATTENTION PLEASE READ * Uncomment the following code after creating both models and entities for ease of adding and dropping dummy data into the database, remember that * in hibernate.cfg.xml hibernate.hbm2ddl.auto = create-drop will create and drop the tables every time the application diff --git a/src/main/java/sba/sms/utils/HibernateUtil.java b/src/main/java/sba/sms/utils/HibernateUtil.java index acb8439..81fca6c 100644 --- a/src/main/java/sba/sms/utils/HibernateUtil.java +++ b/src/main/java/sba/sms/utils/HibernateUtil.java @@ -14,7 +14,7 @@ * secure connection to a database and permits CRUD operations on a table. * The session configuration derives from the hibernate.cfg.xml file in * the 'resources' folder. - * + * WARNING! * DO NOT MODIFY THIS CODE */ diff --git a/src/main/resources/hibernate.cfg.xml b/src/main/resources/hibernate.cfg.xml index 79c4dbd..8a0f083 100644 --- a/src/main/resources/hibernate.cfg.xml +++ b/src/main/resources/hibernate.cfg.xml @@ -21,8 +21,8 @@ org.hibernate.dialect.MySQLDialect - true - true + + 5 20 diff --git a/src/test/java/sba/sms/services/StudentServiceTest.java b/src/test/java/sba/sms/services/StudentServiceTest.java index 823b23a..f62cf58 100644 --- a/src/test/java/sba/sms/services/StudentServiceTest.java +++ b/src/test/java/sba/sms/services/StudentServiceTest.java @@ -13,29 +13,16 @@ import static org.assertj.core.api.Assertions.*; -@FieldDefaults(level = AccessLevel.PRIVATE) class StudentServiceTest { - - static StudentService studentService; - - @BeforeAll - static void beforeAll(){ - studentService = new StudentService(); - CommandLine.addData(); - } +@Test + void validateStudent(){} @Test - void getAllStudents(){ - - List expected = new ArrayList<>(Arrays.asList( - new Student("reema@gmail.com", "reema brown", "password"), - new Student("annette@gmail.com", "annette allen", "password"), - new Student("anthony@gmail.com", "anthony gallegos", "password"), - new Student("ariadna@gmail.com", "ariadna ramirez", "password"), - new Student("bolaji@gmail.com", "bolaji saibu", "password") - )); + void getStudentCourses(){} - assertThat(studentService.getAllStudents()).hasSameElementsAs(expected); - } + @Test + void registerStudentToCourse(){} + @Test + void getStudentByEmail(){} } \ No newline at end of file From 5bd4f8132f304094ec495aa925763ada0e50673c Mon Sep 17 00:00:00 2001 From: Julio Date: Mon, 12 Aug 2024 14:41:27 -0400 Subject: [PATCH 4/5] done 2 --- src/main/java/sba/sms/models/Course.java | 7 +++---- src/main/java/sba/sms/models/Student.java | 5 +---- src/main/java/sba/sms/services/CourseService.java | 2 +- src/main/java/sba/sms/services/StudentService.java | 2 +- src/main/resources/hibernate.cfg.xml | 5 ----- 5 files changed, 6 insertions(+), 15 deletions(-) diff --git a/src/main/java/sba/sms/models/Course.java b/src/main/java/sba/sms/models/Course.java index 52119b6..868ce66 100644 --- a/src/main/java/sba/sms/models/Course.java +++ b/src/main/java/sba/sms/models/Course.java @@ -110,11 +110,10 @@ public int hashCode(){ } //Helper method -public boolean hasStudent(Student student){ + public boolean hasStudent(Student student){ return this.students.contains(student); -} - + } -} +} \ No newline at end of file diff --git a/src/main/java/sba/sms/models/Student.java b/src/main/java/sba/sms/models/Student.java index 37c0fba..634a426 100644 --- a/src/main/java/sba/sms/models/Student.java +++ b/src/main/java/sba/sms/models/Student.java @@ -119,7 +119,4 @@ public int hashCode(){ - } - - - +} \ No newline at end of file diff --git a/src/main/java/sba/sms/services/CourseService.java b/src/main/java/sba/sms/services/CourseService.java index 3db28b9..1a14fa3 100644 --- a/src/main/java/sba/sms/services/CourseService.java +++ b/src/main/java/sba/sms/services/CourseService.java @@ -76,4 +76,4 @@ public List getAllCourses() { } return courseList; } -} +} \ No newline at end of file diff --git a/src/main/java/sba/sms/services/StudentService.java b/src/main/java/sba/sms/services/StudentService.java index 38de19e..2ecc164 100644 --- a/src/main/java/sba/sms/services/StudentService.java +++ b/src/main/java/sba/sms/services/StudentService.java @@ -136,4 +136,4 @@ public List getStudentCourses(String email) { } return courseList; } -} +} \ No newline at end of file diff --git a/src/main/resources/hibernate.cfg.xml b/src/main/resources/hibernate.cfg.xml index 8a0f083..5d7b547 100644 --- a/src/main/resources/hibernate.cfg.xml +++ b/src/main/resources/hibernate.cfg.xml @@ -24,11 +24,6 @@ - 5 - 20 - 300 - 50 - 3000 From d84b584febe955ca6515e334a5d54b04bac187d6 Mon Sep 17 00:00:00 2001 From: Julio Date: Mon, 12 Aug 2024 16:28:01 -0400 Subject: [PATCH 5/5] DONE! --- src/main/java/sba/sms/models/Course.java | 1 + .../java/sba/sms/services/StudentService.java | 27 ++++++++++++++----- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/main/java/sba/sms/models/Course.java b/src/main/java/sba/sms/models/Course.java index 868ce66..bdcb30e 100644 --- a/src/main/java/sba/sms/models/Course.java +++ b/src/main/java/sba/sms/models/Course.java @@ -38,6 +38,7 @@ public Course(){ } + public Course(int id, String name, String instructor, Set students){ this.id = id; this.name = name; diff --git a/src/main/java/sba/sms/services/StudentService.java b/src/main/java/sba/sms/services/StudentService.java index 2ecc164..7d74d96 100644 --- a/src/main/java/sba/sms/services/StudentService.java +++ b/src/main/java/sba/sms/services/StudentService.java @@ -86,10 +86,20 @@ public Student getStudentByEmail(String email) { @Override public boolean validateStudent(String email, String password) { + if (email == null || email.isEmpty() || password == null || password.isEmpty()) { + return false; + } + Student s = getStudentByEmail(email); - return s != null && s.getPassword().equals(password); + if (s != null && s.getPassword() != null) { + return s.getPassword().equals(password); + } + return false; } + + + @Override public void registerStudentToCourse(String email, int courseId) { Session s = HibernateUtil.getSessionFactory().openSession(); @@ -98,11 +108,16 @@ public void registerStudentToCourse(String email, int courseId) { try{ tx = s.beginTransaction(); Student student = getStudentByEmail(email); - Student.addCourse(courseService.getCourseById(courseId)); - s.merge(student); - tx.commit(); - } catch (HibernateException exception){ - if(tx != null) tx.rollback(); + Course course = courseService.getCourseById(courseId); + if (student != null && course != null) { + student.addCourse(course); // Ensure addCourse is an instance method + s.merge(student); + tx.commit(); + } else { + System.out.println("Student or Course not found."); + } + } catch (HibernateException exception) { + if (tx != null) tx.rollback(); exception.printStackTrace(); } finally { s.close();