Completed
Pull Request — dev (#380)
by Konstantin
08:34 queued 03:18
created

show(Integer)   A

Complexity

Conditions 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 10
rs 9.9
cc 2
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.QuestionsMapper;
6
import easytests.api.v1.models.Question;
7
import easytests.core.models.QuestionModelInterface;
8
import easytests.core.options.AnswersOptions;
9
import easytests.core.options.QuestionsOptionsInterface;
10
import easytests.core.options.builder.QuestionsOptionsBuilderInterface;
11
import easytests.core.services.QuestionsServiceInterface;
12
import easytests.core.services.TopicsServiceInterface;
13
import org.springframework.beans.factory.annotation.Autowired;
14
import org.springframework.beans.factory.annotation.Qualifier;
15
import org.springframework.web.bind.annotation.*;
16
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 QuestionsServiceInterface questionsService;
28
29
    @Autowired
30
    protected QuestionsOptionsBuilderInterface questionsOptionsBuilder;
31
32
    @Autowired
33
    protected TopicsServiceInterface topicsService;
34
35
    @Autowired
36
    @Qualifier("QuestionsMapperV1")
37
    private QuestionsMapper questionsMapper;
38
39
    /**
40
     * list
41
     */
42
    /**
43
     * create
44
     */
45
    /**
46
     * update
47
     */
48
    @GetMapping("/{questionId}")
49
    public Question show(@PathVariable Integer questionId) throws NotFoundException, ForbiddenException {
50
        final QuestionModelInterface questionModel = this.getQuestionModel(
51
                questionId,
52
                this.questionsOptionsBuilder.forAuth().withAnswers(new AnswersOptions())
53
        );
54
        if (!this.acl.hasAccess(questionModel)) {
55
            throw new ForbiddenException();
56
        }
57
        return this.questionsMapper.map(questionModel, Question.class);
58
    }
59
60
    private QuestionModelInterface getQuestionModel(Integer id, QuestionsOptionsInterface
61
            questionOptions) throws NotFoundException {
62
        final QuestionModelInterface questionModel = this.questionsService.find(id, questionOptions);
63
        if (questionModel == null) {
64
            throw new NotFoundException();
65
        }
66
        return questionModel;
67
    }
68
69
    private QuestionModelInterface getQuestionModel(Integer id) throws NotFoundException {
0 ignored issues
show
Unused Code introduced by
Remove this unused private "getQuestionModel" method.
Loading history...
70
        return this.getQuestionModel(id, this.questionsOptionsBuilder.forAuth());
71
    }
72
    /**
73
     * delete(questionId)
74
     */
75
}
76