Completed
Pull Request — dev (#387)
by
unknown
07:43 queued 01:37
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.builder.IssuesOptionsBuilder;
11
import easytests.core.services.IssuesServiceInterface;
12
import easytests.core.services.SubjectsServiceInterface;
13
import java.util.List;
14
import java.util.stream.Collectors;
15
import org.springframework.beans.factory.annotation.Autowired;
16
import org.springframework.beans.factory.annotation.Qualifier;
17
import org.springframework.web.bind.annotation.*;
18
19
/**
20
 * @author Yarik2308
21
 */
22
@RestController("IssuesControllerV1")
23
@SuppressWarnings("checkstyle:MultipleStringLiterals")
24
@RequestMapping("/v1/issues")
25
public class IssuesController extends AbstractController {
26
27
    @Autowired
28
    protected IssuesServiceInterface issuesService;
29
30
    @Autowired
31
    protected IssuesOptionsBuilder issuesOptions;
32
33
    @Autowired
34
    protected SubjectsServiceInterface subjectsService;
35
36
    @Autowired
37
    @Qualifier("IssuesMapperV1")
38
    private IssuesMapper issuesMapper;
39
40
    @GetMapping
41
    public List<Issue> list(@RequestParam(name = "subjectId", required = true) Integer subjectId)
42
            throws NotFoundException, ForbiddenException {
43
        final SubjectModelInterface subjectModel = this.subjectsService.find(subjectId);
44
45
        if (subjectModel == null) {
46
            throw new NotFoundException();
47
        }
48
49
        if (!this.acl.hasAccess(subjectModel)) {
50
            throw new ForbiddenException();
51
        }
52
53
        final List<IssueModelInterface> issuesModels = this.issuesService.findBySubject(subjectModel);
54
55
        return issuesModels
56
                .stream()
57
                .map(model -> this.issuesMapper.map(model, Issue.class))
58
                .collect(Collectors.toList());
59
    }
60
    /**
61
     * create
62
     */
63
    /**
64
     * update
65
     */
66
    /**
67
     * show(issueId)
68
     */
69
    /**
70
     * delete(issueId)
71
     */
72
73
    @DeleteMapping("/{issueId}")
74
    public void delete(@PathVariable Integer issueId) throws NotFoundException, ForbiddenException {
75
        final IssueModelInterface issueModel = this.getIssueModel(issueId);
76
77
        if (issueModel == null) {
0 ignored issues
show
Bug introduced by
This condition always evaluates to false. Consider refactoring your code to no longer check for it or rewrite the condition.
Loading history...
78
            throw new NotFoundException();
79
        }
80
        if (!this.acl.hasAccess(issueModel)) {
81
            throw new ForbiddenException();
82
        }
83
        this.issuesService.delete(issueModel);
84
    }
85
86
    private IssueModelInterface getIssueModel(Integer id) throws NotFoundException {
87
        final IssuesOptionsInterface issuesOptionsInterface = this.issuesOptions.forDelete();
88
        return this.getIssueModel(id, issuesOptionsInterface);
89
    }
90
91
    private IssueModelInterface getIssueModel(Integer id, IssuesOptionsInterface issueOption)
92
            throws NotFoundException {
93
        final IssueModelInterface issueModel = this.issuesService.find(id, issueOption);
94
        if (issueModel == null) {
95
            throw new NotFoundException();
96
        }
97
        return issueModel;
98
    }
99
}
100