Completed
Pull Request — dev (#394)
by
unknown
04:28
created

list(Integer)   A

Complexity

Conditions 3

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 20
rs 9.4
cc 3
1
package easytests.api.v1.controllers;
2
3
import easytests.api.v1.exceptions.ForbiddenException;
4
import easytests.api.v1.exceptions.NotFoundException;
5
import easytests.api.v1.mappers.TopicsMapper;
6
import easytests.api.v1.models.Topic;
7
import easytests.core.models.SubjectModelInterface;
8
import easytests.core.models.TopicModelInterface;
9
import easytests.core.options.TopicsOptionsInterface;
10
import easytests.core.options.builder.TopicsOptionsBuilderInterface;
11
import easytests.core.services.SubjectsServiceInterface;
12
import easytests.core.services.TopicsServiceInterface;
13
import java.util.List;
14
import java.util.stream.Collectors;
15
import org.springframework.beans.factory.annotation.Autowired;
16
import org.springframework.beans.factory.annotation.Qualifier;
17
import org.springframework.web.bind.annotation.*;
18
19
/**
20
 * @author lelay
21
 */
22
@RestController("TopicsControllerV1")
23
@SuppressWarnings("checkstyle:MultipleStringLiterals")
24
@RequestMapping("/v1/topics")
25
public class TopicsController extends AbstractController {
26
27
    @Autowired
28
    protected SubjectsServiceInterface subjectsService;
29
30
    @Autowired
31
    protected TopicsServiceInterface topicsService;
32
33
    @Autowired
34
    private TopicsOptionsBuilderInterface topicsOptionsBuilder;
35
36
    @Autowired
37
    @Qualifier("TopicsMapperV1")
38
    private TopicsMapper topicsMapper;
39
40
    @GetMapping("")
41
    public List<Topic> list(@RequestParam(name = "subjectId", required = true) Integer subjectId)
42
            throws NotFoundException, ForbiddenException {
43
        final SubjectModelInterface subjectModel = this.subjectsService.find(subjectId);
44
45
        if (subjectModel == null) {
46
            throw new NotFoundException();
47
        }
48
49
        if (!this.acl.hasAccess(subjectModel)) {
50
            throw new ForbiddenException();
51
        }
52
53
        final List<TopicModelInterface> topicsModels =
54
                this.topicsService.findBySubject(subjectModel);
55
56
        return topicsModels
57
                .stream()
58
                .map(model -> this.topicsMapper.map(model, Topic.class))
59
                .collect(Collectors.toList());
60
    }
61
62
    /**
63
     * create
64
     */
65
    /**
66
     * update
67
     */
68
    @GetMapping("/{topicId}")
69
    public Topic show(@PathVariable Integer topicId) throws NotFoundException, ForbiddenException {
70
        final TopicModelInterface topicModel = this.topicsService.find(topicId);
71
72
        if (topicModel == null) {
73
            throw new NotFoundException();
74
        }
75
76
        if (!this.acl.hasAccess(topicModel)) {
77
            throw new ForbiddenException();
78
        }
79
        return this.topicsMapper.map(topicModel, Topic.class);
80
    }
81
82
    @DeleteMapping("/{topicId}")
83
    public void delete(@PathVariable Integer topicId) throws NotFoundException, ForbiddenException {
84
        final TopicModelInterface topicModelforAuth = getTopicModel(topicId);
85
86
        if (topicModelforAuth == null) {
0 ignored issues
show
Bug introduced by
This condition always evaluates to false. Consider refactoring your code to no longer check for it or rewrite the condition.
Loading history...
87
            throw new NotFoundException();
88
        }
89
90
        if (!this.acl.hasAccess(topicModelforAuth)) {
91
            throw new ForbiddenException();
92
        }
93
        final TopicsOptionsInterface topicsOptionDelete = this.topicsOptionsBuilder.forDelete();
94
        final TopicModelInterface topicModelForDelete = this.topicsService.find(topicId, topicsOptionDelete);
95
96
        this.topicsService.delete(topicModelForDelete, topicsOptionDelete);
97
    }
98
99
    private TopicModelInterface getTopicModel(Integer id, TopicsOptionsInterface topicOptions)
100
            throws NotFoundException {
101
        final TopicModelInterface topicModel = this.topicsService.find(id, topicOptions);
102
        if (topicModel == null) {
103
            throw new NotFoundException();
104
        }
105
        return topicModel;
106
    }
107
108
    private TopicModelInterface getTopicModel(Integer id) throws NotFoundException {
109
        return this.getTopicModel(id, this.topicsOptionsBuilder.forAuth());
110
    }
111
}
112