Blame view

src/main/java/fr/plil/sio/persistence/jpa/UserServiceJpa.java 1.52 KB
fd5e74ca   jcartign   First student ver...
1
2
  package fr.plil.sio.persistence.jpa;
  
fb8c34cc   msahmane   RightJPA à 100%, ...
3
  import fr.plil.sio.persistence.api.Group;
fd5e74ca   jcartign   First student ver...
4
5
6
  import fr.plil.sio.persistence.api.Right;
  import fr.plil.sio.persistence.api.User;
  import fr.plil.sio.persistence.api.UserService;
fb8c34cc   msahmane   RightJPA à 100%, ...
7
  import org.springframework.beans.factory.annotation.Autowired;
fd5e74ca   jcartign   First student ver...
8
9
10
11
  import org.springframework.stereotype.Service;
  
  @Service
  public class UserServiceJpa implements UserService {
9ae0ef87   msahmane   Amélioration des ...
12
      
fb8c34cc   msahmane   RightJPA à 100%, ...
13
      @Autowired
9ae0ef87   msahmane   Amélioration des ...
14
      UserRepository userRepository;
fb8c34cc   msahmane   RightJPA à 100%, ...
15
16
17
      
      @Autowired
      GroupRepository groupRepository;    
9ae0ef87   msahmane   Amélioration des ...
18
      
fd5e74ca   jcartign   First student ver...
19
20
      @Override
      public User create(String name, String groupName) {
fb8c34cc   msahmane   RightJPA à 100%, ...
21
22
23
24
25
26
27
28
          if(name == null)
              throw new IllegalArgumentException("Name cannot be null");
          if(groupName == null)
              throw new IllegalArgumentException("Group name cannot be null");
          
          Group group = groupRepository.findByName(groupName);
          if(group == null)
              throw new IllegalStateException("Group not found");
9ae0ef87   msahmane   Amélioration des ...
29
          
fb8c34cc   msahmane   RightJPA à 100%, ...
30
          User user = new User();
9ae0ef87   msahmane   Amélioration des ...
31
32
          user.setName(name);
          user.setGroup(group);
fb8c34cc   msahmane   RightJPA à 100%, ...
33
34
35
          userRepository.save(user);
          
          return user;
fd5e74ca   jcartign   First student ver...
36
37
38
39
      }
  
      @Override
      public boolean delete(String name) {
fb8c34cc   msahmane   RightJPA à 100%, ...
40
        
9ae0ef87   msahmane   Amélioration des ...
41
          
fd5e74ca   jcartign   First student ver...
42
43
44
45
46
          return false;
      }
  
      @Override
      public User findByName(String name) {
fb8c34cc   msahmane   RightJPA à 100%, ...
47
48
49
50
51
          
          if(name == null)
              throw new IllegalArgumentException("Name cannot be null");
                  
          return userRepository.findByName(name);
fd5e74ca   jcartign   First student ver...
52
53
54
55
      }
  
      @Override
      public boolean isUserHasRight(String userName, Right right) {
fb8c34cc   msahmane   RightJPA à 100%, ...
56
        
fd5e74ca   jcartign   First student ver...
57
58
59
          return false;
      }
  }