76 lines
3.3 KiB
Java
76 lines
3.3 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.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 AuthenticatorService authServ;
|
|
|
|
public LessonController(LessonService lessonServ, AuthenticatorService authServ) {
|
|
this.lessonServ = lessonServ;
|
|
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){
|
|
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 Lesson lesson){
|
|
if(authServ.isNotIn(new Role[]{Role.Secretary,Role.Admin},token))
|
|
return new UnauthorizedResponse<>(null);
|
|
Lesson createdLesson = lessonServ.save(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, authServ.getUserFromToken(token).getRole()))
|
|
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
|
|
return new ResponseEntity<>(HttpStatus.OK);
|
|
}
|
|
}
|