Completed
Push — dev ( 723ba1...f622ff )
by Konstantin
11s
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
81
        this.subjectsService.save(subjectModel);
82
83
        return this.subjectsMapper.map(subjectModel, Identity.class);
84
85
    }
86
87
    private void checkUser(Subject subject) throws BadRequestException {
88
        final UserModelInterface userModel = this.usersService.find(
89
            subject.getUser().getId(),
90
            this.usersOptionsBuilder.forAuth()
91
        );
92
93
        if (!this.acl.hasAccess(userModel)) {
94
            throw new BadRequestException();
95
        }
96
97
    }
98
99
    /**
100
     * update
101
     */
102
    /**
103
     * show(subjectId)
104
     */
105
106
    @GetMapping("/{subjectId}")
107
    public Subject show(@PathVariable Integer subjectId) throws NotFoundException, ForbiddenException {
108
        final SubjectModelInterface subjectModel = this.getSubjectModel(subjectId);
109
110
        if (!this.acl.hasAccess(subjectModel)) {
111
            throw new ForbiddenException();
112
        }
113
        return this.subjectsMapper.map(subjectModel, Subject.class);
114
    }
115
116
    private SubjectModelInterface getSubjectModel(Integer id, SubjectsOptionsInterface subjectOptions)
117
            throws NotFoundException {
118
        final SubjectModelInterface subjectModel = this.subjectsService.find(id, subjectOptions);
119
        if (subjectModel == null) {
120
            throw new NotFoundException();
121
        }
122
        return subjectModel;
123
    }
124
125
    private SubjectModelInterface getSubjectModel(Integer id) throws NotFoundException {
126
        return this.getSubjectModel(id, this.subjectsOptionsBuilder.forAuth());
127
    }
128
    /**
129
     * delete(subjectId)
130
     */
131
}
132