Completed
Pull Request — dev (#375)
by
unknown
12:38 queued 07:08
created

easytests.api.v1.controllers.UsersController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
isAdmin 0 2 ?
A create(User) 0 17 3
passgenerator 0 7 ?
A isAdmin() 0 2 1
A passgenerator(int) 0 7 2
A list() 0 11 2
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.Identity;
6
import easytests.api.v1.models.User;
7
import easytests.auth.services.SessionServiceInterface;
8
import easytests.core.models.UserModel;
9
import easytests.core.models.UserModelInterface;
10
import easytests.core.options.builder.UsersOptionsBuilderInterface;
11
import easytests.core.services.UsersServiceInterface;
12
import java.util.List;
13
import java.util.stream.Collectors;
14
import org.springframework.beans.factory.annotation.Autowired;
15
import org.springframework.beans.factory.annotation.Qualifier;
16
import org.springframework.http.HttpStatus;
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
    /**
46
     * list
47
     */
48
49
    @GetMapping("")
50
    public List<User> list() throws ForbiddenException {
51
        if (!this.isAdmin()) {
52
            throw new ForbiddenException();
53
        }
54
        final List<UserModelInterface> usersModels = this.usersService.findAll();
55
56
        return usersModels
57
                .stream()
58
                .map(model -> this.usersMapper.map(model, User.class))
59
                .collect(Collectors.toList());
60
    }
61
    /**
62
     * create
63
     */
64
65
    @PostMapping("")
66
    @ResponseStatus(HttpStatus.CREATED)
67
    public Identity create(@RequestBody User user) throws BadRequestException {
68
        if (user.getId() != null) {
69
            throw new IdentifiedModelException();
70
        }
71
        if (this.usersService.findByEmail(user.getEmail()) != null) {
72
            throw new BadRequestException("This email already exist.");
73
        }
74
75
        final UserModelInterface userModel = this.usersMapper.map(user, UserModel.class);
76
77
        userModel.setPassword(passgenerator(6));
78
79
        this.usersService.save(userModel);
80
81
        return this.usersMapper.map(userModel, Identity.class);
82
    }
83
 
84
    /**
85
     * update
86
     */
87
    /**
88
     * show(userId)
89
     */
90
    /**
91
     * delete(userId)
92
     */
93
    /**
94
     * showMe
95
     */
96
97
    private String passgenerator(int n) {
98
        final String dict = "qwertyuiopasdfghjklzxcvbnm1234567890QWERTYUIOPASDFGHJKLZXCVBNM";
99
        String pass = "";
100
        for (int i = 0; i < n; i++) {
101
            pass = pass + (dict.charAt(0 + (int) (Math.random() * dict.length())));
0 ignored issues
show
Performance introduced by
String concatenation with + is inefficient. Doing so in a loop may incur a significant performance penalty. Consider using a StringBuilder instead
Loading history...
introduced by
Use "java.util.Random.nextInt()" instead.
Loading history...
102
        }
103
        return pass;
104
    }
105
}
106