Completed
Pull Request — dev (#387)
by
unknown
09:54 queued 05:14
created

getIssueModel

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 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.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
    @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
        IssueModelInterface issueModel = this.getIssueModel(issueId);
76
77
        if (!this.acl.hasAccess(issueModel)) {
78
            throw new ForbiddenException();
79
        }
80
        final IssuesOptionsInterface issuesOptions = this.issuesOptionsBuilder.forDelete();
81
        issueModel = this.issuesService.find(issueId, issuesOptions);
82
        this.issuesService.delete(issueModel, issuesOptions);
83
    }
84
85
    private IssueModelInterface getIssueModel(Integer id) throws NotFoundException {
86
        final IssuesOptionsInterface issuesOptionsInterface = this.issuesOptionsBuilder.forAuth();
87
        return this.getIssueModel(id, issuesOptionsInterface);
88
    }
89
90
    private IssueModelInterface getIssueModel(Integer id, IssuesOptionsInterface issueOption)
91
            throws NotFoundException {
92
        final IssueModelInterface issueModel = this.issuesService.find(id, issueOption);
93
        if (issueModel == null) {
94
            throw new NotFoundException();
95
        }
96
        return issueModel;
97
    }
98
}
99