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

easytests.api.v1.controllers.SubjectsController   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Importance

Changes 8
Bugs 1 Features 0
Metric Value
wmc 9
c 8
b 1
f 0
dl 0
loc 80
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A list(Integer) 0 18 3
A show(Integer) 0 8 2
A getSubjectModel(Integer,SubjectsOptionsInterface) 0 7 2
A delete(Integer) 0 10 2
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, subjectsOptionsBuilder.forAuth());
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
    /**
91
     * delete(subjectId)
92
     */
93
94
    @DeleteMapping("/{subjectId}")
95
    public void delete(@PathVariable Integer subjectId) throws NotFoundException, ForbiddenException {
96
        final SubjectModelInterface subjectModelforAuth
97
                = this.getSubjectModel(subjectId, subjectsOptionsBuilder.forAuth());
98
        if (!this.acl.hasAccess(subjectModelforAuth)) {
99
            throw new ForbiddenException();
100
        }
101
        final SubjectModelInterface subjectModelforDelete
102
                = this.getSubjectModel(subjectId, subjectsOptionsBuilder.forDelete());
103
        this.subjectsService.delete(subjectModelforDelete, subjectsOptionsBuilder.forDelete());
104
    }
105
}
106