Passed
Pull Request — dev (#379)
by Konstantin
04:21
created

checkUser(Subject)   A

Complexity

Conditions 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 7
rs 10
cc 2
1
package easytests.api.v1.controllers;
2
3
import easytests.api.v1.exceptions.*;
4
import easytests.api.v1.exceptions.ForbiddenException;
5
import easytests.api.v1.exceptions.NotFoundException;
6
import easytests.api.v1.mappers.SubjectsMapper;
7
import easytests.api.v1.models.Identity;
8
import easytests.api.v1.models.Subject;
9
import easytests.core.models.SubjectModel;
10
import easytests.core.models.SubjectModelInterface;
11
import easytests.core.models.UserModelInterface;
12
import easytests.core.options.SubjectsOptionsInterface;
13
import easytests.core.options.builder.SubjectsOptionsBuilderInterface;
14
import easytests.core.options.builder.UsersOptionsBuilderInterface;
15
import easytests.core.services.SubjectsServiceInterface;
16
import easytests.core.services.UsersServiceInterface;
17
import java.util.List;
18
import java.util.stream.Collectors;
19
import org.springframework.beans.factory.annotation.Autowired;
20
import org.springframework.beans.factory.annotation.Qualifier;
21
import org.springframework.http.HttpStatus;
22
import org.springframework.web.bind.annotation.*;
23
import org.springframework.web.bind.annotation.GetMapping;
24
import org.springframework.web.bind.annotation.RequestParam;
25
26
/**
27
 * @author VeronikaRevjakina
28
 */
29
@RestController("SubjectsControllerV1")
30
@SuppressWarnings("checkstyle:MultipleStringLiterals")
31
@RequestMapping("/v1/subjects")
32
public class SubjectsController extends AbstractController {
33
34
    @Autowired
35
    protected SubjectsServiceInterface subjectsService;
36
37
    @Autowired
38
    protected SubjectsOptionsBuilderInterface subjectsOptionsBuilder;
39
40
    @Autowired
41
    protected UsersOptionsBuilderInterface usersOptionsBuilder;
42
43
    @Autowired
44
    protected UsersServiceInterface usersService;
45
46
    @Autowired
47
    @Qualifier("SubjectsMapperV1")
48
    private SubjectsMapper subjectsMapper;
49
50
    @GetMapping("")
51
    public List<Subject> list(@RequestParam(name = "userId", required = true) Integer userId)
52
        throws NotFoundException, ForbiddenException {
53
        final UserModelInterface userModel = this.usersService.find(userId);
54
55
        if (userModel == null) {
56
            throw new NotFoundException();
57
        }
58
        if (!this.acl.hasAccess(userModel)) {
59
            throw new ForbiddenException();
60
        }
61
62
        final List<SubjectModelInterface> subjectsModels = this.subjectsService.findByUser(userModel);
63
64
        return subjectsModels
65
                .stream()
66
                .map(model -> this.subjectsMapper.map(model, Subject.class))
67
                .collect(Collectors.toList());
68
    }
69
70
    @PostMapping("")
71
    @ResponseStatus(HttpStatus.CREATED)
72
    public Identity create(@RequestBody Subject subject) throws BadRequestException, ForbiddenException {
73
        if (subject.getId() != null) {
74
            throw new IdentifiedModelException();
75
        }
76
77
        this.checkUser(subject);
78
79
        final SubjectModelInterface subjectModel = this.subjectsMapper.map(subject, SubjectModel.class);
80
        subjectModel.setUser(usersService.find(subject.getUser().getId()));
81
82
        this.subjectsService.save(subjectModel);
83
84
        return this.subjectsMapper.map(subjectModel, Identity.class);
85
86
    }
87
88
    private void checkUser(Subject subject) throws BadRequestException, ForbiddenException {
89
        final UserModelInterface userModel = this.usersService.find(
90
            subject.getUser().getId(),
91
            this.usersOptionsBuilder.forAuth()
92
        );
93
94
        if (!this.acl.hasAccess(userModel)) {
95
            throw new BadRequestException();
96
        }
97
98
    }
99
100
    /**
101
     * update
102
     */
103
    /**
104
     * show(subjectId)
105
     */
106
107
    @GetMapping("/{subjectId}")
108
    public Subject show(@PathVariable Integer subjectId) throws NotFoundException, ForbiddenException {
109
        final SubjectModelInterface subjectModel = this.getSubjectModel(subjectId);
110
111
        if (!this.acl.hasAccess(subjectModel)) {
112
            throw new ForbiddenException();
113
        }
114
        return this.subjectsMapper.map(subjectModel, Subject.class);
115
    }
116
117
    private SubjectModelInterface getSubjectModel(Integer id, SubjectsOptionsInterface subjectOptions)
118
            throws NotFoundException {
119
        final SubjectModelInterface subjectModel = this.subjectsService.find(id, subjectOptions);
120
        if (subjectModel == null) {
121
            throw new NotFoundException();
122
        }
123
        return subjectModel;
124
    }
125
126
    private SubjectModelInterface getSubjectModel(Integer id) throws NotFoundException {
127
        return this.getSubjectModel(id, this.subjectsOptionsBuilder.forAuth());
128
    }
129
    /**
130
     * delete(subjectId)
131
     */
132
}
133