Completed
Push — dev ( 39c8fc...3a01d3 )
by Konstantin
11:12 queued 05:12
created

checkTopic(Integer)   A

Complexity

Conditions 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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