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

easytests.api.v1.controllers.QuestionsController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 3
Bugs 2 Features 0
Metric Value
wmc 5
c 3
b 2
f 0
dl 0
loc 49
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getQuestionModel(Integer,QuestionsOptionsInterface) 0 7 2
A delete(Integer) 0 9 2
A getQuestionModel(Integer) 0 2 1
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