Passed
Pull Request — dev (#395)
by Konstantin
05:34
created

easytests.api.v1.controllers.TopicsController   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Importance

Changes 5
Bugs 2 Features 0
Metric Value
wmc 15
c 5
b 2
f 0
dl 0
loc 119
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
getTopicModel 0 7 ?
show 0 12 ?
A getTopicModel(Integer) 0 2 1
A create(Topic) 0 14 2
A checkSubject(Topic) 0 6 2
A delete(Integer) 0 11 2
A getTopicModel(Integer,TopicsOptionsInterface) 0 7 2
checkSubject 0 6 ?
A list(Integer) 0 20 3
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.TopicsOptionsInterface;
14
import easytests.core.options.builder.SubjectsOptionsBuilder;
15
import easytests.core.options.builder.TopicsOptionsBuilder;
16
import easytests.core.services.SubjectsServiceInterface;
17
import easytests.core.services.TopicsServiceInterface;
18
import java.util.List;
19
import java.util.stream.Collectors;
20
import org.springframework.beans.factory.annotation.Autowired;
21
import org.springframework.beans.factory.annotation.Qualifier;
22
import org.springframework.http.HttpStatus;
23
import org.springframework.web.bind.annotation.*;
24
25
/**
26
 * @author lelay
27
 */
28
@RestController("TopicsControllerV1")
29
@SuppressWarnings("checkstyle:MultipleStringLiterals")
30
@RequestMapping("/v1/topics")
31
public class TopicsController extends AbstractController {
32
33
    @Autowired
34
    protected SubjectsOptionsBuilder subjectsOptionsBuilder;
35
36
    @Autowired
37
    protected TopicsServiceInterface topicsService;
38
39
    @Autowired
40
    protected SubjectsServiceInterface subjectsService;
41
42
    @Autowired
43
    protected TopicsOptionsBuilder topicsOptionsBuilder;
44
45
    @Autowired
46
    @Qualifier("TopicsMapperV1")
47
    private TopicsMapper topicsMapper;
48
49
    @GetMapping("")
50
    public List<Topic> list(@RequestParam(name = "subjectId", required = true) Integer subjectId)
51
            throws NotFoundException, ForbiddenException {
52
        final SubjectModelInterface subjectModel = this.subjectsService.find(subjectId);
53
54
        if (subjectModel == null) {
55
            throw new NotFoundException();
56
        }
57
58
        if (!this.acl.hasAccess(subjectModel)) {
59
            throw new ForbiddenException();
60
        }
61
62
        final List<TopicModelInterface> topicsModels =
63
                this.topicsService.findBySubject(subjectModel);
64
65
        return topicsModels
66
                .stream()
67
                .map(model -> this.topicsMapper.map(model, Topic.class))
68
                .collect(Collectors.toList());
69
    }
70
71
    /**
72
     * create
73
     */
74
75
    @PostMapping("")
76
    @ResponseStatus(HttpStatus.CREATED)
77
    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...
78
        if (topic.getId() != null) {
79
            throw new IdentifiedModelException();
80
        }
81
82
        this.checkSubject(topic);
83
84
        final TopicModelInterface topicModel = this.topicsMapper.map(topic, TopicModel.class);
85
86
        this.topicsService.save(topicModel);
87
88
        return this.topicsMapper.map(topicModel, Identity.class);
89
    }
90
91
    private void checkSubject(Topic topic) throws BadRequestException {
92
        final SubjectModelInterface subjectModel = this.subjectsService.find(
93
                topic.getSubject().getId(),
94
                this.subjectsOptionsBuilder.forAuth()
95
                                );
96
        if (!this.acl.hasAccess(subjectModel)) {
97
            throw new BadRequestException();
98
        }
99
100
    }
101
102
    /**
103
     * update
104
     */
105
    @GetMapping("/{topicId}")
106
    public Topic show(@PathVariable Integer topicId) throws NotFoundException, ForbiddenException {
107
        final TopicModelInterface topicModel = this.topicsService.find(topicId);
108
109
        if (topicModel == null) {
110
            throw new NotFoundException();
111
        }
112
113
        if (!this.acl.hasAccess(topicModel)) {
114
            throw new ForbiddenException();
115
        }
116
        return this.topicsMapper.map(topicModel, Topic.class);
117
    }
118
119
    /**
120
     * delete(topicId)
121
     */
122
123
    @DeleteMapping("/{topicId}")
124
    public void delete(@PathVariable Integer topicId) throws NotFoundException, ForbiddenException {
125
        final TopicModelInterface topicModelforAuth = this.getTopicModel(topicId);
126
127
        if (!this.acl.hasAccess(topicModelforAuth)) {
128
            throw new ForbiddenException();
129
        }
130
        final TopicsOptionsInterface topicsOptionDelete = this.topicsOptionsBuilder.forDelete();
131
        final TopicModelInterface topicModelForDelete = this.topicsService.find(topicId, topicsOptionDelete);
132
133
        this.topicsService.delete(topicModelForDelete, topicsOptionDelete);
134
    }
135
136
    private TopicModelInterface getTopicModel(Integer id, TopicsOptionsInterface topicOptions)
137
            throws NotFoundException {
138
        final TopicModelInterface topicModel = this.topicsService.find(id, topicOptions);
139
        if (topicModel == null) {
140
            throw new NotFoundException();
141
        }
142
        return topicModel;
143
    }
144
145
    private TopicModelInterface getTopicModel(Integer id) throws NotFoundException {
146
        return this.getTopicModel(id, this.topicsOptionsBuilder.forAuth());
147
    }
148
}
149