Completed
Pull Request — dev (#371)
by
unknown
04:51
created

list(Integer)   A

Complexity

Conditions 3

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 20
rs 9.4
cc 3
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;
6
import easytests.common.exceptions.NotFoundException;
7
import easytests.core.models.QuestionModelInterface;
8
import easytests.core.models.TopicModelInterface;
9
import easytests.core.models.empty.TopicModelEmpty;
10
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...
11
import easytests.core.options.builder.QuestionsOptionsBuilderInterface;
12
import easytests.core.services.QuestionsServiceInterface;
13
import easytests.core.services.TopicsServiceInterface;
14
import java.util.List;
15
import java.util.stream.Collectors;
16
import org.springframework.beans.factory.annotation.Autowired;
17
import org.springframework.beans.factory.annotation.Qualifier;
18
import org.springframework.web.bind.annotation.*;
19
20
/**
21
 * @author RisaMagpie
22
 */
23
@RestController("QuestionsControllerV1")
24
@SuppressWarnings("checkstyle:MultipleStringLiterals")
25
@RequestMapping("/v1/questions")
26
public class QuestionsController extends AbstractController {
27
28
    @Autowired
29
    protected QuestionsServiceInterface questionsService;
30
31
    @Autowired
32
    protected QuestionsOptionsBuilderInterface questionsOptionsBuilder;
33
34
    @Autowired
35
    protected TopicsServiceInterface topicsService;
36
37
    @Autowired
38
    @Qualifier("QuestionsMapperV1")
39
    private QuestionsMapper questionsMapper;
40
41
    @GetMapping
42
    public List<Question> list(@RequestParam(name = "topicId", required = true) Integer topicId)
43
            throws NotFoundException, ForbiddenException {
44
        final TopicModelInterface topicModel = this.topicsService.find(topicId);
45
46
        if (topicModel == null) {
47
            throw new NotFoundException();
48
        }
49
50
        if (!this.acl.hasAccess(topicModel)) {
51
            throw new ForbiddenException();
52
        }
53
54
        final List<QuestionModelInterface> questionsModels =
55
                this.questionsService.findByTopic(new TopicModelEmpty(topicId));
56
57
        return questionsModels
58
                .stream()
59
                .map(model -> this.questionsMapper.map(model, Question.class))
60
                .collect(Collectors.toList());
61
    }
62
    /**
63
     * create
64
     */
65
    /**
66
     * update
67
     */
68
    /**
69
     * show(questionId)
70
     */
71
    /**
72
     * delete(questionId)
73
     */
74
75
}
76