|
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.IdentifiedModelException; |
|
6
|
|
|
import easytests.api.v1.exceptions.NotFoundException; |
|
7
|
|
|
import easytests.api.v1.exceptions.UnidentifiedModelException; |
|
8
|
|
|
import easytests.api.v1.mappers.TopicsMapper; |
|
9
|
|
|
import easytests.api.v1.models.Identity; |
|
10
|
|
|
import easytests.api.v1.models.Topic; |
|
11
|
|
|
import easytests.core.models.SubjectModelInterface; |
|
12
|
|
|
import easytests.core.models.TopicModel; |
|
13
|
|
|
import easytests.core.models.TopicModelInterface; |
|
14
|
|
|
import easytests.core.options.TopicsOptionsInterface; |
|
15
|
|
|
import easytests.core.options.builder.SubjectsOptionsBuilder; |
|
16
|
|
|
import easytests.core.options.builder.TopicsOptionsBuilderInterface; |
|
17
|
|
|
import easytests.core.services.SubjectsServiceInterface; |
|
18
|
|
|
import easytests.core.services.TopicsServiceInterface; |
|
19
|
|
|
import java.util.List; |
|
20
|
|
|
import java.util.stream.Collectors; |
|
21
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
22
|
|
|
import org.springframework.beans.factory.annotation.Qualifier; |
|
23
|
|
|
import org.springframework.http.HttpStatus; |
|
24
|
|
|
import org.springframework.web.bind.annotation.*; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @author lelay |
|
28
|
|
|
*/ |
|
29
|
|
|
@RestController("TopicsControllerV1") |
|
30
|
|
|
@SuppressWarnings("checkstyle:MultipleStringLiterals") |
|
31
|
|
|
@RequestMapping("/v1/topics") |
|
32
|
|
|
public class TopicsController extends AbstractController { |
|
33
|
|
|
|
|
34
|
|
|
@Autowired |
|
35
|
|
|
protected SubjectsOptionsBuilder subjectsOptionsBuilder; |
|
36
|
|
|
|
|
37
|
|
|
@Autowired |
|
38
|
|
|
protected TopicsServiceInterface topicsService; |
|
39
|
|
|
|
|
40
|
|
|
@Autowired |
|
41
|
|
|
protected TopicsOptionsBuilderInterface topicsOptionsBuilder; |
|
42
|
|
|
|
|
43
|
|
|
@Autowired |
|
44
|
|
|
protected SubjectsServiceInterface subjectsService; |
|
45
|
|
|
|
|
46
|
|
|
@Autowired |
|
47
|
|
|
@Qualifier("TopicsMapperV1") |
|
48
|
|
|
private TopicsMapper topicsMapper; |
|
49
|
|
|
|
|
50
|
|
|
@GetMapping("") |
|
51
|
|
|
public List<Topic> list(@RequestParam(name = "subjectId", required = true) Integer subjectId) |
|
52
|
|
|
throws NotFoundException, ForbiddenException { |
|
53
|
|
|
final SubjectModelInterface subjectModel = this.subjectsService.find(subjectId); |
|
54
|
|
|
|
|
55
|
|
|
if (subjectModel == null) { |
|
56
|
|
|
throw new NotFoundException(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
if (!this.acl.hasAccess(subjectModel)) { |
|
60
|
|
|
throw new ForbiddenException(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
final List<TopicModelInterface> topicsModels = |
|
64
|
|
|
this.topicsService.findBySubject(subjectModel); |
|
65
|
|
|
|
|
66
|
|
|
return topicsModels |
|
67
|
|
|
.stream() |
|
68
|
|
|
.map(model -> this.topicsMapper.map(model, Topic.class)) |
|
69
|
|
|
.collect(Collectors.toList()); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* create |
|
74
|
|
|
*/ |
|
75
|
|
|
|
|
76
|
|
|
@PostMapping("") |
|
77
|
|
|
@ResponseStatus(HttpStatus.CREATED) |
|
78
|
|
|
public Identity create(@RequestBody Topic topic) throws Exception { |
|
|
|
|
|
|
79
|
|
|
if (topic.getId() != null) { |
|
80
|
|
|
throw new IdentifiedModelException(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
this.checkSubject(topic); |
|
84
|
|
|
|
|
85
|
|
|
final TopicModelInterface topicModel = this.topicsMapper.map(topic, TopicModel.class); |
|
86
|
|
|
|
|
87
|
|
|
this.topicsService.save(topicModel); |
|
88
|
|
|
|
|
89
|
|
|
return this.topicsMapper.map(topicModel, Identity.class); |
|
90
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* update |
|
95
|
|
|
*/ |
|
96
|
|
|
|
|
97
|
|
|
@PutMapping("") |
|
98
|
|
|
public void update(@RequestBody Topic topic) throws Exception { |
|
|
|
|
|
|
99
|
|
|
if (topic.getId() == null) { |
|
100
|
|
|
throw new UnidentifiedModelException(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
final TopicModelInterface topicModel = this.getTopicModel(topic.getId()); |
|
104
|
|
|
|
|
105
|
|
|
this.checkSubject(topic); |
|
106
|
|
|
|
|
107
|
|
|
this.topicsMapper.map(topic, topicModel); |
|
108
|
|
|
|
|
109
|
|
|
this.topicsService.save(topicModel); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
@GetMapping("/{topicId}") |
|
113
|
|
|
public Topic show(@PathVariable Integer topicId) throws NotFoundException, ForbiddenException { |
|
114
|
|
|
final TopicModelInterface topicModel = this.topicsService.find(topicId); |
|
115
|
|
|
|
|
116
|
|
|
if (topicModel == null) { |
|
117
|
|
|
throw new NotFoundException(); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
if (!this.acl.hasAccess(topicModel)) { |
|
121
|
|
|
throw new ForbiddenException(); |
|
122
|
|
|
} |
|
123
|
|
|
return this.topicsMapper.map(topicModel, Topic.class); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* delete(topicId) |
|
128
|
|
|
*/ |
|
129
|
|
|
|
|
130
|
|
|
@DeleteMapping("/{topicId}") |
|
131
|
|
|
public void delete(@PathVariable Integer topicId) throws NotFoundException, ForbiddenException { |
|
132
|
|
|
final TopicModelInterface topicModelforAuth = this.getTopicModel(topicId); |
|
133
|
|
|
|
|
134
|
|
|
if (!this.acl.hasAccess(topicModelforAuth)) { |
|
135
|
|
|
throw new ForbiddenException(); |
|
136
|
|
|
} |
|
137
|
|
|
final TopicsOptionsInterface topicsOptionDelete = this.topicsOptionsBuilder.forDelete(); |
|
138
|
|
|
final TopicModelInterface topicModelForDelete = this.topicsService.find(topicId, topicsOptionDelete); |
|
139
|
|
|
|
|
140
|
|
|
this.topicsService.delete(topicModelForDelete, topicsOptionDelete); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
private void checkSubject(Topic topic) throws ForbiddenException, BadRequestException { |
|
144
|
|
|
final SubjectModelInterface subjectModel = this.subjectsService.find( |
|
145
|
|
|
topic.getSubject().getId(), |
|
146
|
|
|
this.subjectsOptionsBuilder.forAuth() |
|
147
|
|
|
); |
|
148
|
|
|
|
|
149
|
|
|
if (!this.acl.hasAccess(subjectModel)) { |
|
150
|
|
|
throw new BadRequestException(); |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
private TopicModelInterface getTopicModel(Integer id, TopicsOptionsInterface topicOptions) |
|
155
|
|
|
throws NotFoundException { |
|
156
|
|
|
final TopicModelInterface topicModel = this.topicsService.find(id, topicOptions); |
|
157
|
|
|
if (topicModel == null) { |
|
158
|
|
|
throw new NotFoundException(); |
|
159
|
|
|
} |
|
160
|
|
|
return topicModel; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
private TopicModelInterface getTopicModel(Integer id) throws NotFoundException { |
|
164
|
|
|
return this.getTopicModel(id, this.topicsOptionsBuilder.forAuth()); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
} |
|
168
|
|
|
|