Skip to content

Commit 5633cd9

Browse files
author
Hattinger04
committed
reworking rest services (creating student missing!)
1 parent 06fc4fd commit 5633cd9

File tree

4 files changed

+268
-146
lines changed

4 files changed

+268
-146
lines changed

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

Lines changed: 160 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88
import org.springframework.security.access.prepost.PreAuthorize;
99
import org.springframework.web.bind.annotation.DeleteMapping;
1010
import org.springframework.web.bind.annotation.GetMapping;
11+
import org.springframework.web.bind.annotation.PathVariable;
1112
import org.springframework.web.bind.annotation.PostMapping;
1213
import org.springframework.web.bind.annotation.PutMapping;
1314
import org.springframework.web.bind.annotation.RequestBody;
1415
import org.springframework.web.bind.annotation.RequestMapping;
15-
import org.springframework.web.bind.annotation.ResponseBody;
16+
import org.springframework.web.bind.annotation.RequestParam;
1617
import org.springframework.web.bind.annotation.RestController;
1718

1819
import com.fasterxml.jackson.databind.JsonNode;
@@ -33,136 +34,238 @@ public class CourseController {
3334
private CourseService courseService;
3435
@Autowired
3536
private ObjectMapper mapper;
37+
38+
// TODO: check if teacher is in course when making rest request!
39+
40+
/**
41+
* Get student by id from database Needs as @PathVariable student_id
42+
*
43+
* @param json
44+
* @return
45+
*/
46+
@GetMapping("/students")
47+
@PreAuthorize("hasAuthority('DEV')")
48+
public ResponseEntity<?> getStudents() {
49+
List<User> students = courseService.getAllStudents();
50+
return new ResponseEntity<>(students, HttpStatus.OK);
51+
}
52+
53+
54+
/**
55+
* Get student by id from database Needs as @PathVariable student_id
56+
*
57+
* @param json
58+
* @return
59+
*/
60+
@GetMapping("/students/{id}")
61+
@PreAuthorize("hasAuthority('DEV')")
62+
public ResponseEntity<?> getStudentByID(@PathVariable int id) {
63+
Student student = courseService.getStudentByID(id);
64+
if (student == null || courseService.getUserByStudent(student) == null) {
65+
return new ResponseEntity<>("Student not exisiting!", HttpStatus.NOT_FOUND);
66+
}
67+
return new ResponseEntity<>(student, HttpStatus.OK);
68+
}
69+
3670

3771
/**
38-
* Get student by id from database Needs student object
72+
* Get student from course by id from database
73+
* Needs as @PathVariable student_id and courseid
3974
*
4075
* @param json
4176
* @return
4277
*/
78+
@GetMapping("/course/{courseid}/students/{studentsid}")
4379
@PreAuthorize("hasAuthority('TEACHER')")
44-
@PostMapping("/getStudent")
45-
@ResponseBody
46-
public ResponseEntity<?> getStudent(@RequestBody JsonNode node) {
47-
Student student = mapper.convertValue(node.get("student"), Student.class);
48-
if (courseService.getUserByStudent(student) == null) {
80+
public ResponseEntity<?> getStudentInCourseByCourseID(@PathVariable int courseid, @PathVariable int studentid) {
81+
Student student = courseService.getStudentByID(studentid);
82+
Course course = courseService.getCourseByID(studentid);
83+
User user = courseService.getUserByStudent(student);
84+
if (student == null || user == null || courseService.getUserByStudent(student) == null || courseService.isUserInCourse(course, user)) {
4985
return new ResponseEntity<>("Student not exisiting!", HttpStatus.NOT_FOUND);
5086
}
51-
return new ResponseEntity<>(student, HttpStatus.OK);
87+
return new ResponseEntity<>(user, HttpStatus.OK);
5288
}
5389

90+
5491
/**
55-
* Get all students in Course from database Needs course object
92+
* Get all students in Course from database Needs as @PathVariable course_id
5693
*
5794
* @param json
5895
* @return
5996
*/
97+
@GetMapping("/course/{id}/students")
6098
@PreAuthorize("hasAuthority('TEACHER')")
61-
@PostMapping("/getAllStudents")
62-
@ResponseBody
63-
public ResponseEntity<?> getAllStudents(@RequestBody JsonNode node) {
64-
Course course = mapper.convertValue(node.get("course"), Course.class);
99+
public ResponseEntity<?> getAllStudentsByCourseID(@PathVariable int id) {
100+
Course course = courseService.getCourseByID(id);
65101
List<User> students;
66-
if ((students = courseService.getAllStudents(course)) == null) {
102+
if ((students = courseService.getAllStudentsInCourse(course)) == null) {
103+
return new ResponseEntity<>("Course not existing or no users in course!", HttpStatus.NOT_FOUND);
104+
}
105+
return new ResponseEntity<>(students, HttpStatus.OK);
106+
}
107+
108+
/**
109+
* Get all students in Course from database Needs as @RequestParam coursename
110+
*
111+
* @param json
112+
* @return
113+
*/
114+
@GetMapping("/course/students")
115+
@PreAuthorize("hasAuthority('TEACHER')")
116+
public ResponseEntity<?> getAllStudentsByCourseName(@RequestParam(name = "coursename", required = false) String coursename) {
117+
Course course = courseService.getCourseByName(coursename);
118+
List<User> students;
119+
if ((students = courseService.getAllStudentsInCourse(course)) == null) {
67120
return new ResponseEntity<>("Course not existing or no users in course!", HttpStatus.NOT_FOUND);
68121
}
69122
return new ResponseEntity<>(students, HttpStatus.OK);
70123
}
71124

72125
/**
73-
* get teacher from course from database Needs course object
126+
* Adds student to existing Course Needs course object + student object
74127
*
75128
* @param json
76129
* @return
77130
*/
131+
@PostMapping("/course/students")
78132
@PreAuthorize("hasAuthority('TEACHER')")
79-
@PostMapping("/getCourseTeacher")
80-
@ResponseBody
81-
public ResponseEntity<?> getCourseTeacher(@RequestBody JsonNode node) {
133+
public ResponseEntity<?> addStudentCourse(@RequestBody JsonNode node) {
82134
Course course = mapper.convertValue(node.get("course"), Course.class);
135+
Student student = mapper.convertValue(node.get("student"), Student.class);
136+
return courseService.addStudentToCourse(course, student) ? new ResponseEntity<>(HttpStatus.OK)
137+
: new ResponseEntity<>("Could not add student to course!", HttpStatus.NOT_IMPLEMENTED);
138+
}
139+
140+
/**
141+
* Removes student from Course Needs course object + student object
142+
*
143+
* @param json
144+
* @return
145+
*/
146+
@DeleteMapping("/course/students")
147+
@PreAuthorize("hasAuthority('TEACHER')")
148+
public ResponseEntity<?> removeStudentCourse(@RequestBody JsonNode node) {
149+
Course course = mapper.convertValue(node.get("course"), Course.class);
150+
Student student = mapper.convertValue(node.get("student"), Student.class);
151+
return courseService.removeStudentFromCourse(course, student) ? new ResponseEntity<>(HttpStatus.OK)
152+
: new ResponseEntity<>("Could not remove student from course!", HttpStatus.NOT_MODIFIED);
153+
}
154+
155+
/**
156+
* Get teacher from course from database Needs as @PathVariable course_id
157+
*
158+
* @param json
159+
* @return
160+
*/
161+
@GetMapping("/course/{id}/teachers")
162+
@PreAuthorize("hasAuthority('TEACHER')")
163+
public ResponseEntity<?> getCourseTeacherByCourseID(@PathVariable int id) {
164+
Course course = courseService.getCourseByID(id);
83165
List<User> teachers;
84166
if ((teachers = courseService.getCourseTeachers(course)) == null) {
85167
return new ResponseEntity<>("There is no teacher in this course - contact an admin!", HttpStatus.NOT_FOUND);
86168
}
87169
return new ResponseEntity<>(teachers, HttpStatus.OK);
88170
}
89171

172+
90173
/**
91-
* Create a new Course Needs course and teacher object
174+
* get teacher from course from database Needs as @RequestParam coursename
92175
*
93176
* @param json
94177
* @return
95178
*/
179+
@GetMapping("/course/teachers")
96180
@PreAuthorize("hasAuthority('TEACHER')")
97-
@PutMapping("/createCourse")
98-
@ResponseBody
99-
public ResponseEntity<?> createCourse(@RequestBody JsonNode node) {
100-
Course course = mapper.convertValue(node.get("course"), Course.class);
101-
Teacher teacher = mapper.convertValue(node.get("teacher"), Teacher.class);
102-
if (courseService.createCourse(course) != null) {
103-
courseService.setCourseTeacher(course, teacher);
104-
return new ResponseEntity<>(HttpStatus.OK);
181+
public ResponseEntity<?> getCourseTeacherByCourseName(@RequestParam(name = "coursename", required = false) String coursename) {
182+
Course course = courseService.getCourseByName(coursename);
183+
List<User> teachers;
184+
if ((teachers = courseService.getCourseTeachers(course)) == null) {
185+
return new ResponseEntity<>("There is no teacher in this course - contact an admin!", HttpStatus.NOT_FOUND);
105186
}
106-
return new ResponseEntity<>("Could not create course!", HttpStatus.NOT_IMPLEMENTED);
187+
return new ResponseEntity<>(teachers, HttpStatus.OK);
107188
}
108189

109190
/**
110-
* Deletes a existing Course Needs course object
191+
* Get course from database Needs as @PathVariable course_id
111192
*
112193
* @param json
113194
* @return
114195
*/
196+
@GetMapping("/course/{id}")
115197
@PreAuthorize("hasAuthority('TEACHER')")
116-
@DeleteMapping("/deleteCourse")
117-
@ResponseBody
118-
public ResponseEntity<?> deleteCourse(@RequestBody JsonNode node) {
119-
Course course = mapper.convertValue(node.get("course"), Course.class);
120-
return courseService.deleteCourse(course) ? new ResponseEntity<>(HttpStatus.OK)
121-
: new ResponseEntity<>("Could not delete course!", HttpStatus.NOT_MODIFIED);
198+
public ResponseEntity<?> getCourseByID(@PathVariable int id) {
199+
Course course = courseService.getCourseByID(id);
200+
if (course == null) {
201+
return new ResponseEntity<>("There is no course with this name!", HttpStatus.NOT_FOUND);
202+
}
203+
return new ResponseEntity<>(course, HttpStatus.OK);
122204
}
123205

206+
124207
/**
125-
* Adds student to existing Course Needs course object + student object
208+
* Returns all or one course
126209
*
127210
* @param json
128211
* @return
129212
*/
213+
@GetMapping("/course")
130214
@PreAuthorize("hasAuthority('TEACHER')")
131-
@PutMapping("/addStudentCourse")
132-
@ResponseBody
133-
public ResponseEntity<?> addStudentCourse(@RequestBody JsonNode node) {
215+
public ResponseEntity<?> getCourseByCoursename(@RequestParam(name = "coursename", required = false) String coursename) {
216+
if(coursename == null) {
217+
return new ResponseEntity<>(courseService.getAllCourses(), HttpStatus.OK);
218+
}
219+
Course course = courseService.getCourseByName(coursename);
220+
if(course == null) {
221+
return new ResponseEntity<>("There is no course with this name!", HttpStatus.NOT_FOUND);
222+
}
223+
return new ResponseEntity<>(course, HttpStatus.OK);
224+
}
225+
226+
/**
227+
* Create a new Course Needs course and teacher object
228+
*
229+
* @param json
230+
* @return
231+
*/
232+
@PostMapping("/course")
233+
@PreAuthorize("hasAuthority('TEACHER')")
234+
public ResponseEntity<?> createCourse(@RequestBody JsonNode node) {
134235
Course course = mapper.convertValue(node.get("course"), Course.class);
135-
Student student = mapper.convertValue(node.get("student"), Student.class);
136-
return courseService.addStudentToCourse(course, student) ? new ResponseEntity<>(HttpStatus.OK)
137-
: new ResponseEntity<>("Could not add student to course!", HttpStatus.NOT_IMPLEMENTED);
236+
Teacher teacher = mapper.convertValue(node.get("teacher"), Teacher.class);
237+
if (courseService.createCourse(course) != null) {
238+
courseService.setCourseTeacher(course, teacher);
239+
return new ResponseEntity<>(HttpStatus.OK);
240+
}
241+
return new ResponseEntity<>("Could not create course!", HttpStatus.NOT_IMPLEMENTED);
138242
}
139243

140244
/**
141-
* Removes student from Course Needs course object + student object
245+
* Deletes a existing Course Needs course object
142246
*
143247
* @param json
144248
* @return
145249
*/
250+
@DeleteMapping("/course")
146251
@PreAuthorize("hasAuthority('TEACHER')")
147-
@DeleteMapping("/removeStudentCourse")
148-
@ResponseBody
149-
public ResponseEntity<?> removeStudentCourse(@RequestBody JsonNode node) {
252+
public ResponseEntity<?> deleteCourse(@RequestBody JsonNode node) {
150253
Course course = mapper.convertValue(node.get("course"), Course.class);
151-
Student student = mapper.convertValue(node.get("student"), Student.class);
152-
return courseService.removeStudentFromCourse(course, student) ? new ResponseEntity<>(HttpStatus.OK)
153-
: new ResponseEntity<>("Could not remove student from course!", HttpStatus.NOT_MODIFIED);
254+
return courseService.deleteCourse(course) ? new ResponseEntity<>(HttpStatus.OK)
255+
: new ResponseEntity<>("Could not delete course!", HttpStatus.NOT_MODIFIED);
154256
}
155257

258+
259+
156260
/**
157261
* Creates a new exercise in a existing Course Needs course object + exercise
158262
* object
159263
*
160264
* @param json
161265
* @return
162266
*/
267+
@PostMapping("/exercises")
163268
@PreAuthorize("hasAuthority('TEACHER')")
164-
@PutMapping("/createExercise")
165-
@ResponseBody
166269
public ResponseEntity<?> createExercise(@RequestBody JsonNode node) {
167270
Exercise exercise = mapper.convertValue(node.get("exercise"), Exercise.class);
168271
return courseService.createExercise(exercise) != null ? new ResponseEntity<>(HttpStatus.OK)
@@ -176,9 +279,8 @@ public ResponseEntity<?> createExercise(@RequestBody JsonNode node) {
176279
* @param json
177280
* @return
178281
*/
282+
@PutMapping("/exercises")
179283
@PreAuthorize("hasAuthority('TEACHER')")
180-
@PostMapping("/patchExercise")
181-
@ResponseBody
182284
public ResponseEntity<?> patchExercise(@RequestBody JsonNode node) {
183285
Exercise exercise = mapper.convertValue(node.get("exericse"), Exercise.class);
184286
return courseService.createExercise(exercise) != null ? new ResponseEntity<>(HttpStatus.OK)
@@ -191,9 +293,8 @@ public ResponseEntity<?> patchExercise(@RequestBody JsonNode node) {
191293
* @param json
192294
* @return
193295
*/
296+
@DeleteMapping("/deleteExercise")
194297
@PreAuthorize("hasAuthority('TEACHER')")
195-
@PutMapping("/deleteExercise")
196-
@ResponseBody
197298
public ResponseEntity<?> deleteExercise(@RequestBody JsonNode node) {
198299
Exercise exercise = mapper.convertValue(node.get("exercise"), Exercise.class);
199300
return courseService.deleteExercise(exercise) ? new ResponseEntity<>(HttpStatus.OK)
@@ -207,14 +308,15 @@ public ResponseEntity<?> deleteExercise(@RequestBody JsonNode node) {
207308
* @param json
208309
* @return
209310
*/
210-
@PreAuthorize("hasAuthority('TEACHER')")
311+
// TODO: change url!
211312
@PostMapping("/rateExercise")
212-
@ResponseBody
313+
@PreAuthorize("hasAuthority('TEACHER')")
213314
public ResponseEntity<?> rateExercise(@RequestBody JsonNode node) {
214315
// TODO: rate exercise from student
215316
return new ResponseEntity<>(HttpStatus.OK);
216317
}
217318

319+
// TODO: change url!
218320
// Not tested!
219321
/**
220322
* Service for checking if logged in user is in course
@@ -223,9 +325,8 @@ public ResponseEntity<?> rateExercise(@RequestBody JsonNode node) {
223325
* @param json
224326
* @return
225327
*/
328+
@PostMapping("/isUserInCourse")
226329
@PreAuthorize("hasAuthority('TEACHER')")
227-
@GetMapping("/isUserInCourse")
228-
@ResponseBody
229330
public ResponseEntity<?> isUserInCourse(@RequestBody JsonNode node) {
230331
Course course = mapper.convertValue(node.get("course"), Course.class);
231332
if(course == null) {

0 commit comments

Comments
 (0)