Completed
Pull Request — dev (#384)
by
unknown
05:14
created

easytests.api.v1.controllers.SubjectsController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A list(Integer) 0 18 3
A delete(Integer) 0 10 3
A getSubjectModel(Integer,SubjectsOptionsInterface) 0 3 1
A getSubjectModel(Integer) 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.SubjectsOptionsInterface;
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.*;
18
import org.springframework.web.bind.annotation.GetMapping;
19
import org.springframework.web.bind.annotation.RequestParam;
20
21
/**
22
 * @author VeronikaRevjakina
23
 */
24
@RestController("SubjectsControllerV1")
25
@SuppressWarnings("checkstyle:MultipleStringLiterals")
26
@RequestMapping("/v1/subjects")
27
public class SubjectsController extends AbstractController {
28
29
    @Autowired
30
    protected SubjectsServiceInterface subjectsService;
31
32
    @Autowired
33
    protected SubjectsOptionsBuilderInterface subjectsOptionsBuilder;
34
35
    @Autowired
36
    protected UsersServiceInterface usersService;
37
38
    @Autowired
39
    @Qualifier("SubjectsMapperV1")
40
    private SubjectsMapper subjectsMapper;
41
42
    @GetMapping("")
43
    public List<Subject> list(@RequestParam(name = "userId", required = true) Integer userId)
44
        throws NotFoundException, ForbiddenException {
45
        final UserModelInterface userModel = this.usersService.find(userId);
46
47
        if (userModel == null) {
48
            throw new NotFoundException();
49
        }
50
        if (!this.acl.hasAccess(userModel)) {
51
            throw new ForbiddenException();
52
        }
53
54
        final List<SubjectModelInterface> subjectsModels = this.subjectsService.findByUser(userModel);
55
56
        return subjectsModels
57
                .stream()
58
                .map(model -> this.subjectsMapper.map(model, Subject.class))
59
                .collect(Collectors.toList());
60
    }
61
    /**
62
     * create
63
     */
64
    /**
65
     * update
66
     */
67
    /**
68
     * show(subjectId)
69
     */
70
71
    @DeleteMapping("/{subjectId}")
72
    public void delete(@PathVariable Integer subjectId) throws NotFoundException, ForbiddenException {
73
        final SubjectModelInterface subjectModel = this.getSubjectModel(subjectId);
74
        if (subjectModel == null) {
75
            throw new NotFoundException();
76
        }
77
        if (!this.acl.hasAccess(subjectModel)) {
78
            throw new ForbiddenException();
79
        }
80
        this.subjectsService.delete(subjectModel);
81
    }
82
83
    private SubjectModelInterface getSubjectModel(Integer id, SubjectsOptionsInterface subjectOption) {
84
        final SubjectModelInterface subjectModel = this.subjectsService.find(id, subjectOption);
85
        return subjectModel;
86
    }
87
88
    private SubjectModelInterface getSubjectModel(Integer id) throws NotFoundException {
89
        final SubjectsOptionsInterface subjectsOptionsInterface = this.subjectsOptionsBuilder.forDelete();
90
        return this.getSubjectModel(id, subjectsOptionsInterface);
91
    }
92
}
93