Skip to content

Commit 7e2c7ed

Browse files
author
Hattinger04
committed
request body now way better handled
1 parent a2b9142 commit 7e2c7ed

File tree

15 files changed

+155
-191
lines changed

15 files changed

+155
-191
lines changed

src/main/java/io/github/Hattinger04/RestServices.java

Lines changed: 0 additions & 57 deletions
This file was deleted.

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

Lines changed: 100 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@
77
import org.springframework.http.ResponseEntity;
88
import org.springframework.security.access.prepost.PreAuthorize;
99
import org.springframework.web.bind.annotation.DeleteMapping;
10+
import org.springframework.web.bind.annotation.GetMapping;
1011
import org.springframework.web.bind.annotation.PostMapping;
1112
import org.springframework.web.bind.annotation.PutMapping;
1213
import org.springframework.web.bind.annotation.RequestBody;
1314
import org.springframework.web.bind.annotation.RequestMapping;
1415
import org.springframework.web.bind.annotation.ResponseBody;
1516
import org.springframework.web.bind.annotation.RestController;
1617

17-
import io.github.Hattinger04.RestServices;
18+
import com.fasterxml.jackson.databind.JsonNode;
19+
import com.fasterxml.jackson.databind.ObjectMapper;
20+
1821
import io.github.Hattinger04.course.model.CourseService;
1922
import io.github.Hattinger04.course.model.course.Course;
2023
import io.github.Hattinger04.course.model.exercise.Exercise;
@@ -29,194 +32,210 @@ public class CourseController {
2932
@Autowired
3033
private CourseService courseService;
3134
@Autowired
32-
private RestServices restServices;
35+
private ObjectMapper mapper;
3336

34-
3537
/**
36-
* Get student by id in Course from database
37-
* Needs course and student object
38+
* Get student by id from database Needs student object
3839
*
3940
* @param json
4041
* @return
4142
*/
4243
@PreAuthorize("hasAuthority('TEACHER')")
4344
@PostMapping("/getStudent")
4445
@ResponseBody
45-
public ResponseEntity<?> getStudent(@RequestBody String json) {
46-
Student s = (Student) restServices.deserialize(Student.class, json);
47-
User student;
48-
if((student = courseService.getUserByStudent(s)) == null) {
46+
public ResponseEntity<?> getStudent(@RequestBody JsonNode node) {
47+
Student student = mapper.convertValue(node.get("student"), Student.class);
48+
if (courseService.getUserByStudent(student) == null) {
4949
return new ResponseEntity<>("Student not exisiting!", HttpStatus.NOT_FOUND);
5050
}
51-
return new ResponseEntity<>(student, HttpStatus.OK);
51+
return new ResponseEntity<>(student, HttpStatus.OK);
5252
}
53-
53+
5454
/**
55-
* Get all students in Course from database
56-
* Needs course object
55+
* Get all students in Course from database Needs course object
5756
*
5857
* @param json
5958
* @return
6059
*/
6160
@PreAuthorize("hasAuthority('TEACHER')")
6261
@PostMapping("/getAllStudents")
6362
@ResponseBody
64-
public ResponseEntity<?> getAllStudents(@RequestBody String json) {
65-
Course course = (Course) restServices.deserialize(Course.class, json);
66-
List<User> students;
67-
if((students = courseService.getAllStudents(course)) == null) {
63+
public ResponseEntity<?> getAllStudents(@RequestBody JsonNode node) {
64+
Course course = mapper.convertValue(node.get("course"), Course.class);
65+
List<User> students;
66+
if ((students = courseService.getAllStudents(course)) == null) {
6867
return new ResponseEntity<>("Course not existing or no users in course!", HttpStatus.NOT_FOUND);
6968
}
70-
return new ResponseEntity<>(students, HttpStatus.OK);
69+
return new ResponseEntity<>(students, HttpStatus.OK);
7170
}
72-
71+
7372
/**
74-
* get teacher from course from database
75-
* Needs course object
73+
* get teacher from course from database Needs course object
7674
*
7775
* @param json
7876
* @return
7977
*/
8078
@PreAuthorize("hasAuthority('TEACHER')")
8179
@PostMapping("/getCourseTeacher")
8280
@ResponseBody
83-
public ResponseEntity<?> getCourseTeacher(@RequestBody String json) {
84-
Course course = (Course) restServices.deserialize(Course.class, json);
85-
List<User> teachers;
86-
if((teachers = courseService.getCourseTeachers(course)) == null) {
81+
public ResponseEntity<?> getCourseTeacher(@RequestBody JsonNode node) {
82+
Course course = mapper.convertValue(node.get("course"), Course.class);
83+
List<User> teachers;
84+
if ((teachers = courseService.getCourseTeachers(course)) == null) {
8785
return new ResponseEntity<>("There is no teacher in this course - contact an admin!", HttpStatus.NOT_FOUND);
8886
}
89-
return new ResponseEntity<>(teachers, HttpStatus.OK);
87+
return new ResponseEntity<>(teachers, HttpStatus.OK);
9088
}
91-
89+
9290
/**
93-
* Create a new Course
94-
* Needs course and teacher object
91+
* Create a new Course Needs course and teacher object
9592
*
9693
* @param json
9794
* @return
9895
*/
9996
@PreAuthorize("hasAuthority('TEACHER')")
10097
@PutMapping("/createCourse")
10198
@ResponseBody
102-
public ResponseEntity<?> createCourse(@RequestBody String json) {
103-
Object[] objects;
104-
Course course;
105-
if((objects = restServices.deserializeMany(new Class[] {Course.class, Teacher.class}, json)) != null) {
106-
if((course = courseService.createCourse((Course) objects[0])) != null) {
107-
courseService.setCourseTeacher(course, (Teacher) objects[1]);
108-
return new ResponseEntity<>(HttpStatus.OK);
109-
}
110-
return new ResponseEntity<>("Could not create course!", HttpStatus.NOT_IMPLEMENTED);
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);
111105
}
112-
return new ResponseEntity<>("Wrong JSON format!", HttpStatus.NOT_IMPLEMENTED);
106+
return new ResponseEntity<>("Could not create course!", HttpStatus.NOT_IMPLEMENTED);
113107
}
114-
108+
115109
/**
116-
* Deletes a existing Course
117-
* Needs course object
110+
* Deletes a existing Course Needs course object
118111
*
119112
* @param json
120113
* @return
121114
*/
122115
@PreAuthorize("hasAuthority('TEACHER')")
123116
@DeleteMapping("/deleteCourse")
124117
@ResponseBody
125-
public ResponseEntity<?> deleteCourse(@RequestBody String json) {
126-
return courseService.deleteCourse((Course) restServices.deserialize(Course.class, json)) ?
127-
new ResponseEntity<>(HttpStatus.OK) : new ResponseEntity<>("Could not delete course!", HttpStatus.NOT_MODIFIED);
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);
128122
}
129123

130124
/**
131-
* Adds student to existing Course
132-
* Needs course object + student object
125+
* Adds student to existing Course Needs course object + student object
133126
*
134127
* @param json
135128
* @return
136129
*/
137130
@PreAuthorize("hasAuthority('TEACHER')")
138131
@PutMapping("/addStudentCourse")
139132
@ResponseBody
140-
public ResponseEntity<?> addStudentCourse(@RequestBody String json) {
141-
Object[] objects = restServices.deserializeMany(new Class[] {Course.class, Student.class}, json);
142-
return courseService.addStudentToCourse((Course) objects[0], (Student) objects[1]) ?
143-
new ResponseEntity<>(HttpStatus.OK) : new ResponseEntity<>("Could not add student to course!", HttpStatus.NOT_IMPLEMENTED);
133+
public ResponseEntity<?> addStudentCourse(@RequestBody JsonNode node) {
134+
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);
144138
}
145-
139+
146140
/**
147-
* Removes student from Course
148-
* Needs course object + student object
141+
* Removes student from Course Needs course object + student object
149142
*
150143
* @param json
151144
* @return
152145
*/
153146
@PreAuthorize("hasAuthority('TEACHER')")
154147
@DeleteMapping("/removeStudentCourse")
155148
@ResponseBody
156-
public ResponseEntity<?> removeStudentCourse(@RequestBody String json) {
157-
Object[] objects = restServices.deserializeMany(new Class[] {Course.class, Student.class}, json);
158-
return courseService.removeStudentFromCourse((Course) objects[0], (Student) objects[1]) ?
159-
new ResponseEntity<>(HttpStatus.OK) : new ResponseEntity<>("Could not remove student from course!", HttpStatus.NOT_MODIFIED);
149+
public ResponseEntity<?> removeStudentCourse(@RequestBody JsonNode node) {
150+
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);
160154
}
161155

162156
/**
163-
* Creates a new exercise in a existing Course
164-
* Needs course object + exercise object
157+
* Creates a new exercise in a existing Course Needs course object + exercise
158+
* object
165159
*
166160
* @param json
167161
* @return
168162
*/
169163
@PreAuthorize("hasAuthority('TEACHER')")
170164
@PutMapping("/createExercise")
171165
@ResponseBody
172-
public ResponseEntity<?> createExercise(@RequestBody String json) {
173-
return courseService.createExercise((Exercise) restServices.deserialize(Exercise.class, json)) != null ?
174-
new ResponseEntity<>(HttpStatus.OK) : new ResponseEntity<>("Could not create exercise!", HttpStatus.NOT_IMPLEMENTED);
166+
public ResponseEntity<?> createExercise(@RequestBody JsonNode node) {
167+
Exercise exercise = mapper.convertValue(node.get("exercise"), Exercise.class);
168+
return courseService.createExercise(exercise) != null ? new ResponseEntity<>(HttpStatus.OK)
169+
: new ResponseEntity<>("Could not create exercise!", HttpStatus.NOT_IMPLEMENTED);
175170
}
176-
171+
177172
/**
178-
* Changes already existing (and published) exercises
179-
* Needs course object + exercise object
173+
* Changes already existing (and published) exercises Needs course object +
174+
* exercise object
180175
*
181176
* @param json
182177
* @return
183178
*/
184179
@PreAuthorize("hasAuthority('TEACHER')")
185180
@PostMapping("/patchExercise")
186181
@ResponseBody
187-
public ResponseEntity<?> patchExercise(@RequestBody String json) {
188-
return courseService.createExercise((Exercise) restServices.deserialize(Exercise.class, json)) != null ?
189-
new ResponseEntity<>(HttpStatus.OK) : new ResponseEntity<>("Could not patch exercise!", HttpStatus.NOT_IMPLEMENTED);
182+
public ResponseEntity<?> patchExercise(@RequestBody JsonNode node) {
183+
Exercise exercise = mapper.convertValue(node.get("exericse"), Exercise.class);
184+
return courseService.createExercise(exercise) != null ? new ResponseEntity<>(HttpStatus.OK)
185+
: new ResponseEntity<>("Could not patch exercise!", HttpStatus.NOT_IMPLEMENTED);
190186
}
191-
187+
192188
/**
193-
* Deletes existing exercise
194-
* Needs course object + exercise object
189+
* Deletes existing exercise Needs course object + exercise object
195190
*
196191
* @param json
197192
* @return
198193
*/
199194
@PreAuthorize("hasAuthority('TEACHER')")
200195
@PutMapping("/deleteExercise")
201196
@ResponseBody
202-
public ResponseEntity<?> deleteExercise(@RequestBody String json) {
203-
return courseService.deleteExercise((Exercise) restServices.deserialize(Exercise.class, json)) ?
204-
new ResponseEntity<>(HttpStatus.OK) : new ResponseEntity<>("Could not delete exercise!", HttpStatus.NOT_IMPLEMENTED);
197+
public ResponseEntity<?> deleteExercise(@RequestBody JsonNode node) {
198+
Exercise exercise = mapper.convertValue(node.get("exercise"), Exercise.class);
199+
return courseService.deleteExercise(exercise) ? new ResponseEntity<>(HttpStatus.OK)
200+
: new ResponseEntity<>("Could not delete exercise!", HttpStatus.NOT_IMPLEMENTED);
205201
}
206-
202+
207203
/**
208-
* Gives a rating to an exercise for one student
209-
* Needs course object + exercise object + student object
204+
* Gives a rating to an exercise for one student Needs course object + exercise
205+
* object + student object
210206
*
211207
* @param json
212208
* @return
213209
*/
214210
@PreAuthorize("hasAuthority('TEACHER')")
215211
@PostMapping("/rateExercise")
216212
@ResponseBody
217-
public ResponseEntity<?> rateExercise(@RequestBody String json) {
218-
// TODO: rate exercise from student
219-
return new ResponseEntity<>(HttpStatus.OK);
213+
public ResponseEntity<?> rateExercise(@RequestBody JsonNode node) {
214+
// TODO: rate exercise from student
215+
return new ResponseEntity<>(HttpStatus.OK);
216+
}
217+
218+
// Not tested!
219+
/**
220+
* Service for checking if logged in user is in course
221+
* Needs course object
222+
*
223+
* @param json
224+
* @return
225+
*/
226+
@PreAuthorize("hasAuthority('TEACHER')")
227+
@GetMapping("/isUserInCourse")
228+
@ResponseBody
229+
public ResponseEntity<?> isUserInCourse(@RequestBody JsonNode node) {
230+
Course course = mapper.convertValue(node.get("course"), Course.class);
231+
if(course == null) {
232+
return new ResponseEntity<>("Course not found!", HttpStatus.NOT_FOUND);
233+
}
234+
User user = mapper.convertValue(node.get("user"), User.class);
235+
if(courseService.isUserInCourse(course, user)) {
236+
return new ResponseEntity<>(HttpStatus.OK);
237+
}
238+
return new ResponseEntity<>("User not in course!", HttpStatus.NOT_FOUND);
220239
}
221240

222241
}

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
import javax.persistence.ManyToMany;
1414
import javax.persistence.Table;
1515

16-
import com.fasterxml.jackson.annotation.JsonTypeInfo;
17-
import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
1816
import com.fasterxml.jackson.annotation.JsonTypeName;
1917

2018
import io.github.Hattinger04.user.model.User;
@@ -31,7 +29,6 @@
3129
@NoArgsConstructor
3230
@AllArgsConstructor
3331
@ToString
34-
@JsonTypeInfo(include=As.WRAPPER_OBJECT, use=com.fasterxml.jackson.annotation.JsonTypeInfo.Id.NAME, property="type")
3532
@JsonTypeName("course")
3633
public class Course {
3734

0 commit comments

Comments
 (0)