|
1
|
|
|
package easytests.personal.controllers; |
|
2
|
|
|
|
|
3
|
|
|
import easytests.common.controllers.AbstractCrudController; |
|
4
|
|
|
import easytests.common.exceptions.ForbiddenException; |
|
5
|
|
|
import easytests.common.exceptions.NotFoundException; |
|
6
|
|
|
import easytests.core.models.*; |
|
7
|
|
|
import easytests.core.options.*; |
|
8
|
|
|
import easytests.core.options.builder.AnswersOptionsBuilder; |
|
9
|
|
|
import easytests.core.options.builder.QuestionsOptionsBuilder; |
|
10
|
|
|
import easytests.core.options.builder.TopicsOptionsBuilder; |
|
11
|
|
|
import easytests.core.services.AnswersService; |
|
12
|
|
|
import easytests.core.services.QuestionTypesService; |
|
13
|
|
|
import easytests.core.services.QuestionsService; |
|
14
|
|
|
import easytests.core.services.TopicsService; |
|
15
|
|
|
import easytests.personal.dto.QuestionModelDto; |
|
16
|
|
|
import easytests.personal.validators.QuestionModelDtoValidator; |
|
17
|
|
|
import java.util.*; |
|
18
|
|
|
import javax.validation.Valid; |
|
19
|
|
|
import javax.validation.constraints.NotNull; |
|
20
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
21
|
|
|
import org.springframework.stereotype.Controller; |
|
22
|
|
|
import org.springframework.ui.Model; |
|
23
|
|
|
import org.springframework.validation.BindingResult; |
|
24
|
|
|
import org.springframework.web.bind.annotation.*; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @author firkhraag |
|
28
|
|
|
* @author rezenbekk |
|
29
|
|
|
*/ |
|
30
|
|
|
@Controller |
|
31
|
|
|
@SuppressWarnings({ |
|
32
|
|
|
"checkstyle:MultipleStringLiterals", |
|
33
|
|
|
"checkstyle:ClassFanOutComplexity"}) |
|
34
|
|
|
@RequestMapping("/personal/topics/{topicId}/questions") |
|
35
|
|
|
public class QuestionsController extends AbstractCrudController { |
|
36
|
|
|
|
|
37
|
|
|
@Autowired |
|
38
|
|
|
protected TopicsService topicsService; |
|
39
|
|
|
|
|
40
|
|
|
@Autowired |
|
41
|
|
|
private QuestionsService questionsService; |
|
42
|
|
|
|
|
43
|
|
|
@Autowired |
|
44
|
|
|
private QuestionTypesService questionTypesService; |
|
45
|
|
|
|
|
46
|
|
|
@Autowired |
|
47
|
|
|
private QuestionsOptionsBuilder questionsOptionsBuilder; |
|
48
|
|
|
|
|
49
|
|
|
@Autowired |
|
50
|
|
|
private TopicsOptionsBuilder topicsOptionsBuilder; |
|
51
|
|
|
|
|
52
|
|
|
@Autowired |
|
53
|
|
|
private QuestionModelDtoValidator questionModelDtoValidator; |
|
54
|
|
|
|
|
55
|
|
|
@Autowired |
|
56
|
|
|
private AnswersOptionsBuilder answersOptionsBuilder; |
|
57
|
|
|
|
|
58
|
|
|
@Autowired |
|
59
|
|
|
private AnswersService answersService; |
|
60
|
|
|
|
|
61
|
|
|
@GetMapping("") |
|
62
|
|
|
public String list(Model model, @PathVariable("topicId") Integer topicId) { |
|
63
|
|
|
final List<QuestionModelInterface> questions = this.questionsService |
|
64
|
|
|
.findByTopic(this.getCurrentTopicModel(topicId)); |
|
65
|
|
|
model.addAttribute("questions", questions); |
|
66
|
|
|
model.addAttribute("topicId", topicId); |
|
67
|
|
|
return "questions/list"; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
@GetMapping("{questionId}") |
|
71
|
|
|
public String read( |
|
72
|
|
|
Model model, |
|
73
|
|
|
@PathVariable("questionId") Integer questionId, |
|
74
|
|
|
@PathVariable("topicId") Integer topicId) { |
|
75
|
|
|
final TopicModelInterface topicModel = getCurrentTopicModel(topicId); |
|
|
|
|
|
|
76
|
|
|
final QuestionModelInterface questionModel = getQuestionModel(questionId, topicId, false); |
|
77
|
|
|
injectQuestionTypeModels(model); |
|
78
|
|
|
model.addAttribute("question", questionModel); |
|
79
|
|
|
model.addAttribute("topicId", topicId); |
|
80
|
|
|
return "questions/view"; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
@GetMapping("create/") |
|
84
|
|
|
public String create(Model model, @PathVariable("topicId") Integer topicId) { |
|
85
|
|
|
final TopicModelInterface topicModel = getCurrentTopicModel(topicId); |
|
|
|
|
|
|
86
|
|
|
injectQuestionTypeModels(model); |
|
87
|
|
|
final QuestionModelDto questionModelDto = new QuestionModelDto(); |
|
88
|
|
|
setCreateBehaviour(model); |
|
89
|
|
|
model.addAttribute("question", questionModelDto); |
|
90
|
|
|
model.addAttribute("topicId", topicId); |
|
91
|
|
|
return "questions/form"; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
@PostMapping("create/") |
|
95
|
|
|
public String insert( |
|
96
|
|
|
Model model, |
|
97
|
|
|
@Valid @NotNull QuestionModelDto questionModelDto, |
|
98
|
|
|
BindingResult bindingResult, |
|
99
|
|
|
@PathVariable("topicId") Integer topicId) { |
|
100
|
|
|
final TopicModelInterface topicModel = getCurrentTopicModel(topicId); |
|
101
|
|
|
this.questionModelDtoValidator.validate(questionModelDto, bindingResult); |
|
102
|
|
|
if (bindingResult.hasErrors()) { |
|
103
|
|
|
injectQuestionTypeModels(model); |
|
104
|
|
|
setCreateBehaviour(model); |
|
105
|
|
|
model.addAttribute("question", questionModelDto); |
|
106
|
|
|
model.addAttribute("topicId", topicId); |
|
107
|
|
|
model.addAttribute("errors", bindingResult); |
|
108
|
|
|
return "questions/form"; |
|
109
|
|
|
} |
|
110
|
|
|
final QuestionModelInterface questionModel = new QuestionModel(); |
|
111
|
|
|
questionModelDto.mapInto(questionModel, questionTypesService); |
|
112
|
|
|
questionModel.setTopic(topicModel); |
|
113
|
|
|
this.questionsService.save(questionModel); |
|
114
|
|
|
return "redirect:/personal/topics/" + topicId + "/questions/"; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
@GetMapping("update/{questionId}/") |
|
118
|
|
|
public String update( |
|
119
|
|
|
Model model, |
|
120
|
|
|
@PathVariable Integer questionId, |
|
121
|
|
|
@PathVariable("topicId") Integer topicId) { |
|
122
|
|
|
final TopicModelInterface topicModel = getCurrentTopicModel(topicId); |
|
|
|
|
|
|
123
|
|
|
final QuestionModelInterface questionModel = this.getQuestionModel(questionId, topicId, false); |
|
124
|
|
|
injectQuestionTypeModels(model); |
|
125
|
|
|
final QuestionModelDto questionModelDto = new QuestionModelDto(); |
|
126
|
|
|
questionModelDto.map(questionModel); |
|
127
|
|
|
setUpdateBehaviour(model); |
|
128
|
|
|
model.addAttribute("question", questionModelDto); |
|
129
|
|
|
model.addAttribute("topicId", topicId); |
|
130
|
|
|
return "questions/form"; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
@PostMapping("update/{questionId}/") |
|
134
|
|
|
public String save( |
|
135
|
|
|
Model model, |
|
136
|
|
|
@PathVariable Integer questionId, |
|
137
|
|
|
@Valid @NotNull QuestionModelDto questionModelDto, |
|
138
|
|
|
BindingResult bindingResult, |
|
139
|
|
|
@PathVariable("topicId") Integer topicId) { |
|
140
|
|
|
final TopicModelInterface topicModel = getCurrentTopicModel(topicId); |
|
|
|
|
|
|
141
|
|
|
final QuestionModelInterface questionModel = this.getQuestionModel(questionId, topicId, false); |
|
142
|
|
|
this.questionModelDtoValidator.validate(questionModelDto, bindingResult); |
|
143
|
|
|
if (bindingResult.hasErrors()) { |
|
144
|
|
|
injectQuestionTypeModels(model); |
|
145
|
|
|
setUpdateBehaviour(model); |
|
146
|
|
|
model.addAttribute("question", questionModelDto); |
|
147
|
|
|
model.addAttribute("topicId", topicId); |
|
148
|
|
|
model.addAttribute("errors", bindingResult); |
|
149
|
|
|
return "questions/form"; |
|
150
|
|
|
} |
|
151
|
|
|
questionModelDto.mapInto(questionModel, questionTypesService); |
|
152
|
|
|
this.questionsService.save(questionModel); |
|
153
|
|
|
return "redirect:/personal/topics/" + topicId + "/questions/"; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
@GetMapping("delete/{questionId}") |
|
157
|
|
|
public String deleteConfirmation( |
|
158
|
|
|
Model model, |
|
159
|
|
|
@PathVariable("questionId") Integer questionId, |
|
160
|
|
|
@PathVariable("topicId") Integer topicId) { |
|
161
|
|
|
final TopicModelInterface topicModel = getCurrentTopicModel(topicId); |
|
|
|
|
|
|
162
|
|
|
final QuestionModelInterface questionModel = getQuestionModel(questionId, topicId, false); |
|
|
|
|
|
|
163
|
|
|
model.addAttribute("topicId", topicId); |
|
164
|
|
|
return "questions/delete"; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
@PostMapping("delete/{questionId}/") |
|
168
|
|
|
public String delete( |
|
169
|
|
|
Model model, |
|
170
|
|
|
@PathVariable("questionId") Integer questionId, |
|
171
|
|
|
@PathVariable("topicId") Integer topicId) { |
|
172
|
|
|
final TopicModelInterface topicModel = getCurrentTopicModel(topicId); |
|
|
|
|
|
|
173
|
|
|
final QuestionModelInterface questionModel = getQuestionModel(questionId, topicId, true); |
|
174
|
|
|
questionsService.delete(questionModel, this.questionsOptionsBuilder.forDelete()); |
|
175
|
|
|
return "redirect:/personal/topics/" + topicId + "/questions/"; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
private TopicModelInterface getCurrentTopicModel(Integer topicId) { |
|
179
|
|
|
final TopicsOptionsInterface topicsOptions = this.topicsOptionsBuilder.forAuth(); |
|
180
|
|
|
final TopicModelInterface topicModel = topicsService.find(topicId, topicsOptions); |
|
181
|
|
|
checkModel(topicModel); |
|
182
|
|
|
return topicModel; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
private void checkModel(QuestionModelInterface questionModel, Integer topicId) { |
|
186
|
|
|
if (questionModel == null) { |
|
187
|
|
|
throw new NotFoundException(); |
|
188
|
|
|
} |
|
189
|
|
|
if (!questionModel.getTopic().getId().equals(this.getCurrentTopicModel(topicId).getId())) { |
|
190
|
|
|
throw new ForbiddenException(); |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
private void checkModel(TopicModelInterface topicModel) { |
|
195
|
|
|
if (topicModel == null) { |
|
196
|
|
|
throw new NotFoundException(); |
|
197
|
|
|
} |
|
198
|
|
|
if (!topicModel.getSubject().getUser().getId().equals(this.getCurrentUserModel().getId())) { |
|
199
|
|
|
throw new ForbiddenException(); |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
private QuestionModelInterface getQuestionModel(Integer id, Integer topicId, Boolean forDelete) { |
|
204
|
|
|
final QuestionsOptionsInterface questionsOptions = this.questionsOptionsBuilder |
|
205
|
|
|
.forAuth() |
|
206
|
|
|
.withAnswers(new AnswersOptions()); |
|
207
|
|
|
final QuestionModelInterface questionModel = this.questionsService.find(id, questionsOptions); |
|
208
|
|
|
checkModel(questionModel, topicId); |
|
209
|
|
|
if (forDelete) { |
|
210
|
|
|
return this.questionsService.find(id, this.questionsOptionsBuilder.forDelete()); |
|
211
|
|
|
} |
|
212
|
|
|
return questionModel; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
private void injectQuestionTypeModels(Model model) { |
|
216
|
|
|
final List<QuestionTypeModelInterface> questionTypes = this.questionTypesService.findAll(); |
|
217
|
|
|
model.addAttribute("questionTypes", questionTypes); |
|
218
|
|
|
} |
|
219
|
|
|
} |
|
220
|
|
|
|