Passed
Pull Request — dev (#384)
by
unknown
05:13
created

easytests.api.v1.controllers.SubjectsController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A list(Integer) 0 18 3
A getSubjectModel(Integer) 0 3 1
A delete(Integer) 0 10 3
A getSubjectModel(Integer,SubjectsOptionsInterface) 0 3 1
1
package easytests.api.v1.controllers;
2
3
import easytests.api.v1.exceptions.ForbiddenException;
4
import easytests.api.v1.exceptions.NotFoundException;
5
import easytests.api.v1.mappers.SubjectsMapper;
6
import easytests.api.v1.models.Subject;
7
import easytests.core.models.SubjectModelInterface;
8
import easytests.core.models.UserModelInterface;
9
import easytests.core.options.SubjectsOptions;
10
import easytests.core.options.SubjectsOptionsInterface;
11
import easytests.core.options.builder.SubjectsOptionsBuilderInterface;
12
import easytests.core.services.SubjectsServiceInterface;
13
import easytests.core.services.UsersServiceInterface;
14
import java.util.List;
15
import java.util.stream.Collectors;
16
import org.springframework.beans.factory.annotation.Autowired;
17
import org.springframework.beans.factory.annotation.Qualifier;
18
import org.springframework.web.bind.annotation.*;
19
import org.springframework.web.bind.annotation.GetMapping;
20
import org.springframework.web.bind.annotation.RequestParam;
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
    protected SubjectsOptionsBuilderInterface subjectsOptionsBuilder;
35
36
    @Autowired
37
    protected UsersServiceInterface usersService;
38
39
    @Autowired
40
    @Qualifier("SubjectsMapperV1")
41
    private SubjectsMapper subjectsMapper;
42
43
    @GetMapping("")
44
    public List<Subject> list(@RequestParam(name = "userId", required = true) Integer userId)
45
        throws NotFoundException, ForbiddenException {
46
        final UserModelInterface userModel = this.usersService.find(userId);
47
48
        if (userModel == null) {
49
            throw new NotFoundException();
50
        }
51
        if (!this.acl.hasAccess(userModel)) {
52
            throw new ForbiddenException();
53
        }
54
55
        final List<SubjectModelInterface> subjectsModels = this.subjectsService.findByUser(userModel);
56
57
        return subjectsModels
58
                .stream()
59
                .map(model -> this.subjectsMapper.map(model, Subject.class))
60
                .collect(Collectors.toList());
61
    }
62
    /**
63
     * create
64
     */
65
    /**
66
     * update
67
     */
68
    /**
69
     * show(subjectId)
70
     */
71
72
    @DeleteMapping("/{subjectId}")
73
    public void delete(@PathVariable Integer subjectId) throws NotFoundException, ForbiddenException {
74
        final SubjectModelInterface subjectModel = this.getSubjectModel(subjectId);
75
        if (subjectModel == null) {
76
            throw new NotFoundException();
77
        }
78
        if (!this.acl.hasAccess(subjectModel)) {
79
            throw new ForbiddenException();
80
        }
81
        this.subjectsService.delete(new SubjectsOptions().withRelations(subjectModel));
82
    }
83
84
    private SubjectModelInterface getSubjectModel(Integer id, SubjectsOptionsInterface subjectOpt) {
85
        final SubjectModelInterface subjectModel = this.subjectsService.find(id, subjectOpt);
86
        return subjectModel;
87
    }
88
89
    private SubjectModelInterface getSubjectModel(Integer id) throws NotFoundException {
90
        final SubjectsOptionsInterface subjectsOptionsInterface = this.subjectsOptionsBuilder.forDelete();
91
        return this.getSubjectModel(id, subjectsOptionsInterface);
92
    }
93
}
94