Completed
Pull Request — dev (#344)
by
unknown
06:19 queued 01:06
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.IssuesOptionsBuilder;
0 ignored issues
show
Unused Code introduced by
Remove this unused import 'easytests.core.options.builder.IssuesOptionsBuilder'.
Loading history...
12
import easytests.core.options.builder.IssuesOptionsBuilderInterface;
13
import easytests.core.services.IssuesServiceInterface;
14
import easytests.core.services.SubjectsServiceInterface;
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 IssuesOptionsBuilderInterface issuesOptionsBuilder;
32
33
    @Autowired
34
    protected SubjectsServiceInterface subjectsService;
35
36
    @Autowired
37
    @Qualifier("IssuesMapperV1")
38
    private IssuesMapper issuesMapper;
39
40
    /**
41
     * list
42
     */
43
    /**
44
     * create
45
     */
46
47
    @PutMapping
48
    public void update(@RequestBody Issue issue) throws BadRequestException, NotFoundException, ForbiddenException {
49
50
        if(issue.getId()==null) {
51
            throw new UnidentifiedModelException();
52
        }
53
54
        final IssueModelInterface issueModel = this.getIssueModel(issue.getId());
55
56
        if(!this.acl.hasAccess(issueModel.getSubject())) {
57
            throw new ForbiddenException();
58
        }
59
60
        this.issuesMapper.map(issue, issueModel);
61
62
        this.issuesService.save(issueModel);
63
    }
64
65
    /**
66
     * show(issueId)
67
     */
68
    /**
69
     * delete(issueId)
70
     */
71
72
    private IssueModelInterface getIssueModel(Integer id, IssuesOptionsInterface issueOptions) 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