Completed
Pull Request — dev (#331)
by
unknown
05:31
created

list(Integer)   A

Complexity

Conditions 3

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 20
rs 9.4
cc 3
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.mappers.TopicsMapper;
6
import easytests.api.v1.models.Topic;
7
import easytests.auth.services.AccessControlLayerServiceInterface;
0 ignored issues
show
Unused Code introduced by
Remove this unused import 'easytests.auth.services.AccessControlLayerServiceInterface'.
Loading history...
8
import easytests.core.models.SubjectModelInterface;
9
import easytests.core.models.TopicModelInterface;
10
import easytests.core.options.builder.TopicsOptionsBuilderInterface;
11
import easytests.core.services.SubjectsServiceInterface;
12
import easytests.core.services.TopicsServiceInterface;
13
import java.util.List;
14
import java.util.stream.Collectors;
15
import org.springframework.beans.factory.annotation.Autowired;
16
import org.springframework.beans.factory.annotation.Qualifier;
17
import org.springframework.web.bind.annotation.GetMapping;
18
import org.springframework.web.bind.annotation.RequestMapping;
19
import org.springframework.web.bind.annotation.RequestParam;
20
import org.springframework.web.bind.annotation.RestController;
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
    /**
72
     * show(topicId)
73
     */
74
    /**
75
     * delete(topicId)
76
     */
77
}
78