Blame view

src/main/java/fr/plil/sio/web/mvc/UserServiceImpl.java 2.79 KB
2afad654   Julien Iguchi-Cartigny   deal with userForm
1
2
  package fr.plil.sio.web.mvc;
  
322c9502   Julien Cartigny   Ad Spring securit...
3
  import org.springframework.security.crypto.password.PasswordEncoder;
2afad654   Julien Iguchi-Cartigny   deal with userForm
4
5
6
7
  import org.springframework.stereotype.Service;
  import org.springframework.transaction.annotation.Transactional;
  
  import javax.annotation.Resource;
322c9502   Julien Cartigny   Ad Spring securit...
8
  import java.util.HashSet;
d0481dcb   Julien Cartigny   Support for user ...
9
  import java.util.List;
322c9502   Julien Cartigny   Ad Spring securit...
10
  import java.util.Set;
2afad654   Julien Iguchi-Cartigny   deal with userForm
11
12
13
14
15
16
  
  @Service("userService")
  public class UserServiceImpl implements UserService {
  
      @Resource
      private UserRepository userRepository;
c1d2852e   Zak   Ajout css
17
18
19
      
      @Resource
      private DetteRepository detteRepository;
2afad654   Julien Iguchi-Cartigny   deal with userForm
20
  
322c9502   Julien Cartigny   Ad Spring securit...
21
22
23
24
25
26
      @Resource
      private RoleRepository roleRepository;
  
      @Resource
      private PasswordEncoder passwordEncoder;
  
353d9d4f   Zak   désactivation des...
27
28
29
      @Resource
      private SecurityService securityService;
  
2afad654   Julien Iguchi-Cartigny   deal with userForm
30
31
32
33
34
      @Override
      @Transactional
      public User createUser(String username, String password) {
          User user = new User();
          user.setUsername(username);
322c9502   Julien Cartigny   Ad Spring securit...
35
36
37
38
          user.setPassword(passwordEncoder.encode(password));
          Set<Role> roles = new HashSet<>();
          roles.add(roleRepository.findByName("ROLE_USER"));
          user.setRoles(roles);
2afad654   Julien Iguchi-Cartigny   deal with userForm
39
40
41
          userRepository.save(user);
          return user;
      }
322c9502   Julien Cartigny   Ad Spring securit...
42
43
  
      @Override
4abe2e3d   Julien Cartigny   Add @Transactionn...
44
      @Transactional(readOnly = true)
322c9502   Julien Cartigny   Ad Spring securit...
45
46
47
      public User findByUsername(String username) {
          return userRepository.findByUsername(username);
      }
d0481dcb   Julien Cartigny   Support for user ...
48
49
  
      @Override
4abe2e3d   Julien Cartigny   Add @Transactionn...
50
      @Transactional(readOnly = true)
ffbf5d61   Zak   Examen terminé.
51
52
      public List<Dette> findDettes() {
          String username = securityService.findLoggedInUsername();
c1d2852e   Zak   Ajout css
53
54
          User user = userRepository.findByUsername(username);
          return detteRepository.findByBorrower(user);
353d9d4f   Zak   désactivation des...
55
56
57
      }
  
      @Override
95ea306a   Zak   Ajout css, img et...
58
59
60
61
62
63
64
65
      @Transactional(readOnly = true)
      public List<Dette> findCreances() {
          String username = securityService.findLoggedInUsername();
          User user = userRepository.findByUsername(username);
          return detteRepository.findByCreditor(user);
      }
      
      @Override
353d9d4f   Zak   désactivation des...
66
      @Transactional(readOnly = true)
d0481dcb   Julien Cartigny   Support for user ...
67
68
69
      public List<User> findAll() {
          return userRepository.findAll();
      }
353d9d4f   Zak   désactivation des...
70
71
72
  
      @Override
      @Transactional
ffbf5d61   Zak   Examen terminé.
73
      public Dette addDette(String username, String sommeDette) {
353d9d4f   Zak   désactivation des...
74
          //Get the holder of debt
c1d2852e   Zak   Ajout css
75
76
77
78
79
80
81
82
83
84
85
86
87
           User user = userRepository.findByUsername(username);
  
          String connectedUsername = securityService.findLoggedInUsername();
          User connectedUser = userRepository.findByUsername(connectedUsername);
  
          Dette d = new Dette();
          Double dSomme = Double.parseDouble(sommeDette);
          d.setSommeDette(dSomme);
          d.setBorrower(connectedUser);
          d.setCreditor(user);
  
          detteRepository.save(d);
          return d;
353d9d4f   Zak   désactivation des...
88
89
      }
  
8a362652   Zak   Ajout css, js et ...
90
91
92
93
94
95
96
97
98
99
100
101
102
      @Override
      @Transactional
      public String deleteDette(String id){        
          
           String dId = id.split("=")[1];
          
          
           Dette d = detteRepository.findById(Long.parseLong(dId));
          detteRepository.delete(d);
          return id;        
          
      }
      
2afad654   Julien Iguchi-Cartigny   deal with userForm
103
  }