Passed
Pull Request — dev (#380)
by
unknown
04:46
created

easytests.api.v1.controllers.QuestionsController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 5
c 2
b 0
f 1
dl 0
loc 49
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getQuestionModel(Integer,QuestionsOptionsInterface) 0 6 2
A show(Integer) 0 10 2
A getQuestionModel(Integer) 0 2 1
1
package easytests.api.v1.controllers;
2
3
import easytests.api.v1.exceptions.ForbiddenException;
4
import easytests.api.v1.mappers.QuestionsMapper;
5
import easytests.api.v1.models.Question;
0 ignored issues
show
Unused Code introduced by
Remove this unused import 'easytests.api.v1.models.Question'.
Loading history...
6
import easytests.common.exceptions.NotFoundException;
7
import easytests.core.models.QuestionModelInterface;
8
import easytests.core.models.TopicModelInterface;
0 ignored issues
show
Unused Code introduced by
Remove this unused import 'easytests.core.models.TopicModelInterface'.
Loading history...
9
import easytests.core.models.empty.TopicModelEmpty;
0 ignored issues
show
Unused Code introduced by
Remove this unused import 'easytests.core.models.empty.TopicModelEmpty'.
Loading history...
10
import easytests.core.options.AnswersOptions;
11
import easytests.core.options.builder.QuestionsOptionsBuilder;
0 ignored issues
show
Unused Code introduced by
Remove this unused import 'easytests.core.options.builder.QuestionsOptionsBuilder'.
Loading history...
12
import easytests.core.options.QuestionsOptions;
13
import easytests.core.options.QuestionsOptionsInterface;
14
import easytests.core.options.builder.QuestionsOptionsBuilderInterface;
15
import easytests.core.services.QuestionsServiceInterface;
16
import easytests.core.services.TopicsServiceInterface;
17
import java.util.List;
0 ignored issues
show
Unused Code introduced by
Remove this unused import 'java.util.List'.
Loading history...
18
import java.util.stream.Collectors;
0 ignored issues
show
Unused Code introduced by
Remove this unused import 'java.util.stream.Collectors'.
Loading history...
19
import org.springframework.beans.factory.annotation.Autowired;
20
import org.springframework.beans.factory.annotation.Qualifier;
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
     * list
47
     */
48
    /**
49
     * create
50
     */
51
    /**
52
     * update
53
     */
54
    @GetMapping("/{questionId}")
55
    public Object show(@PathVariable Integer questionId) throws NotFoundException, ForbiddenException {
56
        final QuestionModelInterface questionModel = this.getQuestionModel(
57
                questionId,
58
                (new QuestionsOptions()).withAnswers(new AnswersOptions())
59
        );
60
        if (!this.acl.hasAccess(questionModel)) {
61
            throw new ForbiddenException();
62
        }
63
        return this.questionsMapper.map(questionModel, Object.class);
64
    }
65
66
    private QuestionModelInterface getQuestionModel(Integer id, QuestionsOptionsInterface questionOptions) throws NotFoundException {
67
        final QuestionModelInterface questionModel = this.questionsService.find(id, questionOptions);
68
        if (questionModel == null) {
69
            throw new NotFoundException();
70
        }
71
        return questionModel;
72
    }
73
74
    private QuestionModelInterface getQuestionModel(Integer id) throws NotFoundException {
0 ignored issues
show
Unused Code introduced by
Remove this unused private "getQuestionModel" method.
Loading history...
75
        return this.getQuestionModel(id, this.questionsOptionsBuilder.forAuth());
76
    }
77
    /**
78
     * delete(questionId)
79
     */
80
81
}
82