Completed
Pull Request — dev (#396)
by
unknown
05:33
created

easytests.api.v1.controllers.QuestionsController   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getQuestionModel(Integer,QuestionsOptionsInterface) 0 7 2
A create(Question) 0 19 2
A show(Integer) 0 10 2
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.IdentifiedModelException;
5
import easytests.api.v1.exceptions.NotFoundException;
6
import easytests.api.v1.mappers.QuestionsMapper;
7
import easytests.api.v1.models.Identity;
8
import easytests.api.v1.models.Question;
9
import easytests.api.v1.validators.QuiestionsValidator;
10
import easytests.core.models.QuestionModel;
11
import easytests.core.models.QuestionModelInterface;
12
import easytests.core.models.TopicModelInterface;
13
import easytests.core.options.AnswersOptions;
14
import easytests.core.options.QuestionsOptions;
15
import easytests.core.options.QuestionsOptionsInterface;
16
import easytests.core.options.builder.QuestionsOptionsBuilderInterface;
17
import easytests.core.options.builder.TopicsOptionsBuilderInterface;
18
import easytests.core.services.QuestionsServiceInterface;
19
import easytests.core.services.TopicsServiceInterface;
20
import java.util.List;
21
import java.util.stream.Collectors;
22
import org.springframework.beans.factory.annotation.Autowired;
23
import org.springframework.beans.factory.annotation.Qualifier;
24
import org.springframework.http.HttpStatus;
25
import org.springframework.web.bind.annotation.*;
26
27
28
/**
29
 * @author RisaMagpie
30
 */
31
@RestController("QuestionsControllerV1")
32
@SuppressWarnings("checkstyle:MultipleStringLiterals")
33
@RequestMapping("/v1/questions")
34
public class QuestionsController extends AbstractController {
35
    @Autowired
36
    protected QuestionsServiceInterface questionsService;
37
38
    @Autowired
39
    protected TopicsServiceInterface topicsService;
40
41
    @Autowired
42
    protected QuestionsOptionsBuilderInterface questionsOptionsBuilder;
43
44
    @Autowired
45
    protected TopicsOptionsBuilderInterface topicsOptionsBuilder;
46
47
    @Autowired
48
    @Qualifier("QuestionsMapperV1")
49
    private QuestionsMapper questionsMapper;
50
51
    @GetMapping
52
    public List<Question> list(@RequestParam(name = "topicId", required = true) Integer topicId)
53
            throws NotFoundException, ForbiddenException {
54
        final TopicModelInterface topicModel = this.topicsService.find(topicId, this.topicsOptionsBuilder.forAuth());
55
56
        if (topicModel == null) {
57
            throw new NotFoundException();
58
        }
59
60
        if (!this.acl.hasAccess(topicModel)) {
61
            throw new ForbiddenException();
62
        }
63
64
        final List<QuestionModelInterface> questionsModels =
65
                this.questionsService.findByTopic(topicModel, new QuestionsOptions().withAnswers(new AnswersOptions()));
66
67
        return questionsModels
68
                .stream()
69
                .map(model -> this.questionsMapper.map(model, Question.class))
70
                .collect(Collectors.toList());
71
    }
72
73
    /**
74
     * create
75
     */
76
77
    @PostMapping("")
78
    @ResponseStatus(HttpStatus.CREATED)
79
    public Identity create(@RequestBody Question question) throws Exception {
0 ignored issues
show
Best Practice introduced by
Dedicated exceptions should be preferred over throwing the generic Exception.
Loading history...
80
81
        if (question.getId() != null) {
82
            throw new IdentifiedModelException();
83
        }
84
85
        final QuiestionsValidator quiestionsValidator = new QuiestionsValidator();
0 ignored issues
show
Unused Code introduced by
Consider removing the unused local variable quiestionsValidator.
Loading history...
Unused Code Code Smell introduced by
Remove this useless assignment to local variable "quiestionsValidator".
Loading history...
86
87
        final QuestionsOptions questionsOptions = new QuestionsOptions();
88
89
        final AnswersOptions answersOptions = new AnswersOptions();
90
91
        final QuestionModelInterface questionModel = this.questionsMapper.map(question, QuestionModel.class);
92
93
        questionsService.save(questionModel, questionsOptions.withAnswers(answersOptions));
94
95
        return this.questionsMapper.map(questionModel, Identity.class);
96
    }
97
98
    /**
99
     * update
100
     */
101
102
    @GetMapping("/{questionId}")
103
    public Question show(@PathVariable Integer questionId) throws NotFoundException, ForbiddenException {
104
        final QuestionModelInterface questionModel = this.getQuestionModel(
105
                questionId,
106
                this.questionsOptionsBuilder.forAuth().withAnswers(new AnswersOptions())
107
        );
108
        if (!this.acl.hasAccess(questionModel)) {
109
            throw new ForbiddenException();
110
        }
111
        return this.questionsMapper.map(questionModel, Question.class);
112
    }
113
114
    private QuestionModelInterface getQuestionModel(Integer id, QuestionsOptionsInterface
115
            questionOptions) throws NotFoundException {
116
        final QuestionModelInterface questionModel = this.questionsService.find(id, questionOptions);
117
        if (questionModel == null) {
118
            throw new NotFoundException();
119
        }
120
        return questionModel;
121
    }
122
    /**
123
     * delete(questionId)
124
     */
125
}
126