-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserService.java
More file actions
71 lines (56 loc) · 2.38 KB
/
UserService.java
File metadata and controls
71 lines (56 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package net.mnio.springbooter.services.user;
import net.mnio.jConcurrencyOrchestra.InterruptService;
import net.mnio.springbooter.controller.api.UserCreateOrUpdateDto;
import net.mnio.springbooter.persistence.model.User;
import net.mnio.springbooter.persistence.repositories.UserRepository;
import net.mnio.springbooter.util.security.PasswordUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import java.util.Optional;
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
@Autowired
private InterruptService interruptService;
public Page<User> findAll(final Pageable pageable) {
return userRepository.findAll(pageable);
}
public Optional<User> findByEmail(final String email) {
return userRepository.findByEmail(email.toLowerCase());
}
/*
* As soon there is distinguished logic between update and create more tests are needed.
* That's the reason why tracking test coverage is important.
*/
public User createUser(final UserCreateOrUpdateDto dto) {
final User user = new User();
return updateUser(user, dto);
}
public boolean checkPassword(final User user, final String plaintextPassword) {
return PasswordUtil.checkPassword(plaintextPassword, user.getPassword());
}
public User updateUser(final User user, final UserCreateOrUpdateDto dto) {
user.setEmail(StringUtils.stripToEmpty(dto.getEmail()).toLowerCase());
user.setName(StringUtils.stripToEmpty(dto.getName()));
if (StringUtils.isEmpty(user.getEmail())) {
throw new IllegalArgumentException("e-mail must not be empty");
}
if (StringUtils.isEmpty(dto.getPassword())) {
throw new IllegalArgumentException("password must not be empty");
}
final String encode = PasswordUtil.hashPasswordWithSalt(dto.getPassword());
user.setPassword(encode);
interruptService.interrupt(String.format("Before saving user '%s'", user.getName()));
return userRepository.save(user);
}
public void delete(final User user) {
if (user == null) {
return;
}
userRepository.delete(user);
}
}