|
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 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
|
|
|
@GetMapping("/{issueId}") |
|
71
|
|
|
public Object show(@PathVariable Integer issueId) throws NotFoundException, ForbiddenException { |
|
72
|
|
|
|
|
73
|
|
|
final IssueModelInterface issueModel = this.getIssueModel(issueId); |
|
74
|
|
|
|
|
75
|
|
|
if (!this.acl.hasAccess(issueModel)) { |
|
76
|
|
|
throw new ForbiddenException(); |
|
77
|
|
|
} |
|
78
|
|
|
return this.issuesMapper.map(issueModel, Issue.class); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
private IssueModelInterface getIssueModel(Integer id) throws NotFoundException { |
|
82
|
|
|
final IssuesOptionsInterface issuesOptionsInterface = this.issuesOptions.forAuth(); |
|
83
|
|
|
return this.getIssueModel(id, issuesOptionsInterface); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
private IssueModelInterface getIssueModel(Integer id, IssuesOptionsInterface issueOption) |
|
87
|
|
|
throws NotFoundException { |
|
88
|
|
|
final IssueModelInterface issueModel = this.issuesService.find(id, issueOption); |
|
89
|
|
|
if (issueModel == null) { |
|
90
|
|
|
throw new NotFoundException(); |
|
91
|
|
|
} |
|
92
|
|
|
return issueModel; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* delete(issueId) |
|
98
|
|
|
*/ |
|
99
|
|
|
|
|
100
|
|
|
} |
|
101
|
|
|
|