Passed
Pull Request — dev (#390)
by
unknown
04:15
created

checkUser(Subject)   A

Complexity

Conditions 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 10
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.exceptions.UnidentifiedModelException;
6
import easytests.api.v1.mappers.SubjectsMapper;
7
import easytests.api.v1.models.Subject;
8
import easytests.core.models.SubjectModelInterface;
9
import easytests.core.models.UserModelInterface;
10
import easytests.core.options.SubjectsOptionsInterface;
11
import easytests.core.options.builder.SubjectsOptionsBuilderInterface;
12
import easytests.core.options.builder.UsersOptionsBuilderInterface;
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:linelength", "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 UsersOptionsBuilderInterface usersOptionsBuilder;
39
40
    @Autowired
41
    protected UsersServiceInterface usersService;
42
43
    @Autowired
44
    @Qualifier("SubjectsMapperV1")
45
    private SubjectsMapper subjectsMapper;
46
47
48
    //private SubjectsSupport subjectSupport = new TopicsSupport();
49
50
51
    @GetMapping("")
52
    public List<Subject> list(@RequestParam(name = "userId", required = true) Integer userId)
53
            throws NotFoundException, ForbiddenException {
54
        final UserModelInterface userModel = this.usersService.find(userId);
55
56
        if (userModel == null) {
57
            throw new NotFoundException();
58
        }
59
        if (!this.acl.hasAccess(userModel)) {
60
            throw new ForbiddenException();
61
        }
62
63
        final List<SubjectModelInterface> subjectsModels = this.subjectsService.findByUser(userModel);
64
65
        return subjectsModels
66
                .stream()
67
                .map(model -> this.subjectsMapper.map(model, Subject.class))
68
                .collect(Collectors.toList());
69
    }
70
    /**
71
     * create
72
     */
73
74
    @PutMapping("")
75
    public void update(@RequestBody Subject subject) throws UnidentifiedModelException, NotFoundException, ForbiddenException {
76
77
        if (subject.getId() == null) {
78
            throw new UnidentifiedModelException();
79
        }
80
        final SubjectModelInterface subjectModel = this.getSubjectModel(subject.getId());
81
82
        this.checkUser(subject);
83
84
        this.subjectsMapper.map(subject, subjectModel);
85
86
        this.subjectsService.save(subjectModel);
87
    }
88
89
    @GetMapping("/{subjectId}")
90
    public Subject show(@PathVariable Integer subjectId) throws NotFoundException, ForbiddenException {
91
        final SubjectModelInterface subjectModel = this.getSubjectModel(subjectId);
92
93
        if (!this.acl.hasAccess(subjectModel)) {
94
            throw new ForbiddenException();
95
        }
96
        return this.subjectsMapper.map(subjectModel, Subject.class);
97
    }
98
99
    /**
100
     * delete(subjectId)
101
     */
102
103
    private void checkUser(Subject subject) throws ForbiddenException {
104
        final UserModelInterface userModel = this.usersService.find(
105
                subject.getUser().getId(),
106
                this.usersOptionsBuilder.forAuth()
107
        );
108
109
        if (!this.acl.hasAccess(userModel)) {
110
            throw new ForbiddenException();
111
        }
112
113
    }
114
115
    private SubjectModelInterface getSubjectModel(Integer id, SubjectsOptionsInterface subjectOptions)
116
            throws NotFoundException {
117
        final SubjectModelInterface subjectModel = this.subjectsService.find(id, subjectOptions);
118
        if (subjectModel == null) {
119
            throw new NotFoundException();
120
        }
121
        return subjectModel;
122
    }
123
124
    private SubjectModelInterface getSubjectModel(Integer id) throws NotFoundException {
125
        return this.getSubjectModel(id, this.subjectsOptionsBuilder.forAuth());
126
    }
127
}
128