Completed
Pull Request — dev (#379)
by
unknown
04:51
created

show(Integer)   A

Complexity

Conditions 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 8
rs 10
cc 2
1
package easytests.api.v1.controllers;
2
3
import easytests.api.v1.exceptions.*;
4
import easytests.api.v1.exceptions.ForbiddenException;
5
import easytests.api.v1.exceptions.NotFoundException;
6
import easytests.api.v1.mappers.SubjectsMapper;
7
import easytests.api.v1.models.Identity;
8
import easytests.api.v1.models.Subject;
9
import easytests.core.models.SubjectModel;
10
import easytests.core.models.SubjectModelInterface;
11
import easytests.core.models.UserModelInterface;
12
import easytests.core.options.SubjectsOptionsInterface;
13
import easytests.core.options.builder.SubjectsOptionsBuilderInterface;
14
import easytests.core.services.SubjectsServiceInterface;
15
import easytests.core.services.UsersServiceInterface;
16
import java.util.List;
17
import java.util.stream.Collectors;
18
import org.springframework.beans.factory.annotation.Autowired;
19
import org.springframework.beans.factory.annotation.Qualifier;
20
import org.springframework.http.HttpStatus;
21
import org.springframework.web.bind.annotation.*;
22
import org.springframework.web.bind.annotation.GetMapping;
23
import org.springframework.web.bind.annotation.RequestParam;
24
25
/**
26
 * @author VeronikaRevjakina
27
 */
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
        if (subject.getUser() != null) {
86
            final UserModelInterface userModel = this.usersService.find(subject.getUser().getId());
87
88
            if (!this.acl.hasAccess(userModel)) {
89
                throw new ForbiddenException();
90
            }
91
        } else {
92
            throw new BadRequestException("user must exist");
93
        }
94
95
    }
96
    /**
97
     * update
98
     */
99
    /**
100
     * show(subjectId)
101
     */
102
103
    @GetMapping("/{subjectId}")
104
    public Subject show(@PathVariable Integer subjectId) throws NotFoundException, ForbiddenException {
105
        final SubjectModelInterface subjectModel = this.getSubjectModel(subjectId);
106
107
        if (!this.acl.hasAccess(subjectModel)) {
108
            throw new ForbiddenException();
109
        }
110
        return this.subjectsMapper.map(subjectModel, Subject.class);
111
    }
112
113
    private SubjectModelInterface getSubjectModel(Integer id, SubjectsOptionsInterface subjectOptions)
114
            throws NotFoundException {
115
        final SubjectModelInterface subjectModel = this.subjectsService.find(id, subjectOptions);
116
        if (subjectModel == null) {
117
            throw new NotFoundException();
118
        }
119
        return subjectModel;
120
    }
121
122
    private SubjectModelInterface getSubjectModel(Integer id) throws NotFoundException {
123
        return this.getSubjectModel(id, this.subjectsOptionsBuilder.forAuth());
124
    }
125
    /**
126
     * delete(subjectId)
127
     */
128
}
129