Completed
Pull Request — dev (#400)
by
unknown
04:31
created

getTopicModel(Integer,TopicsOptionsInterface)   A

Complexity

Conditions 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
1
package easytests.api.v1.controllers;
2
3
import easytests.api.v1.exceptions.BadRequestException;
4
import easytests.api.v1.exceptions.ForbiddenException;
5
import easytests.api.v1.exceptions.NotFoundException;
6
import easytests.api.v1.exceptions.UnidentifiedModelException;
7
import easytests.api.v1.mappers.TopicsMapper;
8
import easytests.api.v1.models.Topic;
9
import easytests.core.models.SubjectModelInterface;
10
import easytests.core.models.TopicModelInterface;
11
import easytests.core.options.TopicsOptionsInterface;
12
import easytests.core.options.builder.SubjectsOptionsBuilder;
13
import easytests.core.options.builder.TopicsOptionsBuilderInterface;
14
import easytests.core.services.SubjectsServiceInterface;
15
import easytests.core.services.TopicsServiceInterface;
16
import java.util.List;
17
import java.util.stream.Collectors;
18
import org.springframework.beans.factory.annotation.Autowired;
19
import org.springframework.beans.factory.annotation.Qualifier;
20
import org.springframework.web.bind.annotation.*;
21
22
23
/**
24
 * @author lelay
25
 */
26
@RestController("TopicsControllerV1")
27
@SuppressWarnings("checkstyle:MultipleStringLiterals")
28
@RequestMapping("/v1/topics")
29
public class TopicsController extends AbstractController {
30
31
    @Autowired
32
    protected SubjectsOptionsBuilder subjectsOptionsBuilder;
33
34
    @Autowired
35
    protected SubjectsServiceInterface subjectsService;
36
37
    @Autowired
38
    protected TopicsServiceInterface topicsService;
39
40
    @Autowired
41
    protected TopicsOptionsBuilderInterface topicsOptionsBuilder;
42
43
    @Autowired
44
    @Qualifier("TopicsMapperV1")
45
    private TopicsMapper topicsMapper;
46
47
    @GetMapping("")
48
    public List<Topic> list(@RequestParam(name = "subjectId", required = true) Integer subjectId)
49
            throws NotFoundException, ForbiddenException {
50
        final SubjectModelInterface subjectModel = this.subjectsService.find(subjectId);
51
52
        if (subjectModel == null) {
53
            throw new NotFoundException();
54
        }
55
56
        if (!this.acl.hasAccess(subjectModel)) {
57
            throw new ForbiddenException();
58
        }
59
60
        final List<TopicModelInterface> topicsModels =
61
                this.topicsService.findBySubject(subjectModel);
62
63
        return topicsModels
64
                .stream()
65
                .map(model -> this.topicsMapper.map(model, Topic.class))
66
                .collect(Collectors.toList());
67
    }
68
69
    /**
70
     * create
71
     */
72
    /**
73
     * update
74
     */
75
76
    @PutMapping("")
77
    public void update(@RequestBody Topic topic) throws Exception {
0 ignored issues
show
Best Practice introduced by
Dedicated exceptions should be preferred over throwing the generic Exception.
Loading history...
78
        if (topic.getId() == null) {
79
            throw new UnidentifiedModelException();
80
        }
81
82
        final TopicModelInterface topicModel = this.getTopicModel(topic.getId());
83
84
        this.checkSubject(topic);
85
86
        this.topicsMapper.map(topic, topicModel);
87
88
        this.topicsService.save(topicModel);
89
    }
90
91
    @GetMapping("/{topicId}")
92
    public Topic show(@PathVariable Integer topicId) throws NotFoundException, ForbiddenException {
93
        final TopicModelInterface topicModel = this.topicsService.find(topicId);
94
95
        if (topicModel == null) {
96
            throw new NotFoundException();
97
        }
98
99
        if (!this.acl.hasAccess(topicModel)) {
100
            throw new ForbiddenException();
101
        }
102
        return this.topicsMapper.map(topicModel, Topic.class);
103
    }
104
    /**
105
     * delete(topicId)
106
     */
107
108
    private TopicModelInterface getTopicModel(Integer id, TopicsOptionsInterface userOptions) throws NotFoundException {
109
        final TopicModelInterface topicModel = this.topicsService.find(id, userOptions);
110
        if (topicModel == null) {
111
            throw new NotFoundException();
112
        }
113
        return topicModel;
114
    }
115
116
    private TopicModelInterface getTopicModel(Integer id) throws NotFoundException {
117
        return this.getTopicModel(id, this.topicsOptionsBuilder.forAuth());
118
    }
119
120
    private void checkSubject(Topic topic) throws ForbiddenException, BadRequestException {
121
        final SubjectModelInterface subjectModel = this.subjectsService.find(
122
                topic.getSubject().getId(),
123
                this.subjectsOptionsBuilder.forAuth()
124
        );
125
126
        if (!this.acl.hasAccess(subjectModel)) {
127
            throw new BadRequestException();
128
        }
129
    }
130
}
131