Completed
Pull Request — dev (#379)
by
unknown
04:09
created

list(Integer)   A

Complexity

Conditions 3

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
c 4
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.*;
4
import easytests.api.v1.exceptions.ForbiddenException;
5
import easytests.api.v1.exceptions.NotFoundException;
6
import easytests.api.v1.mappers.SubjectsMapper;
7
import easytests.api.v1.models.Identity;
8
import easytests.api.v1.models.Subject;
9
import easytests.core.models.SubjectModel;
10
import easytests.core.models.SubjectModelInterface;
11
import easytests.core.models.UserModelInterface;
12
import easytests.core.options.builder.SubjectsOptionsBuilderInterface;
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 UsersServiceInterface usersService;
40
41
    @Autowired
42
    @Qualifier("SubjectsMapperV1")
43
    private SubjectsMapper subjectsMapper;
44
45
    @GetMapping("")
46
    public List<Subject> list(@RequestParam(name = "userId", required = true) Integer userId)
47
        throws NotFoundException, ForbiddenException {
48
        final UserModelInterface userModel = this.usersService.find(userId);
49
50
        if (userModel == null) {
51
            throw new NotFoundException();
52
        }
53
        if (!this.acl.hasAccess(userModel)) {
54
            throw new ForbiddenException();
55
        }
56
57
        final List<SubjectModelInterface> subjectsModels = this.subjectsService.findByUser(userModel);
58
59
        return subjectsModels
60
                .stream()
61
                .map(model -> this.subjectsMapper.map(model, Subject.class))
62
                .collect(Collectors.toList());
63
    }
64
65
    @PostMapping("")
66
    @ResponseStatus(HttpStatus.CREATED)
67
    public Identity create(@RequestBody Subject subject) throws BadRequestException, ForbiddenException {
68
        if (subject.getId() != null) {
69
            throw new IdentifiedModelException();
70
        }
71
        if (subject.getUser() != null) {
72
            final UserModelInterface userModel = this.usersService.find(subject.getUser().getId());
73
74
            if (!this.acl.hasAccess(userModel)) {
75
                throw new ForbiddenException();
76
            }
77
        } else throw new BadRequestException("user must exist");
78
79
            final SubjectModelInterface subjectModel = this.subjectsMapper.map(subject, SubjectModel.class);
80
81
            this.subjectsService.save(subjectModel);
82
83
            return this.subjectsMapper.map(subjectModel, Identity.class);
84
85
    }
86
    /**
87
     * update
88
     */
89
    /**
90
     * show(subjectId)
91
     */
92
    /**
93
     * delete(subjectId)
94
     */
95
}
96