Blame view

src/main/java/fr/plil/sio/web/mvc/UserServiceImpl.java 2.39 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
17
  
  @Service("userService")
  public class UserServiceImpl implements UserService {
  
      @Resource
      private UserRepository userRepository;
  
322c9502   Julien Cartigny   Ad Spring securit...
18
19
20
21
22
23
      @Resource
      private RoleRepository roleRepository;
  
      @Resource
      private PasswordEncoder passwordEncoder;
  
353d9d4f   Zak   désactivation des...
24
25
26
      @Resource
      private SecurityService securityService;
  
2afad654   Julien Iguchi-Cartigny   deal with userForm
27
28
29
30
31
      @Override
      @Transactional
      public User createUser(String username, String password) {
          User user = new User();
          user.setUsername(username);
322c9502   Julien Cartigny   Ad Spring securit...
32
33
34
35
          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
36
37
38
          userRepository.save(user);
          return user;
      }
322c9502   Julien Cartigny   Ad Spring securit...
39
40
  
      @Override
4abe2e3d   Julien Cartigny   Add @Transactionn...
41
      @Transactional(readOnly = true)
322c9502   Julien Cartigny   Ad Spring securit...
42
43
44
      public User findByUsername(String username) {
          return userRepository.findByUsername(username);
      }
d0481dcb   Julien Cartigny   Support for user ...
45
46
  
      @Override
4abe2e3d   Julien Cartigny   Add @Transactionn...
47
      @Transactional(readOnly = true)
ffbf5d61   Zak   Examen terminé.
48
49
50
51
      public List<Dette> findDettes() {
          String username = securityService.findLoggedInUsername();
          List<Dette> dettes = userRepository.findByUsername(username).getDettes();
          return dettes;
353d9d4f   Zak   désactivation des...
52
53
54
55
      }
  
      @Override
      @Transactional(readOnly = true)
d0481dcb   Julien Cartigny   Support for user ...
56
57
58
      public List<User> findAll() {
          return userRepository.findAll();
      }
353d9d4f   Zak   désactivation des...
59
60
61
  
      @Override
      @Transactional
ffbf5d61   Zak   Examen terminé.
62
      public Dette addDette(String username, String sommeDette) {
353d9d4f   Zak   désactivation des...
63
          //Get the holder of debt
ffbf5d61   Zak   Examen terminé.
64
65
66
67
68
69
70
71
72
73
74
75
76
77
  //        User user = userRepository.findByUsername(username);
  //
  //        //Get connected user
  //        // Authentication auth = SecurityContextHolder.getContext().getAuthentication();
  //        String connectedUsername = securityService.findLoggedInUsername();
  //        User connectedUser = userRepository.findByUsername(connectedUsername);
  //        //Set dette        
  //        List<Dette> newDettes = user.getDettes();
  //        Dette d = new Dette();
  //        Double dSomme = Double.parseDouble(sommeDette);
  //        d.setSommeDette(dSomme);
  //        d.setHolderOfDebt(connectedUser);
  //        d.setDueToUser(user);
  //        newDettes.add(d);
353d9d4f   Zak   désactivation des...
78
          
ffbf5d61   Zak   Examen terminé.
79
          return new Dette();
353d9d4f   Zak   désactivation des...
80
81
      }
  
2afad654   Julien Iguchi-Cartigny   deal with userForm
82
  }