Completed
Pull Request — dev (#393)
by
unknown
04:01
created

showme

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
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.SubjectsOptions;
9
import easytests.core.options.UsersOptions;
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
     * create
59
     */
60
    /**
61
     * update
62
     */
63
    /**
64
     * show(userId)
65
     */
66
    /**
67
     * delete(userId)
68
     */
69
    /**
70
     * showMe
71
     */
72
73
    @GetMapping("/me")
74
    public User showme() throws ForbiddenException, NotFoundException {
75
        final String userEmail = this.sessionService.getUserModel().getEmail();
76
        final UsersOptionsInterface userOptions = new UsersOptions().withSubjects(new SubjectsOptions());
77
        final UserModelInterface userModel = this.usersService.findByEmail(userEmail, userOptions);
78
        if (!this.sessionService.isUser()) {
79
            throw new ForbiddenException();
80
        }
81
        return this.usersMapper.map(userModel, User.class);
82
    }
83
84
    private UserModelInterface getUserModel(Integer id, UsersOptionsInterface userOptions) throws NotFoundException {
85
        final UserModelInterface userModel = this.usersService.find(id, userOptions);
86
        if (userModel == null) {
87
            throw new NotFoundException();
88
        }
89
        return userModel;
90
    }
91
92
    private UserModelInterface getUserModel(Integer id) throws NotFoundException {
0 ignored issues
show
Unused Code introduced by
Remove this unused private "getUserModel" method.
Loading history...
93
        return this.getUserModel(id, this.usersOptionsBuilder.forAuth());
94
    }
95
96
}
97