Completed
Pull Request — dev (#389)
by
unknown
09:19 queued 05:29
created

getUserModel(Integer)   A

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 2
rs 10
cc 1
1
package easytests.api.v1.controllers;
2
3
import easytests.api.v1.exceptions.*;
4
import easytests.api.v1.mappers.UsersMapper;
5
import easytests.api.v1.models.User;
6
import easytests.auth.services.SessionServiceInterface;
7
import easytests.core.models.UserModelInterface;
8
import easytests.core.options.UsersOptionsInterface;
9
import easytests.core.options.builder.UsersOptionsBuilderInterface;
10
import easytests.core.services.UsersServiceInterface;
11
import java.util.List;
12
import java.util.stream.Collectors;
13
import org.springframework.beans.factory.annotation.Autowired;
14
import org.springframework.beans.factory.annotation.Qualifier;
15
import org.springframework.web.bind.annotation.*;
16
17
18
/**
19
 * @author SvetlanaTselikova
20
 */
21
@RestController("UsersControllerV1")
22
@SuppressWarnings("checkstyle:MultipleStringLiterals")
23
@RequestMapping("/v1/users")
24
public class UsersController {
25
26
    @Autowired
27
    protected UsersServiceInterface usersService;
28
29
    @Autowired
30
    private UsersOptionsBuilderInterface usersOptionsBuilder;
31
32
    @Autowired
33
    private SessionServiceInterface sessionService;
34
35
    @Autowired
36
    @Qualifier("UsersMapperV1")
37
    private UsersMapper usersMapper;
38
39
    private Boolean isAdmin() {
40
        return this.sessionService.getUserModel().getIsAdmin();
41
    }
42
43
    @GetMapping("")
44
    public List<User> list() throws ForbiddenException {
45
        if (!this.isAdmin()) {
46
            throw new ForbiddenException();
47
        }
48
        final List<UserModelInterface> usersModels = this.usersService.findAll();
49
50
        return usersModels
51
                .stream()
52
                .map(model -> this.usersMapper.map(model, User.class))
53
                .collect(Collectors.toList());
54
    }
55
56
    /**
57
     * create
58
     */
59
    /**
60
     * update
61
     */
62
    /**
63
     * show(userId)
64
     */
65
    @DeleteMapping("/{userId}")
66
    public void delete(@PathVariable Integer userId) throws NotFoundException, ForbiddenException {
67
        final UserModelInterface userModel = this.getUserModel(userId);
68
69
        if (!this.isAdmin()) {
70
            throw new ForbiddenException();
71
        }
72
73
        if (userModel == null) {
0 ignored issues
show
Bug introduced by
This condition always evaluates to false. Consider refactoring your code to no longer check for it or rewrite the condition.
Loading history...
74
            throw new NotFoundException();
75
        }
76
77
        final UserModelInterface userModelForDelete = this.getUserModel(userId, this.usersOptionsBuilder.forDelete());
78
        this.usersService.delete(userModelForDelete);
79
    }
80
81
    private UserModelInterface getUserModel(Integer id, UsersOptionsInterface userOptions) throws NotFoundException {
82
        final UserModelInterface userModel = this.usersService.find(id, userOptions);
83
        if (userModel == null) {
84
            throw new NotFoundException();
85
        }
86
        return userModel;
87
    }
88
89
    private UserModelInterface getUserModel(Integer id) throws NotFoundException {
90
        return this.getUserModel(id, this.usersOptionsBuilder.forAuth());
91
    }
92
    /**
93
     * showMe
94
     */
95
}
96