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