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

create(Question)   A

Complexity

Conditions 3

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 17
rs 9.55
cc 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.core.models.QuestionModel;
10
import easytests.core.models.QuestionModelInterface;
11
import easytests.core.models.TopicModelInterface;
12
import easytests.core.options.AnswersOptions;
13
import easytests.core.options.QuestionsOptions;
14
import easytests.core.options.QuestionsOptionsInterface;
15
import easytests.core.options.builder.QuestionsOptionsBuilderInterface;
16
import easytests.core.options.builder.TopicsOptionsBuilderInterface;
17
import easytests.core.services.QuestionsServiceInterface;
18
import easytests.core.services.TopicsServiceInterface;
19
import java.util.List;
20
import java.util.stream.Collectors;
21
import org.springframework.beans.factory.annotation.Autowired;
22
import org.springframework.beans.factory.annotation.Qualifier;
23
import org.springframework.http.HttpStatus;
24
import org.springframework.web.bind.annotation.*;
25
26
27
/**
28
 * @author RisaMagpie
29
 */
30
@RestController("QuestionsControllerV1")
31
@SuppressWarnings("checkstyle:MultipleStringLiterals")
32
@RequestMapping("/v1/questions")
33
public class QuestionsController extends AbstractController {
34
    @Autowired
35
    protected QuestionsServiceInterface questionsService;
36
37
    @Autowired
38
    protected TopicsServiceInterface topicsService;
39
40
    @Autowired
41
    protected QuestionsOptionsBuilderInterface questionsOptionsBuilder;
42
43
    @Autowired
44
    protected TopicsOptionsBuilderInterface topicsOptionsBuilder;
45
46
    @Autowired
47
    @Qualifier("QuestionsMapperV1")
48
    private QuestionsMapper questionsMapper;
49
50
    @GetMapping
51
    public List<Question> list(@RequestParam(name = "topicId", required = true) Integer topicId)
52
            throws NotFoundException, ForbiddenException {
53
        final TopicModelInterface topicModel = this.topicsService.find(topicId, this.topicsOptionsBuilder.forAuth());
54
55
        if (topicModel == null) {
56
            throw new NotFoundException();
57
        }
58
59
        if (!this.acl.hasAccess(topicModel)) {
60
            throw new ForbiddenException();
61
        }
62
63
        final List<QuestionModelInterface> questionsModels =
64
                this.questionsService.findByTopic(topicModel, new QuestionsOptions().withAnswers(new AnswersOptions()));
65
66
        return questionsModels
67
                .stream()
68
                .map(model -> this.questionsMapper.map(model, Question.class))
69
                .collect(Collectors.toList());
70
    }
71
72
    /**
73
     * create
74
     */
75
76
    @PostMapping("")
77
    @ResponseStatus(HttpStatus.CREATED)
78
    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...
79
        final TopicModelInterface topicModel = this.topicsService.find(question.getTopic().getId(), this.topicsOptionsBuilder.forAuth());
80
        if (!this.acl.hasAccess(topicModel)) {
81
            throw new ForbiddenException();
82
        }
83
84
        if (question.getId() != null) {
85
            throw new IdentifiedModelException();
86
        }
87
88
        final QuestionModelInterface questionModel = this.questionsMapper.map(question, QuestionModel.class);
89
90
        this.questionsService.save(questionModel, new QuestionsOptions().withAnswers(new AnswersOptions()));
91
92
        return this.questionsMapper.map(questionModel, Identity.class);
93
    }
94
95
    /**
96
     * update
97
     */
98
99
    @GetMapping("/{questionId}")
100
    public Question show(@PathVariable Integer questionId) throws NotFoundException, ForbiddenException {
101
        final QuestionModelInterface questionModel = this.getQuestionModel(
102
                questionId,
103
                this.questionsOptionsBuilder.forAuth().withAnswers(new AnswersOptions())
104
        );
105
        if (!this.acl.hasAccess(questionModel)) {
106
            throw new ForbiddenException();
107
        }
108
        return this.questionsMapper.map(questionModel, Question.class);
109
    }
110
111
    private QuestionModelInterface getQuestionModel(Integer id, QuestionsOptionsInterface
112
            questionOptions) throws NotFoundException {
113
        final QuestionModelInterface questionModel = this.questionsService.find(id, questionOptions);
114
        if (questionModel == null) {
115
            throw new NotFoundException();
116
        }
117
        return questionModel;
118
    }
119
    /**
120
     * delete(questionId)
121
     */
122
123
    private QuestionModelInterface getQuestionModel(Integer id) throws NotFoundException {
124
        return this.getQuestionModel(id, this.questionsOptionsBuilder.forAuth());
125
    }
126
127
    @DeleteMapping("/{questionId}")
128
    public void delete(@PathVariable Integer questionId) throws NotFoundException, ForbiddenException {
129
        final QuestionModelInterface questionModel = this.getQuestionModel(questionId);
130
        final QuestionsOptionsInterface questionOption = this.questionsOptionsBuilder.forDelete();
131
132
        if (!this.acl.hasAccess(questionModel)) {
133
            throw new ForbiddenException();
134
        }
135
136
        final QuestionModelInterface questionModelForDelete = this.questionsService.find(questionId, questionOption);
137
        this.questionsService.delete(questionModelForDelete, questionOption);
138
    }
139
140
}
141