Passed
Pull Request — dev (#390)
by
unknown
05:08
created

easytests.api.v1.controllers.SubjectsController   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Importance

Changes 5
Bugs 2 Features 0
Metric Value
wmc 14
c 5
b 2
f 0
dl 0
loc 107
rs 10

8 Methods

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