Completed
Pull Request — dev (#400)
by
unknown
06:00 queued 01:09
created

easytests.api.v1.controllers.TopicsController   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 13
c 1
b 1
f 0
dl 0
loc 100
rs 10

9 Methods

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