Completed
Pull Request — dev (#389)
by
unknown
07:46 queued 02:55
created

delete(Integer)   A

Complexity

Conditions 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 3 Features 0
Metric Value
c 3
b 3
f 0
dl 0
loc 13
rs 9.75
cc 3
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
    /**
62
     * show(userId)
63
     */
64
    @DeleteMapping("/{userId}")
65
    public void delete(@PathVariable Integer userId) throws NotFoundException, ForbiddenException {
66
        final UserModelInterface userModel = this.usersService.find(userId, this.usersOptionsBuilder.forDelete());
67
68
        if (!this.isAdmin()) {
69
            throw new ForbiddenException();
70
        }
71
72
        if (userModel == null) {
73
            throw new NotFoundException();
74
        }
75
76
        this.usersService.delete(userModel);
77
    }
78
79
    /**
80
     * showMe
81
     */
82
}
83