Passed
Pull Request — dev (#387)
by
unknown
03:22
created

easytests.api.v1.controllers.IssuesController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
wmc 7
c 6
b 0
f 0
dl 0
loc 75
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getIssueModel(Integer) 0 3 1
A delete(Integer) 0 11 2
getIssueModel 0 3 ?
A list(Integer) 0 19 3
A getIssueModel(Integer,IssuesOptionsInterface) 0 3 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 IssuesOptionsBuilder issuesOptionsBuilder;
35
36
    @Autowired
37
    protected SubjectsServiceInterface subjectsService;
38
39
    @Autowired
40
    @Qualifier("IssuesMapperV1")
41
    private IssuesMapper issuesMapper;
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
     * show(issueId)
71
     */
72
    /**
73
     * delete(issueId)
74
     */
75
76
    @DeleteMapping("/{issueId}")
77
    public void delete(@PathVariable Integer issueId) throws NotFoundException, ForbiddenException {
78
        final IssueModelInterface issueModel = this.getIssueModel(issueId);
79
80
        if (issueModel == null) {
81
            throw new NotFoundException();
82
        }
83
        //if (!this.acl.hasAccess(issueModel)){
84
        //    throw new ForbiddenException();
85
        //}
86
        this.issuesService.delete(issueModel);
87
    }
88
89
    private IssueModelInterface getIssueModel(Integer id) throws NotFoundException {
90
        final IssuesOptionsInterface issuesOptionsInterface = this.issuesOptionsBuilder.forDelete();
91
        return this.getIssueModel(id, issuesOptionsInterface);
92
    }
93
94
    private IssueModelInterface getIssueModel(Integer id, IssuesOptionsInterface issueOption) {
95
        final IssueModelInterface issueModel = this.issuesService.find(id, issueOption);
96
        return issueModel;
97
    }
98
}
99