98 lines
4.2 KiB
Java
98 lines
4.2 KiB
Java
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.LessonService;
|
|
import ovh.herisson.Clyde.Services.ProtectionService;
|
|
import ovh.herisson.Clyde.Services.ScheduleLessonService;
|
|
import ovh.herisson.Clyde.Tables.Lesson;
|
|
import ovh.herisson.Clyde.Tables.Role;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
|
|
@RestController
|
|
@CrossOrigin(originPatterns = "*", allowCredentials = "true")
|
|
public class LessonController {
|
|
private final LessonService lessonServ;
|
|
|
|
private final ScheduleLessonService scheduleLessonServ;
|
|
private final AuthenticatorService authServ;
|
|
|
|
public LessonController(LessonService lessonServ, ScheduleLessonService scheduleLessonService, AuthenticatorService authServ) {
|
|
this.lessonServ = lessonServ;
|
|
this.scheduleLessonServ = scheduleLessonService;
|
|
this.authServ = authServ;
|
|
}
|
|
|
|
@GetMapping("/lesson/{id}")
|
|
public ResponseEntity<HashMap<String,Object>> getLesson(@PathVariable long id){
|
|
Lesson lesson = lessonServ.findById(id);
|
|
|
|
if(lesson == null)
|
|
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
|
|
|
|
return new ResponseEntity<>(ProtectionService.lessonWithoutPassword(lesson),HttpStatus.OK);
|
|
}
|
|
|
|
@GetMapping("/lessons")
|
|
public ResponseEntity<Iterable<HashMap<String,Object>>> getAllLessons(@RequestHeader("Authorization") String token){
|
|
if(authServ.isNotIn(new Role[]{Role.Admin,Role.Secretary},token))
|
|
return new UnauthorizedResponse<>(null);
|
|
return new ResponseEntity<>(ProtectionService.lessonsWithoutPassword(lessonServ.findAll()),HttpStatus.OK);
|
|
}
|
|
|
|
@GetMapping("/lessons/owned")
|
|
public ResponseEntity<Iterable<HashMap<String,Object>>> getOwnedLessons(@RequestHeader("Authorization") String token){
|
|
System.out.println(authServ);
|
|
if (authServ.isNotIn(new Role[]{Role.Admin,Role.Teacher},token)){
|
|
return new UnauthorizedResponse<>(null);}
|
|
return new ResponseEntity<>(ProtectionService.lessonsWithoutPassword(lessonServ.findAllOwnedLesson(authServ.getUserFromToken(token))),HttpStatus.OK);
|
|
}
|
|
|
|
|
|
@PostMapping("/lesson")
|
|
public ResponseEntity<HashMap<String, Object>> postLesson(@RequestHeader("Authorization") String token,
|
|
@RequestBody Map<String, Object> lessonInfos){
|
|
if(authServ.isNotIn(new Role[]{Role.Secretary,Role.Admin},token))
|
|
return new UnauthorizedResponse<>(null);
|
|
|
|
|
|
Lesson lesson = lessonServ.createLesson(lessonInfos);
|
|
Lesson createdLesson = lessonServ.save(lesson);
|
|
scheduleLessonServ.saveToAllSchedule(lesson);
|
|
|
|
if(createdLesson==null)
|
|
return new ResponseEntity<>(null,HttpStatus.BAD_REQUEST);
|
|
return new ResponseEntity<>(ProtectionService.lessonWithoutPassword(createdLesson), HttpStatus.OK);
|
|
}
|
|
|
|
@PatchMapping("/lesson/{id}")
|
|
public ResponseEntity<Lesson> patchLesson(@RequestHeader("Authorization") String token,
|
|
@RequestBody Map<String, Object> updates,
|
|
@PathVariable long id){
|
|
if(authServ.isNotIn(new Role[]{Role.Admin,Role.Secretary},token))
|
|
return new UnauthorizedResponse<>(null);
|
|
if(!lessonServ.modifyData(id, updates)){
|
|
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
|
|
}
|
|
return new ResponseEntity<>(HttpStatus.OK);
|
|
}
|
|
|
|
@DeleteMapping("lesson/{id}")
|
|
public ResponseEntity<String> deleteLesson(@RequestHeader("Authorization") String token,
|
|
@PathVariable Long id){
|
|
if (authServ.isNotIn(new Role[]{Role.Admin,Role.Secretary},token))
|
|
return new UnauthorizedResponse<>(null);
|
|
Lesson toDelete = lessonServ.findById(id);
|
|
if(toDelete == null)
|
|
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
|
|
lessonServ.delete(toDelete);
|
|
return new ResponseEntity<>(HttpStatus.OK);
|
|
|
|
}
|
|
}
|