Completed
Pull Request — dev (#371)
by
unknown
08:41 queued 04:06
created

list(Integer)   A

Complexity

Conditions 3

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

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