Passed
Pull Request — dev (#397)
by Konstantin
04:05
created

show(Integer)   A

Complexity

Conditions 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 9
c 1
b 0
f 0
rs 9.95
cc 2
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 issuesOptionsBuilder;
32
33
    @Autowired
34
    protected SubjectsServiceInterface subjectsService;
35
36
    @Autowired
37
    @Qualifier("IssuesMapperV1")
38
    private IssuesMapper issuesMapper;
39
40
    /**
41
     * list
42
     */
43
    @GetMapping
44
    public List<Issue> list(@RequestParam(name = "subjectId", required = true) Integer subjectId)
45
            throws NotFoundException, ForbiddenException {
46
        final SubjectModelInterface subjectModel = this.subjectsService.find(subjectId);
47
48
        if (subjectModel == null) {
49
            throw new NotFoundException();
50
        }
51
52
        if (!this.acl.hasAccess(subjectModel)) {
53
            throw new ForbiddenException();
54
        }
55
56
        final List<IssueModelInterface> issuesModels = this.issuesService.findBySubject(subjectModel);
57
58
        return issuesModels
59
                .stream()
60
                .map(model -> this.issuesMapper.map(model, Issue.class))
61
                .collect(Collectors.toList());
62
    }
63
    /**
64
     * create
65
     */
66
    /**
67
     * update
68
     */
69
70
    /**
71
     * show(issueId)
72
     */
73
    @GetMapping("/{issueId}")
74
    public Object show(@PathVariable Integer issueId) throws NotFoundException, ForbiddenException {
75
76
        final IssueModelInterface issueModel = this.getIssueModel(issueId);
77
78
        if (!this.acl.hasAccess(issueModel)) {
79
            throw new ForbiddenException();
80
        }
81
        return this.issuesMapper.map(issueModel, Issue.class);
82
    }
83
84
85
    /**
86
     * delete(issueId)
87
     */
88
    @DeleteMapping("/{issueId}")
89
    public void delete(@PathVariable Integer issueId) throws NotFoundException, ForbiddenException {
90
        IssueModelInterface issueModel = this.getIssueModel(issueId);
91
92
        if (!this.acl.hasAccess(issueModel)) {
93
            throw new ForbiddenException();
94
        }
95
        final IssuesOptionsInterface issuesOptions = this.issuesOptionsBuilder.forDelete();
96
        issueModel = this.issuesService.find(issueId, issuesOptions);
97
        this.issuesService.delete(issueModel, issuesOptions);
98
    }
99
100
    private IssueModelInterface getIssueModel(Integer id) throws NotFoundException {
101
        final IssuesOptionsInterface issuesOptionsInterface = this.issuesOptionsBuilder.forAuth();
102
        return this.getIssueModel(id, issuesOptionsInterface);
103
    }
104
105
    private IssueModelInterface getIssueModel(Integer id, IssuesOptionsInterface issueOption)
106
            throws NotFoundException {
107
        final IssueModelInterface issueModel = this.issuesService.find(id, issueOption);
108
        if (issueModel == null) {
109
            throw new NotFoundException();
110
        }
111
        return issueModel;
112
    }
113
}
114