Completed
Pull Request — dev (#396)
by
unknown
11:03 queued 06:32
created

getQuestionModel(Integer,QuestionsOptionsInterface)   A

Complexity

Conditions 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 2 Features 1
Metric Value
c 4
b 2
f 1
dl 0
loc 7
rs 10
cc 2
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
80
                .find(question.getTopic().getId(), this.topicsOptionsBuilder.forAuth());
81
        if (!this.acl.hasAccess(topicModel)) {
82
            throw new ForbiddenException();
83
        }
84
85
        if (question.getId() != null) {
86
            throw new IdentifiedModelException();
87
        }
88
89
        final QuestionModelInterface questionModel = this.questionsMapper.map(question, QuestionModel.class);
90
91
        this.questionsService.save(questionModel, new QuestionsOptions().withAnswers(new AnswersOptions()));
92
93
        return this.questionsMapper.map(questionModel, Identity.class);
94
    }
95
96
    /**
97
     * update
98
     */
99
100
    @GetMapping("/{questionId}")
101
    public Question show(@PathVariable Integer questionId) throws NotFoundException, ForbiddenException {
102
        final QuestionModelInterface questionModel = this.getQuestionModel(
103
                questionId,
104
                this.questionsOptionsBuilder.forAuth().withAnswers(new AnswersOptions())
105
        );
106
        if (!this.acl.hasAccess(questionModel)) {
107
            throw new ForbiddenException();
108
        }
109
        return this.questionsMapper.map(questionModel, Question.class);
110
    }
111
112
    private QuestionModelInterface getQuestionModel(Integer id, QuestionsOptionsInterface
113
            questionOptions) throws NotFoundException {
114
        final QuestionModelInterface questionModel = this.questionsService.find(id, questionOptions);
115
        if (questionModel == null) {
116
            throw new NotFoundException();
117
        }
118
        return questionModel;
119
    }
120
    /**
121
     * delete(questionId)
122
     */
123
124
    private QuestionModelInterface getQuestionModel(Integer id) throws NotFoundException {
125
        return this.getQuestionModel(id, this.questionsOptionsBuilder.forAuth());
126
    }
127
128
    @DeleteMapping("/{questionId}")
129
    public void delete(@PathVariable Integer questionId) throws NotFoundException, ForbiddenException {
130
        final QuestionModelInterface questionModel = this.getQuestionModel(questionId);
131
        final QuestionsOptionsInterface questionOption = this.questionsOptionsBuilder.forDelete();
132
133
        if (!this.acl.hasAccess(questionModel)) {
134
            throw new ForbiddenException();
135
        }
136
137
        final QuestionModelInterface questionModelForDelete = this.questionsService.find(questionId, questionOption);
138
        this.questionsService.delete(questionModelForDelete, questionOption);
139
    }
140
141
}
142