Completed
Pull Request — dev (#371)
by
unknown
11:36 queued 07:30
created

easytests.api.v1.controllers.QuestionsController   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A list(Integer) 0 20 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.AnswersOptions;
10
import easytests.core.options.QuestionsOptions;
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 TopicsServiceInterface topicsService;
32
33
    @Autowired
34
    @Qualifier("QuestionsMapperV1")
35
    private QuestionsMapper questionsMapper;
36
37
    @GetMapping
38
    public List<Question> list(@RequestParam(name = "topicId", required = true) Integer topicId)
39
            throws NotFoundException, ForbiddenException {
40
        final TopicModelInterface topicModel = this.topicsService.find(topicId);
41
42
        if (topicModel == null) {
43
            throw new NotFoundException();
44
        }
45
46
        if (!this.acl.hasAccess(topicModel)) {
47
            throw new ForbiddenException();
48
        }
49
50
        final List<QuestionModelInterface> questionsModels =
51
                this.questionsService.findByTopic(topicModel, new QuestionsOptions().withAnswers(new AnswersOptions()));
52
53
        return questionsModels
54
                .stream()
55
                .map(model -> this.questionsMapper.map(model, Question.class))
56
                .collect(Collectors.toList());
57
    }
58
    /**
59
     * create
60
     */
61
    /**
62
     * update
63
     */
64
    /**
65
     * show(questionId)
66
     */
67
    /**
68
     * delete(questionId)
69
     */
70
71
}
72