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

list(Integer)   A

Complexity

Conditions 3

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
c 4
b 1
f 0
dl 0
loc 18
rs 9.5
cc 3
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
        final SubjectModelInterface subjectModel = this.getSubjectModel(subject.getId());
74
75
        this.checkUser(subject);
76
77
        this.subjectsMapper.map(subject, subjectModel);
78
79
        this.subjectsService.save(subjectModel);
80
    }
81
82
    @GetMapping("/{subjectId}")
83
    public Subject show(@PathVariable Integer subjectId) throws NotFoundException, ForbiddenException {
84
        final SubjectModelInterface subjectModel = this.getSubjectModel(subjectId);
85
86
        if (!this.acl.hasAccess(subjectModel)) {
87
            throw new ForbiddenException();
88
        }
89
        return this.subjectsMapper.map(subjectModel, Subject.class);
90
    }
91
92
    /**
93
     * delete(subjectId)
94
     */
95
96
    private void checkUser(Subject subject) throws ForbiddenException {
97
        final UserModelInterface userModel = this.usersService.find(
98
                subject.getUser().getId(),
99
                this.usersOptionsBuilder.forAuth()
100
        );
101
102
        if (!this.acl.hasAccess(userModel)) {
103
            throw new ForbiddenException();
104
        }
105
106
    }
107
108
    private SubjectModelInterface getSubjectModel(Integer id, SubjectsOptionsInterface subjectOptions)
109
            throws NotFoundException {
110
        final SubjectModelInterface subjectModel = this.subjectsService.find(id, subjectOptions);
111
        if (subjectModel == null) {
112
            throw new NotFoundException();
113
        }
114
        return subjectModel;
115
    }
116
117
    private SubjectModelInterface getSubjectModel(Integer id) throws NotFoundException {
118
        return this.getSubjectModel(id, this.subjectsOptionsBuilder.forAuth());
119
    }
120
}
121