Completed
Pull Request — dev (#401)
by
unknown
06:25
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)
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
        if (!this.acl.hasAccess(questionModel)) {
81
            throw new ForbiddenException();
82
        }
83
        this.checkTopic(question);
84
85
        final QuestionsOptionsInterface questionOptionWithAnswers =
86
                new QuestionsOptions().withAnswers(new AnswersOptions());
87
88
        final QuestionModelInterface questionModelWithAnswers =
89
                this.questionsService.find(question.getId(), questionOptionWithAnswers);
90
91
        this.questionsMapper.map(question, questionModelWithAnswers);
92
        this.questionsService.save(questionModelWithAnswers, questionOptionWithAnswers);
93
    }
94
    
95
    @GetMapping("/{questionId}")
96
    public Question show(@PathVariable Integer questionId) throws NotFoundException, ForbiddenException {
97
        final QuestionModelInterface questionModel = this.getQuestionModel(
98
                questionId,
99
                this.questionsOptionsBuilder.forAuth().withAnswers(new AnswersOptions())
100
        );
101
        if (!this.acl.hasAccess(questionModel)) {
102
            throw new ForbiddenException();
103
        }
104
        return this.questionsMapper.map(questionModel, Question.class);
105
    }
106
107
    @DeleteMapping("/{questionId}")
108
    public void delete(@PathVariable Integer questionId) throws NotFoundException, ForbiddenException {
109
        final QuestionModelInterface questionModel = this.getQuestionModel(questionId);
110
        final QuestionsOptionsInterface questionOption = this.questionsOptionsBuilder.forDelete();
111
112
        if (!this.acl.hasAccess(questionModel)) {
113
            throw new ForbiddenException();
114
        }
115
116
        final QuestionModelInterface questionModelForDelete = this.questionsService.find(questionId, questionOption);
117
        this.questionsService.delete(questionModelForDelete, questionOption);
118
    }
119
120
    private void checkTopic(Question question) throws BadRequestException {
121
        final TopicModelInterface topicModel = this.topicsService.find(
122
                question.getTopic().getId(),
123
                this.topicsOptionsBuilder.forAuth()
124
        );
125
126
        if (!this.acl.hasAccess(topicModel)) {
127
            throw new BadRequestException();
128
        }
129
    }
130
131
    private QuestionModelInterface getQuestionModel(Integer id, QuestionsOptionsInterface
132
            questionOptions) throws NotFoundException {
133
        final QuestionModelInterface questionModel = this.questionsService.find(id, questionOptions);
134
        if (questionModel == null) {
135
            throw new NotFoundException();
136
        }
137
        return questionModel;
138
    }
139
140
    private QuestionModelInterface getQuestionModel(Integer id) throws NotFoundException {
141
        return this.getQuestionModel(id, this.questionsOptionsBuilder.forAuth());
142
    }
143
}
144