Completed
Pull Request — dev (#388)
by
unknown
08:03
created

easytests.api.v1.controllers.UsersController   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Importance

Changes 3
Bugs 2 Features 0
Metric Value
wmc 14
c 3
b 2
f 0
dl 0
loc 103
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
isAdmin 0 2 ?
A isAdmin() 0 2 1
A list() 0 11 2
showme 0 8 ?
A showme() 0 8 2
A getUserModel(Integer,UsersOptionsInterface) 0 6 2
A getUserModel(Integer) 0 2 1
A show(Integer) 0 13 3
A update(User) 0 16 3
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.exceptions.UnidentifiedModelException;
6
import easytests.api.v1.mappers.UsersMapper;
7
import easytests.api.v1.models.User;
8
import easytests.auth.services.SessionServiceInterface;
9
import easytests.core.models.UserModelInterface;
10
import easytests.core.options.UsersOptionsInterface;
11
import easytests.core.options.builder.UsersOptionsBuilderInterface;
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
19
20
/**
21
 * @author SvetlanaTselikova
22
 */
23
@RestController("UsersControllerV1")
24
@SuppressWarnings("checkstyle:MultipleStringLiterals")
25
@RequestMapping("/v1/users")
26
public class UsersController {
27
28
    @Autowired
29
    protected UsersServiceInterface usersService;
30
31
    @Autowired
32
    private UsersOptionsBuilderInterface usersOptionsBuilder;
33
34
    @Autowired
35
    private SessionServiceInterface sessionService;
36
37
    @Autowired
38
    @Qualifier("UsersMapperV1")
39
    private UsersMapper usersMapper;
40
41
    private Boolean isAdmin() {
42
        return this.sessionService.getUserModel().getIsAdmin();
43
    }
44
45
    @GetMapping("")
46
    public List<User> list() throws ForbiddenException {
47
        if (!this.isAdmin()) {
48
            throw new ForbiddenException();
49
        }
50
        final List<UserModelInterface> usersModels = this.usersService.findAll();
51
52
        return usersModels
53
                .stream()
54
                .map(model -> this.usersMapper.map(model, User.class))
55
                .collect(Collectors.toList());
56
    }
57
58
    /**
59
     * create
60
     */
61
    /**
62
     * update
63
     */
64
    @GetMapping("/{userId}")
65
    public User show(@PathVariable Integer userId) throws NotFoundException, ForbiddenException {
66
        final UserModelInterface userModel = this.usersService.find(userId);
67
68
        if (!this.isAdmin()) {
69
            throw new ForbiddenException();
70
        }
71
72
        if (userModel == null) {
73
            throw new NotFoundException();
74
        }
75
76
        return this.usersMapper.map(userModel, User.class);
77
    }
78
79
    @PutMapping("")
80
    public void update(@RequestBody User user) throws Exception {
0 ignored issues
show
Best Practice introduced by
Dedicated exceptions should be preferred over throwing the generic Exception.
Loading history...
81
82
        if (!this.isAdmin()) {
83
            throw new ForbiddenException();
84
        }
85
86
        if (user.getId() == null) {
87
            throw new UnidentifiedModelException();
88
        }
89
90
        final UserModelInterface userModel = this.getUserModel(user.getId());
91
92
        this.usersMapper.map(user, userModel);
93
94
        this.usersService.save(userModel);
95
    }
96
    /**
97
     * show(userId)
98
     */
99
    /**
100
     * delete(userId)
101
     */
102
    /**
103
     * showMe
104
     */
105
106
    @GetMapping("/me")
107
    public User showme() throws ForbiddenException {
108
109
        if (!this.sessionService.isUser()) {
110
            throw new ForbiddenException();
111
        }
112
        final UserModelInterface userModel = this.sessionService.getUserModel();
113
        return this.usersMapper.map(userModel, User.class);
114
    }
115
116
    private UserModelInterface getUserModel(Integer id, UsersOptionsInterface userOptions) throws NotFoundException {
117
        final UserModelInterface userModel = this.usersService.find(id, userOptions);
118
        if (userModel == null) {
119
            throw new NotFoundException();
120
        }
121
        return userModel;
122
    }
123
124
    private UserModelInterface getUserModel(Integer id) throws NotFoundException {
125
        return this.getUserModel(id, this.usersOptionsBuilder.forAuth());
126
    }
127
128
}
129