Completed
Pull Request — dev (#401)
by
unknown
05:29
created

getQuestionModel(Integer)   A

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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