Completed
Pull Request — dev (#396)
by
unknown
06:59 queued 02:05
created

create(Question)   A

Complexity

Conditions 3

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 21
rs 9.376
cc 3
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.exceptions.BadRequestException;
6
import easytests.api.v1.exceptions.IdentifiedModelException;
7
import easytests.api.v1.mappers.QuestionsMapper;
8
import easytests.api.v1.models.Identity;
9
import easytests.api.v1.models.Question;
10
import easytests.api.v1.validators.QuiestionsValidator;
11
import easytests.core.models.QuestionModel;
12
import easytests.core.models.QuestionModelInterface;
13
import easytests.core.options.AnswersOptions;
14
import easytests.core.options.QuestionsOptionsInterface;
15
import easytests.core.options.builder.QuestionsOptionsBuilderInterface;
16
import easytests.core.services.QuestionsServiceInterface;
17
import easytests.core.services.TopicsServiceInterface;
18
import org.springframework.beans.factory.annotation.Autowired;
19
import org.springframework.beans.factory.annotation.Qualifier;
20
import org.springframework.http.HttpStatus;
21
import org.springframework.web.bind.annotation.*;
22
23
24
/**
25
 * @author RisaMagpie
26
 */
27
@RestController("QuestionsControllerV1")
28
@SuppressWarnings("checkstyle:MultipleStringLiterals")
29
@RequestMapping("/v1/questions")
30
public class QuestionsController extends AbstractController {
31
32
    @Autowired
33
    protected QuestionsServiceInterface questionsService;
34
35
    @Autowired
36
    protected QuestionsOptionsBuilderInterface questionsOptionsBuilder;
37
38
    @Autowired
39
    protected TopicsServiceInterface topicsService;
40
41
    @Autowired
42
    @Qualifier("QuestionsMapperV1")
43
    private QuestionsMapper questionsMapper;
44
45
46
    /**
47
     * list
48
     */
49
    /**
50
     * create
51
     */
52
53
    @PostMapping("")
54
    @ResponseStatus(HttpStatus.CREATED)
55
    public Identity create(@RequestBody Question question) throws Exception {
56
57
        if (question.getId() != null) {
58
            throw new IdentifiedModelException();
59
        }
60
61
        if (question.getAnswers() == null) {
62
            throw new BadRequestException("Answers must be not absent");
63
        }
64
65
        final QuiestionsValidator quiestionsValidator = new QuiestionsValidator();
66
67
        final QuestionModelInterface questionModel = this.questionsMapper.map(question, QuestionModel.class);
68
69
        quiestionsValidator.validateQuestion(questionModel);
70
71
        this.questionsService.save(questionModel);
72
73
        return this.questionsMapper.map(questionModel, Identity.class);
74
    }
75
76
    /**
77
     * update
78
     */
79
    @GetMapping("/{questionId}")
80
    public Question show(@PathVariable Integer questionId) throws NotFoundException, ForbiddenException {
81
        final QuestionModelInterface questionModel = this.getQuestionModel(
82
                questionId,
83
                this.questionsOptionsBuilder.forAuth().withAnswers(new AnswersOptions())
84
        );
85
        if (!this.acl.hasAccess(questionModel)) {
86
            throw new ForbiddenException();
87
        }
88
        return this.questionsMapper.map(questionModel, Question.class);
89
    }
90
91
    private QuestionModelInterface getQuestionModel(Integer id, QuestionsOptionsInterface
92
            questionOptions) throws NotFoundException {
93
        final QuestionModelInterface questionModel = this.questionsService.find(id, questionOptions);
94
        if (questionModel == null) {
95
            throw new NotFoundException();
96
        }
97
        return questionModel;
98
    }
99
100
    private QuestionModelInterface getQuestionModel(Integer id) throws NotFoundException {
0 ignored issues
show
Unused Code introduced by
Remove this unused private "getQuestionModel" method.
Loading history...
101
        return this.getQuestionModel(id, this.questionsOptionsBuilder.forAuth());
102
    }
103
    /**
104
     * delete(questionId)
105
     */
106
}
107