Completed
Pull Request — dev (#384)
by
unknown
09:08 queued 03:09
created

list(Integer)   A

Complexity

Conditions 3

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
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.mappers.SubjectsMapper;
6
import easytests.api.v1.models.Subject;
7
import easytests.core.models.SubjectModelInterface;
8
import easytests.core.models.UserModelInterface;
9
import easytests.core.options.SubjectsOptionsInterface;
10
import easytests.core.options.builder.SubjectsOptionsBuilderInterface;
11
import easytests.core.services.SubjectsServiceInterface;
12
import easytests.core.services.UsersServiceInterface;
13
import java.util.List;
14
import java.util.stream.Collectors;
15
import org.springframework.beans.factory.annotation.Autowired;
16
import org.springframework.beans.factory.annotation.Qualifier;
17
import org.springframework.web.bind.annotation.*;
18
import org.springframework.web.bind.annotation.GetMapping;
19
import org.springframework.web.bind.annotation.RequestParam;
20
21
/**
22
 * @author VeronikaRevjakina
23
 */
24
@RestController("SubjectsControllerV1")
25
@SuppressWarnings("checkstyle:MultipleStringLiterals")
26
@RequestMapping("/v1/subjects")
27
public class SubjectsController extends AbstractController {
28
29
    @Autowired
30
    protected SubjectsServiceInterface subjectsService;
31
32
    @Autowired
33
    protected SubjectsOptionsBuilderInterface subjectsOptionsBuilder;
34
35
    @Autowired
36
    protected UsersServiceInterface usersService;
37
38
    @Autowired
39
    @Qualifier("SubjectsMapperV1")
40
    private SubjectsMapper subjectsMapper;
41
42
    @GetMapping("")
43
    public List<Subject> list(@RequestParam(name = "userId", required = true) Integer userId)
44
        throws NotFoundException, ForbiddenException {
45
        final UserModelInterface userModel = this.usersService.find(userId);
46
47
        if (userModel == null) {
48
            throw new NotFoundException();
49
        }
50
        if (!this.acl.hasAccess(userModel)) {
51
            throw new ForbiddenException();
52
        }
53
54
        final List<SubjectModelInterface> subjectsModels = this.subjectsService.findByUser(userModel);
55
56
        return subjectsModels
57
                .stream()
58
                .map(model -> this.subjectsMapper.map(model, Subject.class))
59
                .collect(Collectors.toList());
60
    }
61
    /**
62
     * create
63
     */
64
    /**
65
     * update
66
     */
67
    /**
68
     * show(subjectId)
69
     */
70
71
    @GetMapping("/{subjectId}")
72
    public Subject show(@PathVariable Integer subjectId) throws NotFoundException, ForbiddenException {
73
        final SubjectModelInterface subjectModel = this.getSubjectModel(subjectId);
74
75
        if (!this.acl.hasAccess(subjectModel)) {
76
            throw new ForbiddenException();
77
        }
78
        return this.subjectsMapper.map(subjectModel, Subject.class);
79
    }
80
81
    private SubjectModelInterface getSubjectModel(Integer id, SubjectsOptionsInterface subjectOptions)
82
            throws NotFoundException {
83
        final SubjectModelInterface subjectModel = this.subjectsService.find(id, subjectOptions);
84
        if (subjectModel == null) {
85
            throw new NotFoundException();
86
        }
87
        return subjectModel;
88
    }
89
90
    private SubjectModelInterface getSubjectModel(Integer id) throws NotFoundException {
91
        return this.getSubjectModel(id, this.subjectsOptionsBuilder.forAuth());
92
    }
93
    /**
94
     * delete(subjectId)
95
     */
96
97
    @DeleteMapping("/{subjectId}")
98
    public void delete(@PathVariable Integer subjectId) throws NotFoundException, ForbiddenException {
99
        final SubjectModelInterface subjectModel = this.getSubjectModel(subjectId);
100
        if (subjectModel == null) {
0 ignored issues
show
Bug introduced by
This condition always evaluates to false. Consider refactoring your code to no longer check for it or rewrite the condition.
Loading history...
101
            throw new NotFoundException();
102
        }
103
        if (!this.acl.hasAccess(subjectModel)) {
104
            throw new ForbiddenException();
105
        }
106
        this.subjectsService.delete(subjectModel);
107
    }
108
}
109