Completed
Push — dev ( eb1dae...b8d61e )
by Konstantin
12s
created

update(Subject)   A

Complexity

Conditions 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 11
rs 9.85
cc 2
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 UnidentifiedModelException();
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
    @DeleteMapping("/{subjectId}")
109
    public void delete(@PathVariable Integer subjectId) throws NotFoundException, ForbiddenException {
110
        final SubjectModelInterface subjectModelforAuth = this.getSubjectModel(subjectId);
111
        if (!this.acl.hasAccess(subjectModelforAuth)) {
112
            throw new ForbiddenException();
113
        }
114
115
        final SubjectsOptionsInterface subjectsOptionsDelete = this.subjectsOptionsBuilder.forDelete();
116
        final SubjectModelInterface subjectModelforDelete
117
                = this.getSubjectModel(subjectId, subjectsOptionsDelete);
118
        this.subjectsService.delete(subjectModelforDelete, subjectsOptionsDelete);
119
    }
120
121
    private void checkUser(Subject subject) throws BadRequestException {
122
        final UserModelInterface userModel = this.usersService.find(
123
                subject.getUser().getId(),
124
                this.usersOptionsBuilder.forAuth()
125
        );
126
127
        if (!this.acl.hasAccess(userModel)) {
128
            throw new BadRequestException();
129
        }
130
131
    }
132
133
    private SubjectModelInterface getSubjectModel(Integer id, SubjectsOptionsInterface subjectOptions)
134
            throws NotFoundException {
135
        final SubjectModelInterface subjectModel = this.subjectsService.find(id, subjectOptions);
136
        if (subjectModel == null) {
137
            throw new NotFoundException();
138
        }
139
        return subjectModel;
140
    }
141
142
    private SubjectModelInterface getSubjectModel(Integer id) throws NotFoundException {
143
        return this.getSubjectModel(id, this.subjectsOptionsBuilder.forAuth());
144
    }
145
146
}
147