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

passgenerator

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
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
    private String passgenerator(int n) {
0 ignored issues
show
Unused Code introduced by
Remove this unused method parameter "n".
Loading history...
36
        final String dict = "qwertyuiopasdfghjklzxcvbnm1234567890QWERTYUIOPASDFGHJKLZXCVBNM";
37
        String pass = "";
38
        for (int i = 0; i < 6; i++)
39
            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...
40
        return pass;
41
    }
42
43
        /**
44
     * list
45
     */
46
    @PostMapping("")
47
    @ResponseStatus(HttpStatus.CREATED)
48
    public Identity create(@RequestBody User user) throws BadRequestException {
49
        if (user.getId() != null) {
50
            throw new IdentifiedModelException();
51
        }
52
        if (this.usersService.findByEmail(user.getEmail()) != null) {
53
            throw new BadRequestException("This email already exist.");
54
        }
55
56
        final UserModelInterface userModel = this.usersMapper.map(user, UserModel.class);
57
58
        userModel.setPassword(passgenerator(6));
59
60
        this.usersService.save(userModel);
61
62
        return this.usersMapper.map(userModel, Identity.class);
63
    }
64
    /**
65
     * update
66
     */
67
    /**
68
     * show(userId)
69
     */
70
    /**
71
     * delete(userId)
72
     */
73
    /**
74
     * showMe
75
     */
76
}
77