Passed
Pull Request — dev (#400)
by
unknown
04:23
created

easytests.api.v1.controllers.TopicsController   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
dl 0
loc 86
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A list(Integer) 0 20 3
getTopicModel 0 6 ?
show 0 12 ?
A getTopicModel(Integer) 0 2 1
A getTopicModel(Integer,TopicsOptionsInterface) 0 6 2
A show(Integer) 0 12 3
A update(Topic) 0 11 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.TopicsOptionsBuilderInterface;
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 SubjectsServiceInterface subjectsService;
32
33
    @Autowired
34
    protected TopicsServiceInterface topicsService;
35
36
    @Autowired
37
    private TopicsOptionsBuilderInterface topicsOptionsBuilder;
38
39
    @Autowired
40
    @Qualifier("TopicsMapperV1")
41
    private TopicsMapper topicsMapper;
42
43
    @GetMapping("")
44
    public List<Topic> list(@RequestParam(name = "subjectId", required = true) Integer subjectId)
45
            throws NotFoundException, ForbiddenException {
46
        final SubjectModelInterface subjectModel = this.subjectsService.find(subjectId);
47
48
        if (subjectModel == null) {
49
            throw new NotFoundException();
50
        }
51
52
        if (!this.acl.hasAccess(subjectModel)) {
53
            throw new ForbiddenException();
54
        }
55
56
        final List<TopicModelInterface> topicsModels =
57
                this.topicsService.findBySubject(subjectModel);
58
59
        return topicsModels
60
                .stream()
61
                .map(model -> this.topicsMapper.map(model, Topic.class))
62
                .collect(Collectors.toList());
63
    }
64
65
    /**
66
     * create
67
     */
68
    /**
69
     * update
70
     */
71
    @PutMapping("")
72
    public void update(@RequestBody Topic topic) throws BadRequestException, NotFoundException {
73
        if (topic.getId() == null) {
74
            throw new UnidentifiedModelException();
75
        }
76
77
        final TopicModelInterface topicModel = this.getTopicModel(topic.getId());
78
79
        this.topicsMapper.map(topic, topicModel);
80
81
        this.topicsService.save(topicModel);
82
    }
83
84
    @GetMapping("/{topicId}")
85
    public Topic show(@PathVariable Integer topicId) throws NotFoundException, ForbiddenException {
86
        final TopicModelInterface topicModel = this.topicsService.find(topicId);
87
88
        if (topicModel == null) {
89
            throw new NotFoundException();
90
        }
91
92
        if (!this.acl.hasAccess(topicModel)) {
93
            throw new ForbiddenException();
94
        }
95
        return this.topicsMapper.map(topicModel, Topic.class);
96
    }
97
    /**
98
     * delete(topicId)
99
     */
100
101
    private TopicModelInterface getTopicModel(Integer id, TopicsOptionsInterface userOptions) throws NotFoundException {
102
        final TopicModelInterface topicModel = this.topicsService.find(id, userOptions);
103
        if (topicModel == null) {
104
            throw new NotFoundException();
105
        }
106
        return topicModel;
107
    }
108
109
    private TopicModelInterface getTopicModel(Integer id) throws NotFoundException {
110
        return this.getTopicModel(id, this.topicsOptionsBuilder.forAuth());
111
    }
112
}
113