1
0
forked from PGL/Clyde
Files
Clyde/backend/src/main/java/ovh/herisson/Clyde/EndPoints/CourseController.java

112 lines
4.1 KiB
Java
Raw Normal View History

package ovh.herisson.Clyde.EndPoints;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import ovh.herisson.Clyde.Responses.UnauthorizedResponse;
import ovh.herisson.Clyde.Services.AuthenticatorService;
import ovh.herisson.Clyde.Services.CourseService;
import ovh.herisson.Clyde.Services.ProtectionService;
import ovh.herisson.Clyde.Services.TeacherCourseService;
import ovh.herisson.Clyde.Tables.Course;
import ovh.herisson.Clyde.Tables.Role;
2024-03-17 02:45:49 +01:00
import java.util.ArrayList;
2024-03-17 02:45:49 +01:00
import java.util.HashMap;
import java.util.Map;
@RestController
@CrossOrigin(originPatterns = "*", allowCredentials = "true")
public class CourseController {
private final CourseService courseServ;
private final TeacherCourseService teacherCourseServ;
private final AuthenticatorService authServ;
public CourseController(CourseService courseServ, TeacherCourseService teacherCourseServ, AuthenticatorService authServ) {
this.courseServ = courseServ;
this.teacherCourseServ = teacherCourseServ;
this.authServ = authServ;
}
@GetMapping("/course/{id}")
2024-03-17 02:45:49 +01:00
public ResponseEntity<HashMap<String,Object>> getCourse(@RequestHeader("Authorization") String token, @PathVariable long id){
if (authServ.getUserFromToken(token) == null)
return new UnauthorizedResponse<>(null);
2024-03-16 19:13:57 +01:00
Course foundCourse = courseServ.findById(id);
if (foundCourse == null)
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
return new ResponseEntity<>(ProtectionService.courseWithoutPassword(foundCourse), HttpStatus.OK);
}
@GetMapping("/courses")
public ResponseEntity<Iterable<HashMap<String,Object>>> getAllCourses(@RequestHeader("Authorization") String token){
if (authServ.isNotIn(new Role[]{Role.Admin,Role.Secretary},token))
return new UnauthorizedResponse<>(null);
Iterable<Course> courses = courseServ.findAll();
ArrayList<HashMap<String,Object>> coursesWithoutPassword = new ArrayList<>();
for (Course course: courses){
coursesWithoutPassword.add(ProtectionService.courseWithoutPassword(course));
}
return new ResponseEntity<>(coursesWithoutPassword,HttpStatus.OK);
}
@PostMapping("/course")
public ResponseEntity<Map<String ,Object>> postCourse(@RequestHeader("Authorization") String token,
2024-03-16 19:13:57 +01:00
@RequestBody Course course)
{
2024-03-16 20:25:35 +01:00
if (authServ.isNotIn(new Role[]{Role.Secretary,Role.Admin},token))
return new UnauthorizedResponse<>(null);
Course createdCourse = courseServ.save(course);
if (createdCourse == null)
return new ResponseEntity<>(null,HttpStatus.BAD_REQUEST);
return new ResponseEntity<>(ProtectionService.courseWithoutPassword(createdCourse), HttpStatus.CREATED);
}
@PatchMapping("/course/{id}")
public ResponseEntity<Course> patchCourse(@RequestHeader("Authorization") String token,
@RequestBody Map<String,Object> updates,
@PathVariable long id)
{
2024-03-16 20:25:35 +01:00
if (authServ.isNotIn(new Role[]{Role.Admin,Role.Teacher,Role.Secretary}, token))
return new UnauthorizedResponse<>(null);
2024-03-16 19:13:57 +01:00
2024-03-16 20:25:35 +01:00
if (!courseServ.modifyData(id, updates, authServ.getUserFromToken(token).getRole()))
2024-03-16 19:13:57 +01:00
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
2024-03-16 20:25:35 +01:00
return new ResponseEntity<>(HttpStatus.OK);
}
@PostMapping("/course/{id}")
public ResponseEntity<String> postTeachers(@RequestHeader("Authorization") String token,
@RequestBody Iterable<Long> teacherIds,
@PathVariable Long id)
{
2024-03-16 19:13:57 +01:00
2024-03-16 20:25:35 +01:00
if (authServ.isNotIn(new Role[]{Role.Admin,Role.Secretary}, token))
return new UnauthorizedResponse<>(null);
2024-03-16 19:13:57 +01:00
if (!teacherCourseServ.saveAll(teacherIds,courseServ.findById(id)))
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
return new ResponseEntity<>(HttpStatus.OK);
}
}