Completed
Pull Request — dev (#375)
by
unknown
05:22 queued 39s
created

passgenerator(int)   A

Complexity

Conditions 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 7
rs 10
cc 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.core.models.UserModel;
8
import easytests.core.models.UserModelInterface;
9
import easytests.core.options.builder.UsersOptionsBuilderInterface;
10
import easytests.core.services.UsersServiceInterface;
11
import org.springframework.beans.factory.annotation.Autowired;
12
import org.springframework.beans.factory.annotation.Qualifier;
13
import org.springframework.http.HttpStatus;
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
    @Qualifier("UsersMapperV1")
33
    private UsersMapper usersMapper;
34
35
    /**
36
     * list
37
     */
38
39
    @PostMapping("")
40
    @ResponseStatus(HttpStatus.CREATED)
41
    public Identity create(@RequestBody User user) throws BadRequestException {
42
        if (user.getId() != null) {
43
            throw new IdentifiedModelException();
44
        }
45
        if (this.usersService.findByEmail(user.getEmail()) != null) {
46
            throw new BadRequestException("This email already exist.");
47
        }
48
49
        final UserModelInterface userModel = this.usersMapper.map(user, UserModel.class);
50
51
        userModel.setPassword(passgenerator(6));
52
53
        this.usersService.save(userModel);
54
55
        return this.usersMapper.map(userModel, Identity.class);
56
    }
57
    /**
58
     * update
59
     */
60
    /**
61
     * show(userId)
62
     */
63
    /**
64
     * delete(userId)
65
     */
66
    /**
67
     * showMe
68
     */
69
70
    private String passgenerator(int n) {
71
        final String dict = "qwertyuiopasdfghjklzxcvbnm1234567890QWERTYUIOPASDFGHJKLZXCVBNM";
72
        String pass = "";
73
        for (int i = 0; i < n; i++) {
74
            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...
75
        }
76
        return pass;
77
    }
78
}
79