2024-03-16 17:17:07 +01:00
|
|
|
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;
|
2024-03-17 03:06:19 +01:00
|
|
|
import ovh.herisson.Clyde.Services.ProtectionService;
|
2024-03-16 17:17:07 +01:00
|
|
|
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
|
|
|
|
2024-03-17 03:06:19 +01:00
|
|
|
import java.util.ArrayList;
|
2024-03-17 02:45:49 +01:00
|
|
|
import java.util.HashMap;
|
2024-03-16 17:17:07 +01:00
|
|
|
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){
|
2024-03-16 17:17:07 +01:00
|
|
|
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);
|
|
|
|
|
|
2024-03-17 03:06:19 +01:00
|
|
|
return new ResponseEntity<>(ProtectionService.courseWithoutPassword(foundCourse), HttpStatus.OK);
|
2024-03-16 17:17:07 +01:00
|
|
|
}
|
|
|
|
|
|
2024-03-17 02:15:08 +01:00
|
|
|
@GetMapping("/courses")
|
2024-03-17 03:06:19 +01:00
|
|
|
public ResponseEntity<Iterable<HashMap<String,Object>>> getAllCourses(@RequestHeader("Authorization") String token){
|
2024-03-17 02:15:08 +01:00
|
|
|
if (authServ.isNotIn(new Role[]{Role.Admin,Role.Secretary},token))
|
|
|
|
|
return new UnauthorizedResponse<>(null);
|
|
|
|
|
|
2024-03-17 03:06:19 +01:00
|
|
|
Iterable<Course> courses = courseServ.findAll();
|
|
|
|
|
ArrayList<HashMap<String,Object>> coursesWithoutPassword = new ArrayList<>();
|
2024-03-17 02:15:08 +01:00
|
|
|
|
2024-03-17 03:06:19 +01:00
|
|
|
for (Course course: courses){
|
|
|
|
|
coursesWithoutPassword.add(ProtectionService.courseWithoutPassword(course));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new ResponseEntity<>(coursesWithoutPassword,HttpStatus.OK);
|
2024-03-17 02:15:08 +01:00
|
|
|
}
|
|
|
|
|
|
2024-03-16 17:17:07 +01:00
|
|
|
|
|
|
|
|
@PostMapping("/course")
|
2024-03-17 03:06:19 +01:00
|
|
|
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))
|
2024-03-16 17:17:07 +01:00
|
|
|
return new UnauthorizedResponse<>(null);
|
|
|
|
|
|
2024-03-17 02:15:08 +01:00
|
|
|
Course createdCourse = courseServ.save(course);
|
|
|
|
|
if (createdCourse == null)
|
|
|
|
|
return new ResponseEntity<>(null,HttpStatus.BAD_REQUEST);
|
|
|
|
|
|
2024-03-17 03:06:19 +01:00
|
|
|
return new ResponseEntity<>(ProtectionService.courseWithoutPassword(createdCourse), HttpStatus.CREATED);
|
2024-03-16 17:17:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@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))
|
2024-03-16 17:17:07 +01:00
|
|
|
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);
|
2024-03-16 17:17:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@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))
|
2024-03-16 17:17:07 +01:00
|
|
|
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);
|
2024-03-16 17:17:07 +01:00
|
|
|
|
|
|
|
|
return new ResponseEntity<>(HttpStatus.OK);
|
|
|
|
|
}
|
|
|
|
|
}
|