Completed
Pull Request — dev (#395)
by
unknown
04:30
created

easytests.api.v1.controllers.TopicsController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
show 0 12 ?
A create(Topic) 0 12 2
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.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.TopicsOptionsBuilderInterface;
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 SubjectsServiceInterface subjectsService;
34
35
    @Autowired
36
    protected TopicsServiceInterface topicsService;
37
38
    @Autowired
39
    private TopicsOptionsBuilderInterface topicsOptionsBuilder;
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 BadRequestException {
74
        if (topic.getId() != null) {
75
            throw new IdentifiedModelException();
76
        }
77
78
        final TopicModelInterface userModel = this.topicsMapper.map(topic, TopicModel.class);
79
80
        this.topicsService.save(userModel);
81
82
        return this.topicsMapper.map(userModel, Identity.class);
83
    }
84
85
    /**
86
     * update
87
     */
88
    @GetMapping("/{topicId}")
89
    public Topic show(@PathVariable Integer topicId) throws NotFoundException, ForbiddenException {
90
        final TopicModelInterface topicModel = this.topicsService.find(topicId);
91
92
        if (topicModel == null) {
93
            throw new NotFoundException();
94
        }
95
96
        if (!this.acl.hasAccess(topicModel)) {
97
            throw new ForbiddenException();
98
        }
99
        return this.topicsMapper.map(topicModel, Topic.class);
100
    }
101
    /**
102
     * delete(topicId)
103
     */
104
}
105