- , принадлежащий пользователю
Код: Выделить всё
Project - Каждый проект имеет много экзаменов
- Каждый экзамен имеет много вопросов
[*]
Код: Выделить всё
GET /projects[*]
Код: Выделить всё
GET /projects/{projectId}/examsКод: Выделить всё
DELETE /projects/{projectId}/exams/{examId}Код: Выделить всё
@RestController
@RequestMapping("/projects")
public class ProjectController {
private final ProjectService projectService;
public ProjectController(ProjectService projectService) {
this.projectService = projectService;
}
@GetMapping
public ResponseEntity getAll(@AuthenticationPrincipal UserPrincipal principal) {
return ResponseEntity.ok(projectService.getAllByUser(principal.getUser()));
}
@DeleteMapping("/{projectId}")
public ResponseEntity delete(@PathVariable Integer projectId, @AuthenticationPrincipal UserPrincipal principal) {
projectService.delete(projectId, principal.getUser());
return ResponseEntity.ok().build();
}
}
Код: Выделить всё
@RestController
@RequestMapping("/projects/{projectId}/exams")
public class ExamController {
private final ExamService examService;
public ExamController(ExamService examService) {
this.examService = examService;
}
@GetMapping
public ResponseEntity getAll(
@PathVariable Integer projectId,
@AuthenticationPrincipal UserPrincipal principal
) {
return ResponseEntity.ok(examService.getAllByProject(projectId, principal.getUser()));
}
@DeleteMapping("/{examId}")
public ResponseEntity delete(@PathVariable Integer examId) {
examService.delete(examId);
return ResponseEntity.ok().build();
}
}
< /code>
examservice (проверка владения внутри): < /em>
Эта проверка владения кажется очень неловким.public Iterable getAllByProject(Integer projectId, User user) {
Optional project = projectRepository.findAllByOwnerAndId(user, projectId);
if (project.isEmpty()) {
throw new ProjectNotFoundException(projectId);
}
return project.get().getExams();
}
Подробнее здесь: https://stackoverflow.com/questions/797 ... wt-authent