2222public class CourseService {
2323
2424 // TODO: Nothing(!) tested yet
25-
26- private CourseRepository courseRepository ;
27- private ExerciseRepository exerciseRepository ;
28- private SolutionRepository solutionRepository ;
29- private TeacherRepository teacherRepository ;
30- private StudentRepository studentRepository ;
31-
25+
26+ private CourseRepository courseRepository ;
27+ private ExerciseRepository exerciseRepository ;
28+ private SolutionRepository solutionRepository ;
29+ private TeacherRepository teacherRepository ;
30+ private StudentRepository studentRepository ;
31+
3232 @ Autowired
33- public CourseService (CourseRepository courseRepository , ExerciseRepository exerciseRepository , SolutionRepository solutionRepository ,
34- TeacherRepository teacherRepository , StudentRepository studentRepository ) {
33+ public CourseService (CourseRepository courseRepository , ExerciseRepository exerciseRepository ,
34+ SolutionRepository solutionRepository , TeacherRepository teacherRepository ,
35+ StudentRepository studentRepository ) {
3536 this .courseRepository = courseRepository ;
3637 this .exerciseRepository = exerciseRepository ;
3738 this .solutionRepository = solutionRepository ;
38- this .teacherRepository = teacherRepository ;
39- this .studentRepository = studentRepository ;
39+ this .teacherRepository = teacherRepository ;
40+ this .studentRepository = studentRepository ;
4041 }
4142
4243 public Course createCourse (Course course ) {
43- return courseRepository .save (course );
44+ return courseRepository .save (course );
4445 }
46+
4547 public void deleteCourse (Course course ) {
4648 courseRepository .delete (course );
4749 }
48-
50+
4951 public Course getCourseByID (int id ) {
50- return courseRepository .findById (id );
52+ return courseRepository .findById (id );
5153 }
52-
54+
5355 public Course getCourseByName (String name ) {
54- return courseRepository .findByName (name );
56+ return courseRepository .findByName (name );
5557 }
56-
58+
5759 public Student getStudent (Course course , Student student ) {
58- Student s ;
59- if ((s = studentRepository .findById (student .getId ())) == null ) { // TODO: if not tested!
60- s = studentRepository .findByName (student .getUser ().getUsername ());
60+ Student s = null ;
61+ try {
62+ if ((s = studentRepository .findById (student .getId ())) == null ) { // TODO: if not tested!
63+ s = studentRepository .findByName (student .getUser ().getUsername ());
64+ }
65+ } catch (NullPointerException e ) {
66+ return s ;
6167 }
6268 return s ;
6369 }
64-
70+
6571 public List <Student > getAllStudents (Course course ) {
6672 return courseRepository .getAllStudents (course .getId ());
6773 }
68-
74+
6975 public Teacher getCourseTeacher (Course course ) {
70- return courseRepository .getCourseTeacher (course .getId ());
76+ return courseRepository .getCourseTeacher (course .getId ());
7177 }
72-
78+
7379 public void setCourseTeacher (Course course , Teacher teacher ) {
7480 teacherRepository .save (teacher );
7581 courseRepository .addUserToCourse (teacher .getId (), course .getId ());
7682 }
77-
83+
7884 public void deleteCourseTeacher (Course course , Teacher teacher ) {
7985 teacherRepository .delete (teacher );
8086 courseRepository .removeUserFromCourse (teacher .getId (), course .getId ());
8187 }
82-
83- // TODO: working with student / teacher table
88+
89+ // TODO: working with student / teacher table
8490 public void addStudentToCourse (Course course , Student student ) {
85- studentRepository .save (student );
91+ studentRepository .save (student );
8692 courseRepository .addUserToCourse (student .getId (), course .getId ());
8793 }
88-
94+
8995 public void addStudentsToCourse (Course course , Set <Student > students ) {
90- for (Student student : students ) {
91- studentRepository .save (student );
96+ for (Student student : students ) {
97+ studentRepository .save (student );
9298 courseRepository .addUserToCourse (student .getId (), course .getId ());
9399 }
94100 }
95-
101+
96102 public void removeStudentFromCourse (Course course , Student student ) {
97103 studentRepository .delete (student );
98104 courseRepository .removeUserFromCourse (student .getId (), student .getId ());
99105 }
100-
106+
101107 public void removeStudentsFromCourse (Course course , Set <Student > students ) {
102- for (Student student : students ) {
108+ for (Student student : students ) {
103109 studentRepository .delete (student );
104110 courseRepository .removeUserFromCourse (student .getId (), student .getId ());
105111 }
106112 }
107-
113+
108114 public boolean isUserTeacher (Course course , User user ) {
109- return courseRepository .isUserTeacher (user .getId (), course .getId ()) != 0 ;
115+ return courseRepository .isUserTeacher (user .getId (), course .getId ()) != 0 ;
110116 }
111-
117+
112118 public boolean isUserStudent (Course course , User user ) {
113- return courseRepository .isUserStudent (user .getId (), course .getId ()) != 0 ;
119+ return courseRepository .isUserStudent (user .getId (), course .getId ()) != 0 ;
114120 }
115-
121+
116122 public boolean isUserInCourse (Course course , User user ) {
117- return courseRepository .isUserStudent (user .getId (), course .getId ()) != 0 || courseRepository .isUserTeacher (user .getId (), course .getId ()) != 0 ;
123+ return courseRepository .isUserStudent (user .getId (), course .getId ()) != 0
124+ || courseRepository .isUserTeacher (user .getId (), course .getId ()) != 0 ;
118125 }
119-
126+
120127 public Exercise createExercise (Exercise exercise ) {
121- return exerciseRepository .save (exercise );
128+ return exerciseRepository .save (exercise );
122129 }
130+
123131 public void deleteExercise (Exercise exercise ) {
124132 exerciseRepository .delete (exercise );
125133 }
126-
134+
127135 public Exercise getExerciseByID (int id ) {
128- return exerciseRepository .findById (id );
136+ return exerciseRepository .findById (id );
129137 }
138+
130139 /**
131- * Get exercise by course_id and course name
140+ * Get exercise by course_id and course name
132141 *
133142 * @param course_id
134143 * @param name
135144 * @return
136145 */
137146 public Exercise getExerciseByCourse (int course_id , String name ) {
138- return exerciseRepository .findByCourse (course_id , name );
147+ return exerciseRepository .findByCourse (course_id , name );
139148 }
140-
141149
142-
143-
144- public Solution createSolution (Solution solution ) {
145- return solutionRepository .save (solution );
150+ public Solution createSolution (Solution solution ) {
151+ return solutionRepository .save (solution );
146152 }
153+
147154 public void deleteSolution (Solution solution ) {
148155 solutionRepository .delete (solution );
149156 }
150157
151158 public Solution getSolutionByID (int id ) {
152- return solutionRepository .findById (id );
159+ return solutionRepository .findById (id );
153160 }
161+
154162 /**
155- * Get solution by exercise_id and exercise name
163+ * Get solution by exercise_id and exercise name
156164 *
157165 * @param exercise_id
158166 * @param name
@@ -161,6 +169,6 @@ public Solution getSolutionByID(int id) {
161169 public Solution getSolutionByExercise (int exercise_id , String name ) {
162170 return solutionRepository .findByExercise (exercise_id , name );
163171 }
164-
165- // TODO: teacher correcting students work
172+
173+ // TODO: teacher correcting students work
166174}
0 commit comments