Completed
Pull Request — dev (#389)
by
unknown
12:16 queued 02:58
created

easytests.api.v1.controllers.UsersController   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Importance

Changes 4
Bugs 4 Features 0
Metric Value
wmc 14
c 4
b 4
f 0
dl 0
loc 92
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
isAdmin 0 2 ?
A isAdmin() 0 2 1
A list() 0 11 2
A show(Integer) 0 13 3
showme 0 8 ?
A showme() 0 8 2
A getUserModel(Integer,UsersOptionsInterface) 0 6 2
A getUserModel(Integer) 0 2 1
A delete(Integer) 0 14 3
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
    @GetMapping("/{userId}")
63
    public User show(@PathVariable Integer userId) throws NotFoundException, ForbiddenException {
64
        final UserModelInterface userModel = this.usersService.find(userId);
65
66
        if (!this.isAdmin()) {
67
            throw new ForbiddenException();
68
        }
69
70
        if (userModel == null) {
71
            throw new NotFoundException();
72
        }
73
74
        return this.usersMapper.map(userModel, User.class);
75
    }
76
77
    @DeleteMapping("/{userId}")
78
    public void delete(@PathVariable Integer userId) throws NotFoundException, ForbiddenException {
79
        if (!this.isAdmin()) {
80
            throw new ForbiddenException();
81
        }
82
83
        final UsersOptionsInterface usersOptions = this.usersOptionsBuilder.forDelete();
84
        final UserModelInterface userModel = this.usersService.find(userId, usersOptions);
85
86
        if (userModel == null) {
87
            throw new NotFoundException();
88
        }
89
90
        this.usersService.delete(userModel, usersOptions);
91
    }
92
93
    @GetMapping("/me")
94
    public User showme() throws ForbiddenException {
95
96
        if (!this.sessionService.isUser()) {
97
            throw new ForbiddenException();
98
        }
99
        final UserModelInterface userModel = this.sessionService.getUserModel();
100
        return this.usersMapper.map(userModel, User.class);
101
    }
102
103
    private UserModelInterface getUserModel(Integer id, UsersOptionsInterface userOptions) throws NotFoundException {
104
        final UserModelInterface userModel = this.usersService.find(id, userOptions);
105
        if (userModel == null) {
106
            throw new NotFoundException();
107
        }
108
        return userModel;
109
    }
110
111
    private UserModelInterface getUserModel(Integer id) throws NotFoundException {
0 ignored issues
show
Unused Code introduced by
Remove this unused private "getUserModel" method.
Loading history...
112
        return this.getUserModel(id, this.usersOptionsBuilder.forAuth());
113
    }
114
115
}
116