Completed
Pull Request — dev (#348)
by
unknown
05:40
created

list(Integer)   A

Complexity

Conditions 3

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 19
rs 9.45
cc 3
1
package easytests.api.v1.controllers;
2
3
import easytests.api.v1.exceptions.ForbiddenException;
4
import easytests.api.v1.mappers.SubjectsMapper;
5
import easytests.api.v1.models.Subject;
6
import easytests.common.exceptions.NotFoundException;
7
import easytests.core.models.SubjectModelInterface;
8
import easytests.core.models.UserModelInterface;
9
import easytests.core.models.empty.UserModelEmpty;
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.GetMapping;
18
import org.springframework.web.bind.annotation.RequestParam;
19
import org.springframework.web.bind.annotation.*;
20
21
22
/**
23
 * @author VeronikaRevjakina
24
 */
25
@RestController("SubjectsControllerV1")
26
@SuppressWarnings("checkstyle:MultipleStringLiterals")
27
@RequestMapping("/v1/subjects")
28
public class SubjectsController extends AbstractController{
29
30
    @Autowired
31
    protected SubjectsServiceInterface subjectsService;
32
33
    @Autowired
34
    private SubjectsOptionsBuilderInterface subjectsOptionsBuilder;
35
36
    @Autowired
37
    protected UsersServiceInterface usersService;
38
39
    @Autowired
40
    @Qualifier("SubjectsMapperV1")
41
    private SubjectsMapper subjectsMapper;
42
43
44
    @GetMapping("")
45
    public List<Subject> list(@RequestParam(name = "userId", required = true) Integer userId)
46
         throws NotFoundException, ForbiddenException {
47
            final UserModelInterface userModel = this.usersService.find(userId);
48
49
            if (userModel == null) {
50
                throw new NotFoundException();
51
            }
52
53
            if (!this.acl.hasAccess(userModel)) {
54
                throw new ForbiddenException();
55
            }
56
57
        final List<SubjectModelInterface> subjectsModels = this.subjectsService.findByUser(new UserModelEmpty(userId));
58
59
        return subjectsModels
60
                .stream()
61
                .map(model -> this.subjectsMapper.map(model, Subject.class))
62
                .collect(Collectors.toList());
63
    }
64
65
    /**
66
     * create
67
     */
68
    /**
69
     * update
70
     */
71
    /**
72
     * show(subjectId)
73
     */
74
    /**
75
     * delete(subjectId)
76
     */
77
}
78