Blame view

src/main/java/fr/plil/sio/persistence/jdbc/RightServiceJdbc.java 2.13 KB
a800fde0   jcartign   First version, in...
1
2
3
4
5
  package fr.plil.sio.persistence.jdbc;
  
  import fr.plil.sio.persistence.api.Right;
  import fr.plil.sio.persistence.api.RightService;
  import org.springframework.stereotype.Service;
fa02d31b   rvangrev   Commit avant le m...
6
  import java.lang.Math;
a800fde0   jcartign   First version, in...
7
  
5418cb05   jcartign   Javadoc of API is...
8
  import java.util.List;
e1ed8749   Rémi Vangrevelynghe   save before merge
9
  import org.springframework.beans.factory.annotation.Autowired;
5418cb05   jcartign   Javadoc of API is...
10
  
a800fde0   jcartign   First version, in...
11
  @Service
292aec6f   jcartign   First version bef...
12
  public class RightServiceJdbc implements RightService {
ef4eb263   rvangrev   Nouvelles modific...
13
      
e1ed8749   Rémi Vangrevelynghe   save before merge
14
      @Autowired
e1ed8749   Rémi Vangrevelynghe   save before merge
15
16
      private RightRepository rightRepository;
      
a800fde0   jcartign   First version, in...
17
18
      @Override
      public Right create(String name) {
ef4eb263   rvangrev   Nouvelles modific...
19
20
21
          if(name==null){
              throw new IllegalArgumentException("name cannot be null");
          }
e1ed8749   Rémi Vangrevelynghe   save before merge
22
23
          Right right = new Right();
          right.setName(name);
fa02d31b   rvangrev   Commit avant le m...
24
25
          rightRepository.save(right, 0);
              
e1ed8749   Rémi Vangrevelynghe   save before merge
26
          return right;
a800fde0   jcartign   First version, in...
27
28
29
30
      }
  
      @Override
      public Right create(String name, Right parent) {
fa02d31b   rvangrev   Commit avant le m...
31
          if(name==null){
e1ed8749   Rémi Vangrevelynghe   save before merge
32
33
              throw new IllegalArgumentException("name cannot be null");
          }
fa02d31b   rvangrev   Commit avant le m...
34
35
36
37
38
39
40
          if(parent==null){
              throw new IllegalArgumentException("parent cannot be null");
          }
          if (parent.getId()==null){
              throw new IllegalArgumentException("parent id cannot be null");
          }
         
e1ed8749   Rémi Vangrevelynghe   save before merge
41
42
43
          Right right = new Right();
          right.setName(name);
          right.setParent(parent);
fa02d31b   rvangrev   Commit avant le m...
44
45
          int parentID = Math.toIntExact(parent.getId());
          rightRepository.save(right, parentID);
e1ed8749   Rémi Vangrevelynghe   save before merge
46
          return right;
a800fde0   jcartign   First version, in...
47
48
49
      }
  
      @Override
5418cb05   jcartign   Javadoc of API is...
50
      public boolean delete(Right right) {
ef4eb263   rvangrev   Nouvelles modific...
51
52
53
          if(right==null){
              throw new IllegalArgumentException("name cannot be null");
          }
73c004db   rvangrev   TP presque termin...
54
55
56
          if(rightRepository.findByName(right.getName()).size()==0){
              throw new IllegalArgumentException("right not in database");
          }
fa02d31b   rvangrev   Commit avant le m...
57
          return rightRepository.delete(right.getId());
a800fde0   jcartign   First version, in...
58
59
60
      }
  
      @Override
5418cb05   jcartign   Javadoc of API is...
61
      public List<Right> findByName(String name) {
fa02d31b   rvangrev   Commit avant le m...
62
63
64
65
          if(name==null){
              throw new IllegalArgumentException("name cannot be null");
          }
          return rightRepository.findByName(name);
5418cb05   jcartign   Javadoc of API is...
66
67
68
69
      }
  
      @Override
      public Right findOne(Long id) {
ef4eb263   rvangrev   Nouvelles modific...
70
71
72
          if(id==null){
              throw new IllegalArgumentException("name cannot be null");
          }
fa02d31b   rvangrev   Commit avant le m...
73
74
          return rightRepository.findOne(id);
          
a800fde0   jcartign   First version, in...
75
76
      }
  }