forked from dlizarra/spring-boot-react-webpack-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserServiceImpl.java
73 lines (57 loc) · 1.78 KB
/
UserServiceImpl.java
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
72
73
package com.dlizarra.starter.user;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.dlizarra.starter.role.RoleName;
import com.dlizarra.starter.role.Role;
import com.dlizarra.starter.support.orika.OrikaBeanMapper;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserRepository userRepository;
@Autowired
private OrikaBeanMapper mapper;
@Transactional
@Override
public void createUser(final UserDto userDto, final RoleName roleName) {
final User user = mapper.map(userDto, User.class);
final Role role = new Role();
role.setRolename(roleName);
role.setUser(user);
user.getRoles().add(role);
user.setEnabled(true);
userRepository.save(user);
}
@Transactional(readOnly = true)
@Override
public List<UserDto> getUsers() {
final List<User> users = userRepository.findAll(new Sort("id"));
final List<UserDto> usersDto = new ArrayList<UserDto>();
users.forEach(x -> usersDto.add(mapper.map(x, UserDto.class)));
return usersDto;
}
@Transactional(readOnly = true)
@Override
public UserDto getUser(final Integer id) {
return mapper.map(find(id), UserDto.class);
}
@Transactional
@Override
public void updateUser(final UserDto user) {
// TODO Auto-generated method stub
}
@Transactional
@Override
public void deleteUser(final Integer id) {
userRepository.delete(id);
}
@Transactional(readOnly = true)
private User find(final Integer id) {
final Optional<User> userOpt = userRepository.findOne(id);
return userOpt.orElseThrow(() -> new UserNotFoundException(id));
}
}