Passed
Pull Request — dev (#388)
by
unknown
06:38
created

getUserModel(Integer)   A

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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