Completed
Push — dev ( ad4cc5...91b440 )
by Konstantin
08:19 queued 03:36
created

getTopicModel(Integer)   A

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 2
rs 10
cc 1
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 = this.getTopicModel(topicId);
85
86
        if (!this.acl.hasAccess(topicModelforAuth)) {
87
            throw new ForbiddenException();
88
        }
89
        final TopicsOptionsInterface topicsOptionDelete = this.topicsOptionsBuilder.forDelete();
90
        final TopicModelInterface topicModelForDelete = this.topicsService.find(topicId, topicsOptionDelete);
91
92
        this.topicsService.delete(topicModelForDelete, topicsOptionDelete);
93
    }
94
95
    private TopicModelInterface getTopicModel(Integer id, TopicsOptionsInterface topicOptions)
96
            throws NotFoundException {
97
        final TopicModelInterface topicModel = this.topicsService.find(id, topicOptions);
98
        if (topicModel == null) {
99
            throw new NotFoundException();
100
        }
101
        return topicModel;
102
    }
103
104
    private TopicModelInterface getTopicModel(Integer id) throws NotFoundException {
105
        return this.getTopicModel(id, this.topicsOptionsBuilder.forAuth());
106
    }
107
}
108