Passed
Pull Request — dev (#401)
by
unknown
06:43
created

checkTopic(Question)   A

Complexity

Conditions 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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