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

create(User)   A

Complexity

Conditions 3

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

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