| Total Complexity | 12 |
| Total Lines | 93 |
| Duplicated Lines | 0 % |
| Changes | 10 | ||
| Bugs | 1 | Features | 0 |
| 1 | package easytests.api.v1.controllers; |
||
| 28 | @RestController("SubjectsControllerV1") |
||
| 29 | @SuppressWarnings("checkstyle:MultipleStringLiterals") |
||
| 30 | @RequestMapping("/v1/subjects") |
||
| 31 | public class SubjectsController extends AbstractController { |
||
| 32 | |||
| 33 | @Autowired |
||
| 34 | protected SubjectsServiceInterface subjectsService; |
||
| 35 | |||
| 36 | @Autowired |
||
| 37 | protected SubjectsOptionsBuilderInterface subjectsOptionsBuilder; |
||
| 38 | |||
| 39 | @Autowired |
||
| 40 | protected UsersServiceInterface usersService; |
||
| 41 | |||
| 42 | @Autowired |
||
| 43 | @Qualifier("SubjectsMapperV1") |
||
| 44 | private SubjectsMapper subjectsMapper; |
||
| 45 | |||
| 46 | @GetMapping("") |
||
| 47 | public List<Subject> list(@RequestParam(name = "userId", required = true) Integer userId) |
||
| 48 | throws NotFoundException, ForbiddenException { |
||
| 49 | final UserModelInterface userModel = this.usersService.find(userId); |
||
| 50 | |||
| 51 | if (userModel == null) { |
||
| 52 | throw new NotFoundException(); |
||
| 53 | } |
||
| 54 | if (!this.acl.hasAccess(userModel)) { |
||
| 55 | throw new ForbiddenException(); |
||
| 56 | } |
||
| 57 | |||
| 58 | final List<SubjectModelInterface> subjectsModels = this.subjectsService.findByUser(userModel); |
||
| 59 | |||
| 60 | return subjectsModels |
||
| 61 | .stream() |
||
| 62 | .map(model -> this.subjectsMapper.map(model, Subject.class)) |
||
| 63 | .collect(Collectors.toList()); |
||
| 64 | } |
||
| 65 | |||
| 66 | @PostMapping("") |
||
| 67 | @ResponseStatus(HttpStatus.CREATED) |
||
| 68 | public Identity create(@RequestBody Subject subject) throws BadRequestException, ForbiddenException { |
||
| 69 | if (subject.getId() != null) { |
||
| 70 | throw new IdentifiedModelException(); |
||
| 71 | } |
||
| 72 | |||
| 73 | this.checkUser(subject); |
||
| 74 | |||
| 75 | final SubjectModelInterface subjectModel = this.subjectsMapper.map(subject, SubjectModel.class); |
||
| 76 | subjectModel.setUser(usersService.find(subject.getUser().getId())); |
||
| 77 | |||
| 78 | this.subjectsService.save(subjectModel); |
||
| 79 | |||
| 80 | return this.subjectsMapper.map(subjectModel, Identity.class); |
||
| 81 | |||
| 82 | } |
||
| 83 | |||
| 84 | private void checkUser(Subject subject) throws BadRequestException, ForbiddenException { |
||
| 85 | final UserModelInterface userModel = this.usersService.find(subject.getUser().getId()); |
||
| 86 | |||
| 87 | if (!this.acl.hasAccess(userModel)) { |
||
| 88 | throw new BadRequestException(); |
||
| 89 | } |
||
| 90 | |||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * update |
||
| 95 | */ |
||
| 96 | /** |
||
| 97 | * show(subjectId) |
||
| 98 | */ |
||
| 99 | |||
| 100 | @GetMapping("/{subjectId}") |
||
| 101 | public Subject show(@PathVariable Integer subjectId) throws NotFoundException, ForbiddenException { |
||
| 102 | final SubjectModelInterface subjectModel = this.getSubjectModel(subjectId); |
||
| 103 | |||
| 104 | if (!this.acl.hasAccess(subjectModel)) { |
||
| 105 | throw new ForbiddenException(); |
||
| 106 | } |
||
| 107 | return this.subjectsMapper.map(subjectModel, Subject.class); |
||
| 108 | } |
||
| 109 | |||
| 110 | private SubjectModelInterface getSubjectModel(Integer id, SubjectsOptionsInterface subjectOptions) |
||
| 111 | throws NotFoundException { |
||
| 112 | final SubjectModelInterface subjectModel = this.subjectsService.find(id, subjectOptions); |
||
| 113 | if (subjectModel == null) { |
||
| 114 | throw new NotFoundException(); |
||
| 115 | } |
||
| 116 | return subjectModel; |
||
| 117 | } |
||
| 118 | |||
| 119 | private SubjectModelInterface getSubjectModel(Integer id) throws NotFoundException { |
||
| 120 | return this.getSubjectModel(id, this.subjectsOptionsBuilder.forAuth()); |
||
| 121 | } |
||
| 126 |