Passed
Pull Request — dev (#400)
by
unknown
06:30
created

easytests.api.v1.controllers.TopicsController   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Importance

Changes 6
Bugs 3 Features 0
Metric Value
wmc 15
c 6
b 3
f 0
dl 0
loc 118
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A list(Integer) 0 20 3
show 0 12 ?
A show(Integer) 0 12 3
A update(Topic) 0 13 2
getTopicModel 0 7 ?
A getTopicModel(Integer) 0 2 1
A checkSubject(Topic) 0 7 2
A delete(Integer) 0 11 2
A getTopicModel(Integer,TopicsOptionsInterface) 0 7 2
checkSubject 0 7 ?
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
 * @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
75
    @PutMapping("")
76
    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...
77
        if (topic.getId() == null) {
78
            throw new UnidentifiedModelException();
79
        }
80
81
        final TopicModelInterface topicModel = this.getTopicModel(topic.getId());
82
83
        this.checkSubject(topic);
84
85
        this.topicsMapper.map(topic, topicModel);
86
87
        this.topicsService.save(topicModel);
88
    }
89
90
    @GetMapping("/{topicId}")
91
    public Topic show(@PathVariable Integer topicId) throws NotFoundException, ForbiddenException {
92
        final TopicModelInterface topicModel = this.topicsService.find(topicId);
93
94
        if (topicModel == null) {
95
            throw new NotFoundException();
96
        }
97
98
        if (!this.acl.hasAccess(topicModel)) {
99
            throw new ForbiddenException();
100
        }
101
        return this.topicsMapper.map(topicModel, Topic.class);
102
    }
103
104
    /**
105
     * delete(topicId)
106
     */
107
108
    @DeleteMapping("/{topicId}")
109
    public void delete(@PathVariable Integer topicId) throws NotFoundException, ForbiddenException {
110
        final TopicModelInterface topicModelforAuth = this.getTopicModel(topicId);
111
112
        if (!this.acl.hasAccess(topicModelforAuth)) {
113
            throw new ForbiddenException();
114
        }
115
        final TopicsOptionsInterface topicsOptionDelete = this.topicsOptionsBuilder.forDelete();
116
        final TopicModelInterface topicModelForDelete = this.topicsService.find(topicId, topicsOptionDelete);
117
118
        this.topicsService.delete(topicModelForDelete, topicsOptionDelete);
119
    }
120
121
    private void checkSubject(Topic topic) throws ForbiddenException, BadRequestException {
122
        final SubjectModelInterface subjectModel = this.subjectsService.find(
123
                topic.getSubject().getId(),
124
                this.subjectsOptionsBuilder.forAuth()
125
        );
126
127
        if (!this.acl.hasAccess(subjectModel)) {
128
            throw new BadRequestException();
129
        }
130
    }
131
132
    private TopicModelInterface getTopicModel(Integer id, TopicsOptionsInterface topicOptions)
133
            throws NotFoundException {
134
        final TopicModelInterface topicModel = this.topicsService.find(id, topicOptions);
135
        if (topicModel == null) {
136
            throw new NotFoundException();
137
        }
138
        return topicModel;
139
    }
140
141
    private TopicModelInterface getTopicModel(Integer id) throws NotFoundException {
142
        return this.getTopicModel(id, this.topicsOptionsBuilder.forAuth());
143
    }
144
145
}
146