Passed
Pull Request — dev (#381)
by
unknown
06:54
created

show(Integer)   A

Complexity

Conditions 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.85
cc 2
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.SubjectsMapper;
6
import easytests.api.v1.models.Subject;
7
import easytests.core.models.SubjectModelInterface;
8
import easytests.core.models.UserModelInterface;
9
import easytests.core.options.builder.SubjectsOptionsBuilderInterface;
10
import easytests.core.options.SubjectsOptions;
11
import easytests.core.options.SubjectsOptionsInterface;
12
import easytests.core.options.TopicsOptions;
13
import easytests.core.services.SubjectsServiceInterface;
14
import easytests.core.services.UsersServiceInterface;
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.web.bind.annotation.*;
20
import org.springframework.web.bind.annotation.GetMapping;
21
import org.springframework.web.bind.annotation.RequestParam;
22
23
/**
24
 * @author VeronikaRevjakina
25
 */
26
@RestController("SubjectsControllerV1")
27
@SuppressWarnings("checkstyle:MultipleStringLiterals")
28
@RequestMapping("/v1/subjects")
29
public class SubjectsController extends AbstractController {
30
31
    @Autowired
32
    protected SubjectsServiceInterface subjectsService;
33
34
    @Autowired
35
    protected SubjectsOptionsBuilderInterface subjectsOptionsBuilder;
36
37
    @Autowired
38
    protected UsersServiceInterface usersService;
39
40
    @Autowired
41
    @Qualifier("SubjectsMapperV1")
42
    private SubjectsMapper subjectsMapper;
43
44
    @GetMapping("")
45
    public List<Subject> list(@RequestParam(name = "userId", required = true) Integer userId)
46
        throws NotFoundException, ForbiddenException {
47
        final UserModelInterface userModel = this.usersService.find(userId);
48
49
        if (userModel == null) {
50
            throw new NotFoundException();
51
        }
52
        if (!this.acl.hasAccess(userModel)) {
53
            throw new ForbiddenException();
54
        }
55
56
        final List<SubjectModelInterface> subjectsModels = this.subjectsService.findByUser(userModel);
57
58
        return subjectsModels
59
                .stream()
60
                .map(model -> this.subjectsMapper.map(model, Subject.class))
61
                .collect(Collectors.toList());
62
    }
63
    /**
64
     * create
65
     */
66
    /**
67
     * update
68
     */
69
    /**
70
     * show(subjectId)
71
     */
72
    @GetMapping("/{subjectId}")
73
    public Subject show(@PathVariable Integer subjectId) throws NotFoundException, ForbiddenException {
74
        final SubjectModelInterface subjectModel = this.getSubjectModel(
75
                subjectId,
76
                (new SubjectsOptions()).withTopics(new TopicsOptions())
77
        );
78
79
        if (!this.acl.hasAccess(subjectModel)) {
80
            throw new ForbiddenException();
81
        }
82
        return this.subjectsMapper.map(subjectModel, Subject.class);
83
    }
84
85
    private SubjectModelInterface getSubjectModel(Integer id, SubjectsOptionsInterface subjectOptions) throws NotFoundException {
86
        final SubjectModelInterface subjectModel = this.subjectsService.find(id, subjectOptions);
87
        if (subjectModel == null) {
88
            throw new NotFoundException();
89
        }
90
        return subjectModel;
91
    }
92
93
    private SubjectModelInterface getSubjectModel(Integer id) throws NotFoundException {
0 ignored issues
show
Unused Code introduced by
Remove this unused private "getSubjectModel" method.
Loading history...
94
        return this.getSubjectModel(id, this.subjectsOptionsBuilder.forAuth());
95
    }
96
    /**
97
     * delete(subjectId)
98
     */
99
}
100