Completed
Pull Request — dev (#392)
by
unknown
05:10
created

delete(Integer)   A

Complexity

Conditions 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 9
rs 9.95
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.core.models.QuestionModelInterface;
6
import easytests.core.options.QuestionsOptionsInterface;
7
import easytests.core.options.builder.QuestionsOptionsBuilderInterface;
8
import easytests.core.services.QuestionsService;
9
import org.springframework.beans.factory.annotation.Autowired;
10
import org.springframework.beans.factory.annotation.Qualifier;
11
import org.springframework.web.bind.annotation.*;
12
13
/**
14
 * @author RisaMagpie
15
 */
16
@RestController("QuestionControllerV1")
17
@SuppressWarnings("checkstyle:MultipleStringLiterals")
18
@RequestMapping("/v1/questions")
19
public class QuestionsController extends AbstractController {
20
21
    @Autowired
22
    protected QuestionsService questionsService;
23
24
    @Autowired
25
    protected QuestionsOptionsBuilderInterface questionsOptionsBuilder;
26
27
    @Autowired
28
    @Qualifier("QuestionsMapperV1")
29
    private QuestionsMapper questionsMapper;
30
31
    /**
32
     * list
33
     */
34
    /**
35
     * create
36
     */
37
    /**
38
     * update
39
     */
40
    /**
41
     * show(questionId)
42
     */
43
    @DeleteMapping("/{questionId}")
44
    public void delete(@PathVariable Integer questionId) throws NotFoundException, ForbiddenException {
45
        final QuestionModelInterface questionModel = this.getQuestionModel(questionId);
46
47
        if (!this.acl.hasAccess(questionModel)) {
48
            throw new ForbiddenException();
49
        }
50
        
51
        this.questionsService.delete(questionModel);
52
    }
53
54
    private QuestionModelInterface getQuestionModel(Integer id, QuestionsOptionsInterface questionOptions)
55
            throws NotFoundException {
56
        final QuestionModelInterface questionModel = this.questionsService.find(id, questionOptions);
57
        if (questionModel == null) {
58
            throw new NotFoundException();
59
        }
60
        return questionModel;
61
    }
62
63
    private QuestionModelInterface getQuestionModel(Integer id) throws NotFoundException {
64
        return this.getQuestionModel(id, this.questionsOptionsBuilder.forDelete());
65
    }
66
67
}
68