UserServiceJdbc.java
2.12 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
72
73
74
package fr.plil.sio.persistence.jdbc;
import fr.plil.sio.persistence.api.Right;
import fr.plil.sio.persistence.api.Group;
import fr.plil.sio.persistence.api.User;
import fr.plil.sio.persistence.api.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceJdbc implements UserService {
@Autowired
private UserRepository userRepository;
@Autowired
private GroupRepository groupRepository;
@Override
public User create(String name, String groupName) {
if(name==null || groupName==null){
throw new IllegalArgumentException("name cannot be null");
}
User user = userRepository.findByName(name);
if(user!=null){
throw new IllegalArgumentException("a user with the same name already exists");
}
user = new User();
user.setName(name);
Group group = new Group();
group = groupRepository.findByName(groupName);
if(group!=null)
user.setGroup(group);
else{
throw new IllegalArgumentException("group trouve avec le find by name null");
}
userRepository.save(user, group);
return user;
}
@Override
public boolean delete(String name) {
if(name == null){
throw new IllegalArgumentException("name cannot be null");
}
User user = findByName(name);
if(user==null){
return false;
}
userRepository.delete(user.getId());
return true;
}
@Override
public User findByName(String name) {
if(name==null){
throw new IllegalArgumentException("name cannot be null");
}
return userRepository.findByName(name);
}
@Override
public boolean isUserHasRight(String userName, Right right) {
if(userName==null){
throw new IllegalArgumentException("name cannot be null");
}
if(right==null){
throw new IllegalArgumentException("right cannot be null");
}
else{
return false;
}
}
}