Completed
Pull Request — dev (#388)
by
unknown
09:18 queued 04:25
created

easytests.api.v1.controllers.UsersController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 8
c 4
b 1
f 0
dl 0
loc 73
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
isAdmin 0 2 ?
A isAdmin() 0 2 1
A list() 0 11 2
A getUserModel(Integer,UsersOptionsInterface) 0 6 2
A getUserModel(Integer) 0 2 1
A update(User) 0 11 2
1
package easytests.api.v1.controllers;
2
3
import easytests.api.v1.exceptions.BadRequestException;
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
     * create
60
     */
61
    /**
62
     * update
63
     */
64
65
    @PutMapping("")
66
    public void update(@RequestBody User user) throws BadRequestException, NotFoundException {
67
        if (user.getId() == null) {
68
            throw new UnidentifiedModelException();
69
        }
70
71
        final UserModelInterface userModel = this.getUserModel(user.getId());
72
73
        this.usersMapper.map(user, userModel);
74
75
        this.usersService.save(userModel);
76
    }
77
    /**
78
     * show(userId)
79
     */
80
    /**
81
     * delete(userId)
82
     */
83
    /**
84
     * showMe
85
     */
86
87
    private UserModelInterface getUserModel(Integer id, UsersOptionsInterface userOptions) throws NotFoundException {
88
        final UserModelInterface userModel = this.usersService.find(id, userOptions);
89
        if (userModel == null) {
90
            throw new NotFoundException();
91
        }
92
        return userModel;
93
    }
94
95
    private UserModelInterface getUserModel(Integer id) throws NotFoundException {
96
        return this.getUserModel(id, this.usersOptionsBuilder.forAuth());
97
    }
98
}
99