77import org .springframework .http .ResponseEntity ;
88import org .springframework .security .access .prepost .PreAuthorize ;
99import org .springframework .web .bind .annotation .DeleteMapping ;
10+ import org .springframework .web .bind .annotation .GetMapping ;
1011import org .springframework .web .bind .annotation .PostMapping ;
1112import org .springframework .web .bind .annotation .PutMapping ;
1213import org .springframework .web .bind .annotation .RequestBody ;
1314import org .springframework .web .bind .annotation .RequestMapping ;
1415import org .springframework .web .bind .annotation .ResponseBody ;
1516import 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+
1821import io .github .Hattinger04 .course .model .CourseService ;
1922import io .github .Hattinger04 .course .model .course .Course ;
2023import 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}
0 commit comments