Completed
Pull Request — dev (#371)
by
unknown
05:43
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.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.models.TopicModelInterface;
9
import easytests.core.options.builder.QuestionsOptionsBuilderInterface;
10
import easytests.core.services.QuestionsServiceInterface;
11
import easytests.core.services.TopicsServiceInterface;
12
import java.util.List;
13
import java.util.stream.Collectors;
14
import org.springframework.beans.factory.annotation.Autowired;
15
import org.springframework.beans.factory.annotation.Qualifier;
16
import org.springframework.web.bind.annotation.*;
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
    @GetMapping
40
    public List<Question> list(@RequestParam(name = "topicId", required = true) Integer topicId)
41
            throws NotFoundException, ForbiddenException {
42
        final TopicModelInterface topicModel = this.topicsService.find(topicId);
43
44
        if (topicModel == null) {
45
            throw new NotFoundException();
46
        }
47
48
        if (!this.acl.hasAccess(topicModel)) {
49
            throw new ForbiddenException();
50
        }
51
52
        final List<QuestionModelInterface> questionsModels =
53
                this.questionsService.findByTopic(topicModel);
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