Passed
Pull Request — dev (#344)
by
unknown
05:09
created

getIssueModel(Integer)   A

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 2
rs 10
1
package easytests.api.v1.controllers;
2
3
import easytests.api.v1.exceptions.BadRequestException;
4
import easytests.api.v1.exceptions.ForbiddenException;
5
import easytests.api.v1.exceptions.UnidentifiedModelException;
6
import easytests.api.v1.mappers.IssuesMapper;
7
import easytests.api.v1.models.Issue;
8
import easytests.common.exceptions.NotFoundException;
9
import easytests.core.models.IssueModelInterface;
10
import easytests.core.options.IssuesOptionsInterface;
11
import easytests.core.options.builder.IssuesOptionsBuilderInterface;
12
import easytests.core.services.IssuesServiceInterface;
13
import easytests.core.services.SubjectsServiceInterface;
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 IssuesOptionsBuilderInterface issuesOptionsBuilder;
31
32
    @Autowired
33
    protected SubjectsServiceInterface subjectsService;
34
35
    @Autowired
36
    @Qualifier("IssuesMapperV1")
37
    private IssuesMapper issuesMapper;
38
39
    /**
40
     * list
41
     */
42
    /**
43
     * create
44
     */
45
46
    @PutMapping("")
47
    public void update(@RequestBody Issue issue) throws BadRequestException, NotFoundException, ForbiddenException {
48
49
        if (issue.getId() == null) {
50
            throw new UnidentifiedModelException();
51
        }
52
53
        final IssueModelInterface issueModel = this.getIssueModel(issue.getId());
54
55
        if (!this.acl.hasAccess(issueModel.getSubject())) {
56
            throw new ForbiddenException();
57
        }
58
59
        this.issuesMapper.map(issue, issueModel);
60
61
        this.issuesService.save(issueModel);
62
    }
63
64
    /**
65
     * show(issueId)
66
     */
67
    /**
68
     * delete(issueId)
69
     */
70
71
    private IssueModelInterface getIssueModel(Integer id, IssuesOptionsInterface issueOptions)
72
            throws NotFoundException {
73
        final IssueModelInterface issueModel = this.issuesService.find(id, issueOptions);
74
        if (issueModel == null) {
75
            throw new NotFoundException();
76
        }
77
        return issueModel;
78
    }
79
80
    private IssueModelInterface getIssueModel(Integer id) throws NotFoundException {
81
        return this.getIssueModel(id, this.issuesOptionsBuilder.forAuth());
82
    }
83
}
84