Advertisement
ImHungryHi

signupserviceimpl

Sep 17th, 2021
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. package cc.serviceops.account.dao;
  2. import cc.serviceops.account.SignupDto;
  3. import cc.serviceops.account.User;
  4. import cc.serviceops.account.helpers.UserRole;
  5. import cc.serviceops.organisation.Organisation;
  6. import cc.serviceops.organisation.dao.OrganisationRepository;
  7. import org.springframework.security.crypto.password.PasswordEncoder;
  8. import org.springframework.stereotype.Service;
  9.  
  10. @Service
  11. public class SignupServiceImpl implements SignupService {
  12. private final OrganisationRepository organisationRepository;
  13. private final UserRepository userRepository;
  14. private final PasswordEncoder passwordEncoder;
  15.  
  16. public SignupServiceImpl(OrganisationRepository organisationRepository,
  17. UserRepository userRepository, PasswordEncoder passwordEncoder){
  18. this.organisationRepository = organisationRepository;
  19. this.userRepository = userRepository;
  20. this.passwordEncoder = passwordEncoder;
  21. }
  22.  
  23. @Override
  24. public void customValidate(SignupDto signupDto) throws PasswordException {
  25. if (signupDto.getPassword() == null) {
  26. throw new PasswordException("password empty");
  27. }
  28. if (signupDto.getPassword().length() < 8) {
  29. throw new PasswordException("password should have at least 8 characters");
  30. }
  31. if (!signupDto.getPassword().equals(signupDto.getCheckPassword())) {
  32. throw new PasswordException("passwords dont match");
  33. }
  34. }
  35.  
  36. @Override
  37. public void save(SignupDto signupDto) {
  38. Organisation organisation = saveOrganisation(signupDto);
  39. saveUser(signupDto, organisation);
  40. }
  41.  
  42. private Organisation saveOrganisation(SignupDto signupDto) {
  43. Organisation organisation = new Organisation();
  44. organisation.setName(signupDto.getName());
  45. organisation.setAddress(signupDto.getAddress());
  46. organisation.setZipcode(signupDto.getZipcode());
  47. organisation.setCommunity(signupDto.getCommunity());
  48. organisation.setVat(signupDto.getVat());
  49. organisationRepository.save(organisation);
  50. return organisation;
  51. }
  52.  
  53. private User saveUser(SignupDto signupDto, Organisation organisation) {
  54. String password = passwordEncoder.encode(signupDto.getPassword());
  55.  
  56. User user = new User();
  57. user.setEmail(signupDto.getEmail());
  58. user.setPassword(password);
  59. user.setFirstName(signupDto.getFirstName());
  60. user.setLastName(signupDto.getLastName());
  61. user.setOrganisation(organisation);
  62. user.setRole(UserRole.ADMIN);
  63. userRepository.save(user);
  64. return user;
  65. }
  66.  
  67. @Override
  68. public User saveGuest(User user) {
  69. String password = passwordEncoder.encode(user.getPassword());
  70. user.setPassword(password);
  71. return userRepository.save(user);
  72. }
  73. }
  74.  
  75.  
  76.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement