Completed
Pull Request — dev (#388)
by
unknown
05:18
created

easytests.api.v1.controllers.SubjectsController   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Importance

Changes 7
Bugs 1 Features 0
Metric Value
wmc 11
c 7
b 1
f 0
dl 0
loc 83
rs 10

5 Methods

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