Completed
Pull Request — dev (#395)
by
unknown
06:56
created

easytests.api.v1.controllers.TopicsController   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
show 0 12 ?
A checkSubject(Topic) 0 6 2
A create(Topic) 0 14 2
checkSubject 0 6 ?
A show(Integer) 0 12 3
A list(Integer) 0 20 3
1
package easytests.api.v1.controllers;
2
3
import easytests.api.v1.exceptions.ForbiddenException;
4
import easytests.api.v1.exceptions.IdentifiedModelException;
5
import easytests.api.v1.exceptions.NotFoundException;
6
import easytests.api.v1.mappers.TopicsMapper;
7
import easytests.api.v1.models.Identity;
8
import easytests.api.v1.models.Topic;
9
import easytests.core.models.SubjectModelInterface;
10
import easytests.core.models.TopicModel;
11
import easytests.core.models.TopicModelInterface;
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.http.HttpStatus;
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 TopicsServiceInterface topicsService;
36
37
    @Autowired
38
    protected SubjectsServiceInterface subjectsService;
39
40
    @Autowired
41
    @Qualifier("TopicsMapperV1")
42
    private TopicsMapper topicsMapper;
43
44
    @GetMapping("")
45
    public List<Topic> list(@RequestParam(name = "subjectId", required = true) Integer subjectId)
46
            throws NotFoundException, ForbiddenException {
47
        final SubjectModelInterface subjectModel = this.subjectsService.find(subjectId);
48
49
        if (subjectModel == null) {
50
            throw new NotFoundException();
51
        }
52
53
        if (!this.acl.hasAccess(subjectModel)) {
54
            throw new ForbiddenException();
55
        }
56
57
        final List<TopicModelInterface> topicsModels =
58
                this.topicsService.findBySubject(subjectModel);
59
60
        return topicsModels
61
                .stream()
62
                .map(model -> this.topicsMapper.map(model, Topic.class))
63
                .collect(Collectors.toList());
64
    }
65
66
    /**
67
     * create
68
     */
69
70
    @PostMapping("")
71
    @ResponseStatus(HttpStatus.CREATED)
72
    public Identity create(@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...
73
        if (topic.getId() != null) {
74
            throw new IdentifiedModelException();
75
        }
76
77
        this.checkSubject(topic);
78
79
        final TopicModelInterface topicModel = this.topicsMapper.map(topic, TopicModel.class);
80
81
        this.topicsService.save(topicModel);
82
83
        return this.topicsMapper.map(topicModel, Identity.class);
84
    }
85
86
    private void checkSubject(Topic topic) throws ForbiddenException {
87
        final SubjectModelInterface subjectModel = this.subjectsService.find(
88
                topic.getSubject().getId(),
89
                this.subjectsOptionsBuilder.forAuth()
90
                                );
91
        if (!this.acl.hasAccess(subjectModel)) {
92
            throw new ForbiddenException();
93
        }
94
95
    }
96
97
    /**
98
     * update
99
     */
100
    @GetMapping("/{topicId}")
101
    public Topic show(@PathVariable Integer topicId) throws NotFoundException, ForbiddenException {
102
        final TopicModelInterface topicModel = this.topicsService.find(topicId);
103
104
        if (topicModel == null) {
105
            throw new NotFoundException();
106
        }
107
108
        if (!this.acl.hasAccess(topicModel)) {
109
            throw new ForbiddenException();
110
        }
111
        return this.topicsMapper.map(topicModel, Topic.class);
112
    }
113
114
    /**
115
     * delete(topicId)
116
     */
117
}
118