Completed
Pull Request — dev (#379)
by
unknown
04:41
created

checkUser

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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