Passed
Pull Request — dev (#385)
by
unknown
04:25
created

getIssueModel(Integer)   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
1
package easytests.api.v1.controllers;
2
3
import easytests.api.v1.exceptions.ForbiddenException;
4
import easytests.api.v1.exceptions.NotFoundException;
5
import easytests.api.v1.mappers.IssuesMapper;
6
import easytests.api.v1.models.Issue;
7
import easytests.core.models.IssueModelInterface;
8
import easytests.core.models.SubjectModelInterface;
9
import easytests.core.options.IssuesOptionsInterface;
10
import easytests.core.options.SubjectsOptions;
11
import easytests.core.options.builder.IssuesOptionsBuilder;
12
import easytests.core.services.IssuesServiceInterface;
13
import easytests.core.services.SubjectsServiceInterface;
14
import java.util.List;
15
import java.util.stream.Collectors;
16
import org.springframework.beans.factory.annotation.Autowired;
17
import org.springframework.beans.factory.annotation.Qualifier;
18
import org.springframework.web.bind.annotation.*;
19
20
/**
21
 * @author Yarik2308
22
 */
23
@RestController("IssuesControllerV1")
24
@SuppressWarnings("checkstyle:MultipleStringLiterals")
25
@RequestMapping("/v1/issues")
26
public class IssuesController extends AbstractController {
27
28
    @Autowired
29
    protected IssuesServiceInterface issuesService;
30
31
    @Autowired
32
    protected IssuesOptionsBuilder issuesOptions;
33
34
    @Autowired
35
    protected SubjectsServiceInterface subjectsService;
36
37
    @Autowired
38
    @Qualifier("IssuesMapperV1")
39
    private IssuesMapper issuesMapper;
40
41
    @GetMapping
42
    public List<Issue> list(@RequestParam(name = "subjectId", required = true) Integer subjectId)
43
            throws NotFoundException, ForbiddenException {
44
        final SubjectModelInterface subjectModel = this.subjectsService.find(subjectId);
45
46
        if (subjectModel == null) {
47
            throw new NotFoundException();
48
        }
49
50
        if (!this.acl.hasAccess(subjectModel)) {
51
            throw new ForbiddenException();
52
        }
53
54
        final List<IssueModelInterface> issuesModels = this.issuesService.findBySubject(subjectModel);
55
56
        return issuesModels
57
                .stream()
58
                .map(model -> this.issuesMapper.map(model, Issue.class))
59
                .collect(Collectors.toList());
60
    }
61
    /**
62
     * create
63
     */
64
    /**
65
     * update
66
     */
67
    /**
68
     * show(issueId)
69
     */
70
71
    @GetMapping("/{issueId}")
72
    public Object show(@PathVariable Integer issueId) throws NotFoundException, ForbiddenException {
73
74
        final IssueModelInterface issueModel = this.getIssueModel(issueId);
75
76
        if (!this.acl.hasAccess(issueModel)) {
77
            throw new ForbiddenException();
78
        }
79
        return this.issuesMapper.map(issueModel, Issue.class);
80
    }
81
82
    private IssueModelInterface getIssueModel(Integer id) throws NotFoundException {
83
        final IssuesOptionsInterface issuesOptionsInterface = this.issuesOptions.forAuth().withSubject(new SubjectsOptions());
84
        return this.getIssueModel(id, issuesOptionsInterface);
85
    }
86
87
    private IssueModelInterface getIssueModel(Integer id, IssuesOptionsInterface issueOption)
88
            throws NotFoundException {
89
        final IssueModelInterface issueModel = this.issuesService.find(id, issueOption);
90
        if (issueModel == null) {
91
            throw new NotFoundException();
92
        }
93
        return issueModel;
94
    }
95
96
97
    /**
98
     * delete(issueId)
99
     */
100
101
}
102