|
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.builder.UsersOptionsBuilderInterface; |
|
9
|
|
|
import easytests.core.services.UsersServiceInterface; |
|
10
|
|
|
import java.util.List; |
|
11
|
|
|
import java.util.stream.Collectors; |
|
12
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
13
|
|
|
import org.springframework.beans.factory.annotation.Qualifier; |
|
14
|
|
|
import org.springframework.web.bind.annotation.*; |
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @author SvetlanaTselikova |
|
19
|
|
|
*/ |
|
20
|
|
|
@RestController("UsersControllerV1") |
|
21
|
|
|
@SuppressWarnings("checkstyle:MultipleStringLiterals") |
|
22
|
|
|
@RequestMapping("/v1/users") |
|
23
|
|
|
public class UsersController { |
|
24
|
|
|
|
|
25
|
|
|
@Autowired |
|
26
|
|
|
protected UsersServiceInterface usersService; |
|
27
|
|
|
|
|
28
|
|
|
@Autowired |
|
29
|
|
|
private UsersOptionsBuilderInterface usersOptionsBuilder; |
|
30
|
|
|
|
|
31
|
|
|
@Autowired |
|
32
|
|
|
private SessionServiceInterface sessionService; |
|
33
|
|
|
|
|
34
|
|
|
@Autowired |
|
35
|
|
|
@Qualifier("UsersMapperV1") |
|
36
|
|
|
private UsersMapper usersMapper; |
|
37
|
|
|
|
|
38
|
|
|
private Boolean isAdmin() { |
|
39
|
|
|
return this.sessionService.getUserModel().getIsAdmin(); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
@GetMapping("") |
|
43
|
|
|
public List<User> list() throws ForbiddenException { |
|
44
|
|
|
if (!this.isAdmin()) { |
|
45
|
|
|
throw new ForbiddenException(); |
|
46
|
|
|
} |
|
47
|
|
|
final List<UserModelInterface> usersModels = this.usersService.findAll(); |
|
48
|
|
|
|
|
49
|
|
|
return usersModels |
|
50
|
|
|
.stream() |
|
51
|
|
|
.map(model -> this.usersMapper.map(model, User.class)) |
|
52
|
|
|
.collect(Collectors.toList()); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* create |
|
57
|
|
|
*/ |
|
58
|
|
|
/** |
|
59
|
|
|
* update |
|
60
|
|
|
*/ |
|
61
|
|
|
@GetMapping("/{userId}") |
|
62
|
|
|
public User show(@PathVariable Integer userId) throws NotFoundException, ForbiddenException { |
|
63
|
|
|
final UserModelInterface userModel = this.usersService.find(userId); |
|
64
|
|
|
|
|
65
|
|
|
if (!this.isAdmin()) { |
|
66
|
|
|
throw new ForbiddenException(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
if (userModel == null) { |
|
70
|
|
|
throw new NotFoundException(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return this.usersMapper.map(userModel, User.class); |
|
74
|
|
|
} |
|
75
|
|
|
/** |
|
76
|
|
|
* delete(userId) |
|
77
|
|
|
*/ |
|
78
|
|
|
/** |
|
79
|
|
|
* showMe |
|
80
|
|
|
*/ |
|
81
|
|
|
} |
|
82
|
|
|
|