Completed
Pull Request — dev (#375)
by Konstantin
10:50 queued 06:10
created

create(User)   A

Complexity

Conditions 3

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
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.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
        if (!this.sessionService.isUser()) {
43
            return false;
44
        }
45
        return this.sessionService.getUserModel().getIsAdmin();
46
    }
47
48
    /**
49
     * list
50
     */
51
52
    @GetMapping("")
53
    public List<User> list() throws ForbiddenException {
54
        if (!this.isAdmin()) {
55
            throw new ForbiddenException();
56
        }
57
        final List<UserModelInterface> usersModels = this.usersService.findAll();
58
59
        return usersModels
60
                .stream()
61
                .map(model -> this.usersMapper.map(model, User.class))
62
                .collect(Collectors.toList());
63
    }
64
    /**
65
     * create
66
     */
67
68
    @PostMapping("")
69
    @ResponseStatus(HttpStatus.CREATED)
70
    public Identity create(@RequestBody User user) throws BadRequestException {
71
        if (user.getId() != null) {
72
            throw new IdentifiedModelException();
73
        }
74
        if (this.usersService.findByEmail(user.getEmail()) != null) {
75
            throw new BadRequestException("This email already exist.");
76
        }
77
78
        final UserModelInterface userModel = this.usersMapper.map(user, UserModel.class);
79
80
        userModel.setPassword(passgenerator(6));
81
82
        this.usersService.save(userModel);
83
84
        return this.usersMapper.map(userModel, Identity.class);
85
    }
86
 
87
    /**
88
     * update
89
     */
90
    /**
91
     * show(userId)
92
     */
93
    /**
94
     * delete(userId)
95
     */
96
    /**
97
     * showMe
98
     */
99
100
    private String passgenerator(int n) {
101
        final String dict = "qwertyuiopasdfghjklzxcvbnm1234567890QWERTYUIOPASDFGHJKLZXCVBNM";
102
        String pass = "";
103
        for (int i = 0; i < n; i++) {
104
            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...
105
        }
106
        return pass;
107
    }
108
}
109