|
1
|
|
|
package easytests.api.v1.controllers; |
|
2
|
|
|
|
|
3
|
|
|
import easytests.api.v1.exceptions.BadRequestException; |
|
4
|
|
|
import easytests.api.v1.exceptions.IdentifiedModelException; |
|
5
|
|
|
import easytests.api.v1.mappers.QuestionsMapper; |
|
6
|
|
|
import easytests.api.v1.models.Identity; |
|
7
|
|
|
import easytests.api.v1.models.Question; |
|
8
|
|
|
import easytests.core.models.QuestionModel; |
|
9
|
|
|
import easytests.core.models.QuestionModelInterface; |
|
10
|
|
|
import easytests.core.options.builder.QuestionsOptionsBuilder; |
|
11
|
|
|
import easytests.core.services.QuestionsService; |
|
12
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
13
|
|
|
import org.springframework.beans.factory.annotation.Qualifier; |
|
14
|
|
|
import org.springframework.http.HttpStatus; |
|
15
|
|
|
import org.springframework.web.bind.annotation.*; |
|
16
|
|
|
import easytests.api.v1.validators.*; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @author RisaMagpie |
|
20
|
|
|
*/ |
|
21
|
|
|
@RestController("QuestionsControllerV1") |
|
22
|
|
|
@SuppressWarnings("checkstyle:MultipleStringLiterals") |
|
23
|
|
|
@RequestMapping("/v1/questions") |
|
24
|
|
|
public class QuestionsController extends AbstractController{ |
|
25
|
|
|
|
|
26
|
|
|
@Autowired |
|
27
|
|
|
protected QuestionsService questionsService; |
|
28
|
|
|
|
|
29
|
|
|
@Autowired |
|
30
|
|
|
protected QuestionsOptionsBuilder questionsOptions; |
|
31
|
|
|
|
|
32
|
|
|
@Autowired |
|
33
|
|
|
@Qualifier("QuestionsMapperV1") |
|
34
|
|
|
private QuestionsMapper questionsMapper; |
|
35
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* list |
|
39
|
|
|
*/ |
|
40
|
|
|
/** |
|
41
|
|
|
* create |
|
42
|
|
|
*/ |
|
43
|
|
|
|
|
44
|
|
|
@PostMapping("") |
|
45
|
|
|
@ResponseStatus(HttpStatus.CREATED) |
|
46
|
|
|
public Identity create(@RequestBody Question question) throws Exception { |
|
47
|
|
|
|
|
48
|
|
|
if (question.getId() != null) { |
|
49
|
|
|
throw new IdentifiedModelException(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
if (question.getAnswers() == null) { |
|
53
|
|
|
throw new BadRequestException("Answers must be not absent"); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
//VALIDATOR |
|
57
|
|
|
final QuiestionsValidator quiestionsValidator = new QuiestionsValidator(); |
|
58
|
|
|
|
|
59
|
|
|
final QuestionModelInterface questionModel = this.questionsMapper.map(question, QuestionModel.class); |
|
60
|
|
|
|
|
61
|
|
|
quiestionsValidator.validateQuestion(questionModel); |
|
62
|
|
|
|
|
63
|
|
|
this.questionsService.save(questionModel); |
|
64
|
|
|
|
|
65
|
|
|
return this.questionsMapper.map(questionModel, Identity.class); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* update |
|
70
|
|
|
*/ |
|
71
|
|
|
/** |
|
72
|
|
|
* show(questionId) |
|
73
|
|
|
*/ |
|
74
|
|
|
/** |
|
75
|
|
|
* delete(questionId) |
|
76
|
|
|
*/ |
|
77
|
|
|
} |
|
78
|
|
|
|