1
0
forked from PGL/Clyde

cleaned all controllers

This commit is contained in:
2024-03-16 19:13:57 +01:00
parent 069466ef5f
commit 97b57b361d
16 changed files with 181 additions and 126 deletions

View File

@@ -9,10 +9,6 @@ import ovh.herisson.Clyde.Services.CourseService;
import ovh.herisson.Clyde.Services.TeacherCourseService;
import ovh.herisson.Clyde.Tables.Course;
import ovh.herisson.Clyde.Tables.Role;
import ovh.herisson.Clyde.Tables.TeacherCourse;
import ovh.herisson.Clyde.Tables.User;
import java.util.ArrayList;
import java.util.Map;
@RestController
@@ -36,13 +32,21 @@ public class CourseController {
if (authServ.getUserFromToken(token) == null)
return new UnauthorizedResponse<>(null);
return new ResponseEntity<>(courseServ.findById(id), HttpStatus.OK);
Course foundCourse = courseServ.findById(id);
if (foundCourse == null)
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
return new ResponseEntity<>(foundCourse, HttpStatus.OK);
}
@PostMapping("/course")
public ResponseEntity<Course> postCourse(@RequestHeader("Authorization") String token, @RequestBody Course course){
if (authServ.isNotSecretaryOrAdmin(token))
public ResponseEntity<Course> postCourse(@RequestHeader("Authorization") String token,
@RequestBody Course course)
{
if (authServ.IsNotIn(new Role[]{Role.Secretary,Role.Admin},token))
return new UnauthorizedResponse<>(null);
return new ResponseEntity<>(courseServ.save(course), HttpStatus.CREATED);
@@ -55,11 +59,15 @@ public class CourseController {
@PathVariable long id)
{
if (authServ.IsNotIn(new Role[]{Role.Admin,Role.Teacher,Role.Secretary}, token)){
if (authServ.IsNotIn(new Role[]{Role.Admin,Role.Teacher,Role.Secretary}, token))
return new UnauthorizedResponse<>(null);
}
return new ResponseEntity<>(courseServ.modifyData(id, updates, authServ.getUserFromToken(token).getRole()), HttpStatus.OK);
Course modifiedCourse = courseServ.modifyData(id,updates,authServ.getUserFromToken(token).getRole());
if (modifiedCourse == null)
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
return new ResponseEntity<>(modifiedCourse, HttpStatus.OK);
}
@PostMapping("/course/{id}")
@@ -67,14 +75,14 @@ public class CourseController {
@RequestBody Iterable<Long> teacherIds,
@PathVariable Long id)
{
if (authServ.IsNotIn(new Role[]{Role.Admin,Role.Secretary}, token))
return new UnauthorizedResponse<>(null);
if (!teacherCourseServ.saveAll(teacherIds,courseServ.findById(id)))
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
teacherCourseServ.saveAll(teacherIds,courseServ.findById(id));
return new ResponseEntity<>(HttpStatus.OK);
}
}