Skip to content

Commit d949c77

Browse files
author
Hattinger04
committed
reworking many rest services after testing them
1 parent 5633cd9 commit d949c77

File tree

12 files changed

+80
-40
lines changed

12 files changed

+80
-40
lines changed

src/main/java/io/github/Hattinger04/course/CourseController.java

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.github.Hattinger04.course;
22

33
import java.util.List;
4+
import java.util.stream.Collectors;
45

56
import org.springframework.beans.factory.annotation.Autowired;
67
import org.springframework.http.HttpStatus;
@@ -59,8 +60,8 @@ public ResponseEntity<?> getStudents() {
5960
*/
6061
@GetMapping("/students/{id}")
6162
@PreAuthorize("hasAuthority('DEV')")
62-
public ResponseEntity<?> getStudentByID(@PathVariable int id) {
63-
Student student = courseService.getStudentByID(id);
63+
public ResponseEntity<?> getStudentByID(@PathVariable long id) {
64+
Student student = courseService.getStudentByID((int)id);
6465
if (student == null || courseService.getUserByStudent(student) == null) {
6566
return new ResponseEntity<>("Student not exisiting!", HttpStatus.NOT_FOUND);
6667
}
@@ -75,16 +76,17 @@ public ResponseEntity<?> getStudentByID(@PathVariable int id) {
7576
* @param json
7677
* @return
7778
*/
78-
@GetMapping("/course/{courseid}/students/{studentsid}")
79+
@GetMapping("/course/{courseid}/students/{studentid}")
7980
@PreAuthorize("hasAuthority('TEACHER')")
80-
public ResponseEntity<?> getStudentInCourseByCourseID(@PathVariable int courseid, @PathVariable int studentid) {
81-
Student student = courseService.getStudentByID(studentid);
82-
Course course = courseService.getCourseByID(studentid);
81+
public ResponseEntity<?> getStudentInCourseByCourseID(@PathVariable long courseid, @PathVariable long studentid) {
82+
Student student = courseService.getStudentByID((int) studentid);
83+
Course course = courseService.getCourseByID((int) studentid);
8384
User user = courseService.getUserByStudent(student);
84-
if (student == null || user == null || courseService.getUserByStudent(student) == null || courseService.isUserInCourse(course, user)) {
85+
if (student == null || user == null || !courseService.isUserInCourse(course, user)) {
8586
return new ResponseEntity<>("Student not exisiting!", HttpStatus.NOT_FOUND);
8687
}
87-
return new ResponseEntity<>(user, HttpStatus.OK);
88+
student.getUser().setPassword("");
89+
return new ResponseEntity<>(student, HttpStatus.OK);
8890
}
8991

9092

@@ -96,12 +98,16 @@ public ResponseEntity<?> getStudentInCourseByCourseID(@PathVariable int courseid
9698
*/
9799
@GetMapping("/course/{id}/students")
98100
@PreAuthorize("hasAuthority('TEACHER')")
99-
public ResponseEntity<?> getAllStudentsByCourseID(@PathVariable int id) {
100-
Course course = courseService.getCourseByID(id);
101-
List<User> students;
101+
public ResponseEntity<?> getAllStudentsByCourseID(@PathVariable long id) {
102+
Course course = courseService.getCourseByID((int) id);
103+
List<Student> students;
102104
if ((students = courseService.getAllStudentsInCourse(course)) == null) {
103105
return new ResponseEntity<>("Course not existing or no users in course!", HttpStatus.NOT_FOUND);
104106
}
107+
students.stream().map(user -> {
108+
user.getUser().setPassword("");
109+
return user;
110+
}).collect(Collectors.toList());
105111
return new ResponseEntity<>(students, HttpStatus.OK);
106112
}
107113

@@ -115,10 +121,14 @@ public ResponseEntity<?> getAllStudentsByCourseID(@PathVariable int id) {
115121
@PreAuthorize("hasAuthority('TEACHER')")
116122
public ResponseEntity<?> getAllStudentsByCourseName(@RequestParam(name = "coursename", required = false) String coursename) {
117123
Course course = courseService.getCourseByName(coursename);
118-
List<User> students;
124+
List<Student> students;
119125
if ((students = courseService.getAllStudentsInCourse(course)) == null) {
120126
return new ResponseEntity<>("Course not existing or no users in course!", HttpStatus.NOT_FOUND);
121127
}
128+
students.stream().map(user -> {
129+
user.getUser().setPassword("");
130+
return user;
131+
}).collect(Collectors.toList());
122132
return new ResponseEntity<>(students, HttpStatus.OK);
123133
}
124134

@@ -160,8 +170,8 @@ public ResponseEntity<?> removeStudentCourse(@RequestBody JsonNode node) {
160170
*/
161171
@GetMapping("/course/{id}/teachers")
162172
@PreAuthorize("hasAuthority('TEACHER')")
163-
public ResponseEntity<?> getCourseTeacherByCourseID(@PathVariable int id) {
164-
Course course = courseService.getCourseByID(id);
173+
public ResponseEntity<?> getCourseTeacherByCourseID(@PathVariable long id) {
174+
Course course = courseService.getCourseByID((int) id);
165175
List<User> teachers;
166176
if ((teachers = courseService.getCourseTeachers(course)) == null) {
167177
return new ResponseEntity<>("There is no teacher in this course - contact an admin!", HttpStatus.NOT_FOUND);
@@ -195,8 +205,8 @@ public ResponseEntity<?> getCourseTeacherByCourseName(@RequestParam(name = "cour
195205
*/
196206
@GetMapping("/course/{id}")
197207
@PreAuthorize("hasAuthority('TEACHER')")
198-
public ResponseEntity<?> getCourseByID(@PathVariable int id) {
199-
Course course = courseService.getCourseByID(id);
208+
public ResponseEntity<?> getCourseByID(@PathVariable long id) {
209+
Course course = courseService.getCourseByID((int) id);
200210
if (course == null) {
201211
return new ResponseEntity<>("There is no course with this name!", HttpStatus.NOT_FOUND);
202212
}

src/main/java/io/github/Hattinger04/course/model/CourseService.java

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.ArrayList;
44
import java.util.List;
55
import java.util.Set;
6+
import java.util.stream.Collectors;
67

78
import org.springframework.beans.factory.annotation.Autowired;
89
import org.springframework.stereotype.Service;
@@ -40,16 +41,25 @@ public CourseService(CourseRepository courseRepository, ExerciseRepository exerc
4041
}
4142

4243
public List<Course> getAllCourses() {
43-
return courseRepository.findAll();
44+
try {
45+
return courseRepository.findAll();
46+
} catch (Exception e) {
47+
return null;
48+
}
4449
}
45-
50+
4651
public Course createCourse(Course course) {
47-
return courseRepository.save(course);
52+
try {
53+
return courseRepository.save(course);
54+
} catch (Exception e) {
55+
return null;
56+
}
57+
4858
}
4959

5060
public boolean deleteCourse(Course course) {
5161
try {
52-
List<User> students = getAllStudentsInCourse(course);
62+
List<User> students = getAllUsersInCourse(course);
5363
Student s;
5464
for (User user : students) {
5565
if ((s = studentRepository.findByUserId(user.getId()).get(0)) != null) {
@@ -111,13 +121,23 @@ public List<User> getAllStudents() {
111121
}
112122
return users;
113123
}
124+
114125

115-
public List<User> getAllStudentsInCourse(Course course) {
116-
List<User> users = new ArrayList<>();
117-
for (String[] s : courseRepository.getAllStudents(course.getId())) {
118-
users.add(new User(Integer.valueOf(s[0]), s[1]));
126+
public List<Student> getAllStudentsInCourse(Course course) {
127+
try {
128+
List<String[]> studentsString = courseRepository.getAllStudents(course.getId());
129+
return studentsString.stream().map(x -> new Student(Integer.parseInt(x[0]), Integer.parseInt(x[0]))).collect(Collectors.toList());
130+
} catch (Exception e) {
131+
return null;
119132
}
120-
return users;
133+
}
134+
135+
public List<User> getAllUsersInCourse(Course course) {
136+
List<Student> students = getAllStudentsInCourse(course);
137+
if(students == null) {
138+
return null;
139+
}
140+
return students.stream().map(x -> getUserByStudent(x)).collect(Collectors.toList());
121141
}
122142

123143
public List<User> getCourseTeachers(Course course) {
@@ -209,7 +229,7 @@ public boolean addStudentToCourse(Course course, Student student) {
209229
}
210230

211231
public Student getStudentByID(int id) {
212-
return studentRepository.getById((long) id);
232+
return studentRepository.findById(id);
213233
}
214234

215235
public boolean addStudentsToCourse(Course course, Set<Student> students) {
@@ -272,7 +292,7 @@ public boolean deleteExercise(Exercise exercise) {
272292
}
273293
}
274294

275-
public Exercise getExerciseByID(int id) {
295+
public Exercise getExerciseByID(int id) {
276296
return exerciseRepository.findById(id);
277297
}
278298

src/main/java/io/github/Hattinger04/course/model/course/CourseRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public interface CourseRepository extends JpaRepository<Course, Long>{
1919
@Query(value = "SELECT user_id, username FROM USERS u JOIN TEACHER t using(user_id) JOIN user_course c using(user_id) where c.course_id=:course_id", nativeQuery = true)
2020
List<String[]> getCourseTeacher(int course_id);
2121

22-
@Query(value = "SELECT user_id, username FROM USERS u JOIN STUDENT s using(user_id) JOIN user_course c using(user_id) where c.course_id=:course_id", nativeQuery = true)
22+
@Query(value = "SELECT student_id, user_id FROM STUDENT s JOIN USERS u using(user_id) JOIN user_course c using(user_id) where c.course_id=:course_id", nativeQuery = true)
2323
List<String[]> getAllStudents(int course_id);
2424

2525
@Query(value = "SELECT EXISTS(SELECT user_id FROM USERS u JOIN STUDENT s using(user_id) JOIN user_course c using(user_id) where user_id=:user_id and c.course_id=:course_id)", nativeQuery = true)

src/main/java/io/github/Hattinger04/course/model/exercise/ExerciseRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ public interface ExerciseRepository extends JpaRepository<Exercise, Long>{
1010
Exercise findById(int id);
1111
// TODO: SQL not tested yet!
1212
@Query(value = "SELECT * FROM EXERCISE e JOIN course c USING (course_id) where course_id=:course_id and c.name=:name", nativeQuery = true)
13-
Exercise findByCourse(@Param("course_id") int course_id, @Param("name")String name);
13+
Exercise findByCourse(@Param("course_id") long course_id, @Param("name")String name);
1414
}

src/main/java/io/github/Hattinger04/course/model/solution/SolutionRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ public interface SolutionRepository extends JpaRepository<Solution, Long>{
1010
Solution findById(int id);
1111
// TODO: SQL not tested yet!
1212
@Query(value = "SELECT * FROM SOLUTION s JOIN exercise e USING (exercise_id) where exercise_id=:exercise_id and e.name=:name", nativeQuery = true)
13-
Solution findByExercise(@Param("exercise_id")int exercise_id, @Param("name")String name);
13+
Solution findByExercise(@Param("exercise_id")long exercise_id, @Param("name")String name);
1414
}

src/main/java/io/github/Hattinger04/course/model/student/Student.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,10 @@ public class Student {
3636
@ManyToOne
3737
@JoinColumn(name="user_id", nullable = false)
3838
private User user;
39+
40+
41+
public Student(int student_id, int user_id) {
42+
this.id = student_id;
43+
this.user = new User(user_id);
44+
}
3945
}

src/main/java/io/github/Hattinger04/course/model/student/StudentRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
public interface StudentRepository extends JpaRepository<Student, Long>{
1010
Student findById(int id);
1111
@Query(value = "SELECT * FROM STUDENT s JOIN USERS u USING (user_id) where u.user_id=:user_id", nativeQuery = true)
12-
List<Student> findByUserId(@Param("user_id") int user_id);
12+
List<Student> findByUserId(@Param("user_id") long user_id);
1313
@Query(value = "SELECT * FROM STUDENT s JOIN USERS u USING (user_id) where u.username=:username", nativeQuery = true)
1414
List<Student> findByName(@Param("username") String username);
1515
}

src/main/java/io/github/Hattinger04/course/model/teacher/TeacherRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
public interface TeacherRepository extends JpaRepository<Teacher, Long>{
88
Teacher findById(int id); @Query(value = "SELECT * FROM TEACHER t JOIN USERS u USING (user_id) where u.user_id=:user_id", nativeQuery = true)
9-
Teacher findByUserId(@Param("user_id") int user_id);
9+
Teacher findByUserId(@Param("user_id") long user_id);
1010
@Query(value = "SELECT * FROM TEACHER t JOIN USERS u USING (user_id) where u.username=:username", nativeQuery = true)
1111
Teacher findByName(@Param("username") String username);
1212

src/main/java/io/github/Hattinger04/user/UserController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public ResponseEntity<?> getUserByUsername(@RequestParam(name = "username", requ
6666
*/
6767
@GetMapping("/users/{id}")
6868
@PreAuthorize("hasAuthority('DEV')")
69-
public ResponseEntity<?> getUserById(@PathVariable int id) {
69+
public ResponseEntity<?> getUserById(@PathVariable long id) {
7070
User userFound = userService.findUserByID(id);
7171
if (userFound == null) {
7272
return new ResponseEntity<>(HttpStatus.NOT_FOUND);

src/main/java/io/github/Hattinger04/user/model/UserRepository.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ public interface UserRepository extends JpaRepository<User, Long> {
2121
@Modifying
2222
@Query(value = "Insert into user_role(user_id, role_id) VALUES (:user_id, :role_id)", nativeQuery = true)
2323
@Transactional
24-
public void insertUserRole(@Param("user_id") int user_id, @Param("role_id") int role_id);
24+
public void insertUserRole(@Param("user_id") long user_id, @Param("role_id") long role_id);
2525

2626
@Modifying
2727
@Query(value = "delete from user_role where user_id = :user_id and role_id=:role_id", nativeQuery = true)
2828
@Transactional
29-
public void removeUserRole(@Param("user_id") int user_id, @Param("role_id") int role_id);
29+
public void removeUserRole(@Param("user_id") long user_id, @Param("role_id") long role_id);
3030

3131
}

0 commit comments

Comments
 (0)