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

checkUser

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
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
    @GetMapping("")
48
    public List<Subject> list(@RequestParam(name = "userId", required = true) Integer userId)
49
            throws NotFoundException, ForbiddenException {
50
        final UserModelInterface userModel = this.usersService.find(userId);
51
52
        if (userModel == null) {
53
            throw new NotFoundException();
54
        }
55
        if (!this.acl.hasAccess(userModel)) {
56
            throw new ForbiddenException();
57
        }
58
59
        final List<SubjectModelInterface> subjectsModels = this.subjectsService.findByUser(userModel);
60
61
        return subjectsModels
62
                .stream()
63
                .map(model -> this.subjectsMapper.map(model, Subject.class))
64
                .collect(Collectors.toList());
65
    }
66
    /**
67
     * create
68
     */
69
70
    @PutMapping("")
71
    public void update(@RequestBody Subject subject) throws UnidentifiedModelException, NotFoundException, ForbiddenException {
72
73
        if (subject.getId() == null) {
74
            throw new UnidentifiedModelException();
75
        }
76
        final SubjectModelInterface subjectModel = this.getSubjectModel(subject.getId());
77
78
        this.checkUser(subject);
79
80
        this.subjectsMapper.map(subject, subjectModel);
81
82
        this.subjectsService.save(subjectModel);
83
    }
84
85
    @GetMapping("/{subjectId}")
86
    public Subject show(@PathVariable Integer subjectId) throws NotFoundException, ForbiddenException {
87
        final SubjectModelInterface subjectModel = this.getSubjectModel(subjectId);
88
89
        if (!this.acl.hasAccess(subjectModel)) {
90
            throw new ForbiddenException();
91
        }
92
        return this.subjectsMapper.map(subjectModel, Subject.class);
93
    }
94
95
    /**
96
     * delete(subjectId)
97
     */
98
99
    private void checkUser(Subject subject) throws ForbiddenException {
100
        final UserModelInterface userModel = this.usersService.find(
101
                subject.getUser().getId(),
102
                this.usersOptionsBuilder.forAuth()
103
        );
104
105
        if (!this.acl.hasAccess(userModel)) {
106
            throw new ForbiddenException();
107
        }
108
109
    }
110
111
    private SubjectModelInterface getSubjectModel(Integer id, SubjectsOptionsInterface subjectOptions)
112
            throws NotFoundException {
113
        final SubjectModelInterface subjectModel = this.subjectsService.find(id, subjectOptions);
114
        if (subjectModel == null) {
115
            throw new NotFoundException();
116
        }
117
        return subjectModel;
118
    }
119
120
    private SubjectModelInterface getSubjectModel(Integer id) throws NotFoundException {
121
        return this.getSubjectModel(id, this.subjectsOptionsBuilder.forAuth());
122
    }
123
}
124