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

list(Integer)   A

Complexity

Conditions 3

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

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