Completed
Pull Request — dev (#379)
by
unknown
03:49
created

easytests.api.v1.controllers.SubjectsController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A list(Integer) 0 18 3
B create(Subject) 0 22 5
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.getUser() != null) {
69
            final UserModelInterface userModel = this.usersService.find(subject.getUser().getId());
70
71
72
            if (subject.getId() != null) {
73
                throw new IdentifiedModelException();
74
            }
75
            if (subject.getUser() == null) {
76
                throw new BadRequestException("user must exist");
77
            }
78
            if (!this.acl.hasAccess(userModel)) {
79
                throw new ForbiddenException();
80
            }
81
82
            final SubjectModelInterface subjectModel = this.subjectsMapper.map(subject, SubjectModel.class);
83
84
            this.subjectsService.save(subjectModel);
85
86
            return this.subjectsMapper.map(subjectModel, Identity.class);
87
        } else throw new BadRequestException();
88
    }
89
    /**
90
     * update
91
     */
92
    /**
93
     * show(subjectId)
94
     */
95
    /**
96
     * delete(subjectId)
97
     */
98
}
99