[ce6ad22] | 1 | package parkup.services;
|
---|
| 2 |
|
---|
[97fbc67] | 3 | import java.time.LocalDateTime;
|
---|
[ce6ad22] | 4 | import java.util.List;
|
---|
| 5 | import java.util.Objects;
|
---|
| 6 | import java.util.Optional;
|
---|
[97fbc67] | 7 | import java.util.UUID;
|
---|
[ce6ad22] | 8 | import javax.transaction.Transactional;
|
---|
| 9 | import org.springframework.beans.factory.annotation.Autowired;
|
---|
[97fbc67] | 10 | import org.springframework.data.jpa.repository.Modifying;
|
---|
| 11 | import org.springframework.security.core.userdetails.UserDetails;
|
---|
| 12 | import org.springframework.security.core.userdetails.UserDetailsService;
|
---|
| 13 | import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
---|
| 14 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
---|
[ce6ad22] | 15 | import org.springframework.stereotype.Service;
|
---|
[97fbc67] | 16 | import parkup.configs.email.EmailValidator;
|
---|
| 17 | import parkup.configs.token.ConfirmationTokenRP;
|
---|
| 18 | import parkup.configs.token.ConfirmationTokenService;
|
---|
[9dd526f] | 19 | import parkup.data.Plate;
|
---|
| 20 | import parkup.entities.RegisteredUser;
|
---|
| 21 | import parkup.repositories.RegisteredUserRepository;
|
---|
[ce6ad22] | 22 |
|
---|
| 23 | @Service
|
---|
[9dd526f] | 24 | public class RegisteredUserService implements UserDetailsService {
|
---|
| 25 | private final RegisteredUserRepository registeredUserRepository;
|
---|
[97fbc67] | 26 | private final BCryptPasswordEncoder bCryptPasswordEncoder;
|
---|
| 27 | private final ConfirmationTokenService confirmationTokenService;
|
---|
[9dd526f] | 28 | private final EmailValidator emailValidator;
|
---|
[ce6ad22] | 29 | @Autowired
|
---|
[9dd526f] | 30 | public RegisteredUserService(RegisteredUserRepository registeredUserRepository, BCryptPasswordEncoder bCryptPasswordEncoder, ConfirmationTokenService confirmationTokenService, EmailValidator emailValidator) {
|
---|
| 31 | this.registeredUserRepository = registeredUserRepository;
|
---|
[97fbc67] | 32 | this.bCryptPasswordEncoder = bCryptPasswordEncoder;
|
---|
| 33 | this.confirmationTokenService = confirmationTokenService;
|
---|
[9dd526f] | 34 | this.emailValidator = emailValidator;
|
---|
[ce6ad22] | 35 | }
|
---|
| 36 |
|
---|
[9dd526f] | 37 | public List<RegisteredUser> getRegPark() {
|
---|
| 38 | return this.registeredUserRepository.findAll();
|
---|
[ce6ad22] | 39 | }
|
---|
| 40 |
|
---|
[9dd526f] | 41 | public Optional<RegisteredUser> addRegPark(RegisteredUser regPark) {
|
---|
| 42 | Optional<RegisteredUser> regParkOpt = this.registeredUserRepository.findRegisteredUserByEmail(regPark.getEmail());
|
---|
[ce6ad22] | 43 | if (regParkOpt.isPresent()) {
|
---|
| 44 | throw new IllegalStateException("Email already taken, try adding a registriranParkirac with a different valid email address");
|
---|
| 45 | } else {
|
---|
[97fbc67] | 46 | if (regPark.getEmail() != null && regPark.getEmail().length() > 1 && regPark.getEmail().contains("@")) {
|
---|
[9dd526f] | 47 | this.registeredUserRepository.save(regPark);
|
---|
[97fbc67] | 48 | }
|
---|
| 49 | else {
|
---|
| 50 | throw new IllegalStateException("email not valid");
|
---|
| 51 | }
|
---|
[ce6ad22] | 52 | }
|
---|
[97fbc67] | 53 | return regParkOpt;
|
---|
[ce6ad22] | 54 | }
|
---|
| 55 |
|
---|
| 56 | @Transactional
|
---|
[52df867] | 57 | public RegisteredUser updateRegPark(int regParkId,String password, String name, String surname, String mobile, String email, List<Plate> regTablicki) {
|
---|
[9dd526f] | 58 | Optional<RegisteredUser> regParkOpt = Optional.ofNullable(this.registeredUserRepository.findByRegParkId(regParkId));
|
---|
[ce6ad22] | 59 | if (regParkOpt.isPresent()) {
|
---|
[9dd526f] | 60 | RegisteredUser regParkNov = this.registeredUserRepository.findByRegParkId(regParkId);
|
---|
[ce6ad22] | 61 | if (email != null && email.length() > 1 && email.contains("@") && !Objects.equals(regParkNov.getEmail(), email)) {
|
---|
[9dd526f] | 62 | Optional<RegisteredUser> userOpt1 = this.registeredUserRepository.findRegisteredUserByEmail(email);
|
---|
[ce6ad22] | 63 | if (userOpt1.isPresent()) {
|
---|
| 64 | throw new IllegalStateException("email taken");
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | regParkNov.setEmail(email);
|
---|
| 68 | }
|
---|
[52df867] | 69 | if(!password.isEmpty()){
|
---|
| 70 | regParkNov.setPassword(bCryptPasswordEncoder.encode(password));
|
---|
| 71 | }
|
---|
[74fb0dc] | 72 | if (name != null && name.length() > 1 && !Objects.equals(regParkNov.getFirstName(), name)) {
|
---|
| 73 | regParkNov.setFirstName(name);
|
---|
[ce6ad22] | 74 | }
|
---|
| 75 |
|
---|
[74fb0dc] | 76 | if (surname != null && surname.length() > 1 && !Objects.equals(regParkNov.getLastName(), surname)) {
|
---|
| 77 | regParkNov.setLastName(surname);
|
---|
[ce6ad22] | 78 | }
|
---|
| 79 |
|
---|
| 80 | if (mobile != null && mobile.length() > 0 && !Objects.equals(regParkNov.getMobile(), mobile)) {
|
---|
| 81 | regParkNov.setMobile(mobile);
|
---|
| 82 | }
|
---|
[9dd526f] | 83 | regParkNov.setPlates(regTablicki);
|
---|
[97fbc67] | 84 | return regParkNov;
|
---|
| 85 | }else{
|
---|
| 86 | throw new IllegalStateException("RegistriranParkirac doesn't exist, therefore can't be updated");
|
---|
[ce6ad22] | 87 | }
|
---|
| 88 | }
|
---|
| 89 |
|
---|
[97fbc67] | 90 | @Transactional
|
---|
| 91 | @Modifying
|
---|
[9dd526f] | 92 | public Optional<RegisteredUser> deleteRegPark(int regParkId) {
|
---|
| 93 | Optional<RegisteredUser> regPark = Optional.ofNullable(this.registeredUserRepository.findByRegParkId(regParkId));
|
---|
[ce6ad22] | 94 | if (regPark.isPresent()) {
|
---|
[97fbc67] | 95 | //TODO da povikamo metod od ConfirmationTokenService za brisenje na ConfirmationTokenRP *DONE
|
---|
| 96 | //TODO da se izbrisat tablicki i da ne se dupliraat istite tablicki pri update ili add na nov registriranParkirac
|
---|
[9dd526f] | 97 | this.confirmationTokenService.deleteByRegisteredUser_RegParkId(regParkId);
|
---|
| 98 | this.registeredUserRepository.deleteByRegParkId(regParkId);
|
---|
[ce6ad22] | 99 | } else {
|
---|
| 100 | throw new IllegalStateException("RegistriranParkirac doesn't exist, therefore can't be deleted");
|
---|
| 101 | }
|
---|
[97fbc67] | 102 | return regPark;
|
---|
[ce6ad22] | 103 | }
|
---|
| 104 |
|
---|
[9dd526f] | 105 | public RegisteredUser findById(int regParkId) {
|
---|
| 106 | Optional<RegisteredUser> regPark = Optional.ofNullable(this.registeredUserRepository.findByRegParkId(regParkId));
|
---|
[ce6ad22] | 107 | return regPark.orElse(null);
|
---|
| 108 | }
|
---|
[97fbc67] | 109 |
|
---|
| 110 | @Override
|
---|
| 111 | public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
|
---|
[9dd526f] | 112 | return registeredUserRepository.findRegisteredUserByEmail(email)
|
---|
[97fbc67] | 113 | .orElseThrow(() ->
|
---|
| 114 | new UsernameNotFoundException("Email not found"));
|
---|
| 115 |
|
---|
| 116 | }
|
---|
| 117 |
|
---|
[9dd526f] | 118 | public String signUpRegistriranParkirac(RegisteredUser registeredUser){
|
---|
| 119 | if (registeredUser.getEmail() == null || registeredUser.getEmail().isEmpty())
|
---|
| 120 | throw new IllegalArgumentException("Empty email");
|
---|
| 121 | if (!emailValidator.test(registeredUser.getEmail()))
|
---|
| 122 | throw new IllegalArgumentException("Invalid email");
|
---|
| 123 | boolean registriranParkiracExists = registeredUserRepository
|
---|
| 124 | .findRegisteredUserByEmail(registeredUser.getEmail())
|
---|
[97fbc67] | 125 | .isPresent();
|
---|
| 126 | if(registriranParkiracExists){
|
---|
| 127 | throw new IllegalStateException("Email already taken");
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | String encodedPassword = bCryptPasswordEncoder
|
---|
[9dd526f] | 131 | .encode(registeredUser.getPassword());
|
---|
[97fbc67] | 132 |
|
---|
[9dd526f] | 133 | registeredUser.setPassword(encodedPassword);
|
---|
[97fbc67] | 134 |
|
---|
[9dd526f] | 135 | registeredUserRepository.save(registeredUser);
|
---|
[97fbc67] | 136 |
|
---|
| 137 | String token = UUID.randomUUID().toString();
|
---|
| 138 | ConfirmationTokenRP confirmationTokenRP = new ConfirmationTokenRP(
|
---|
| 139 | token,
|
---|
| 140 | LocalDateTime.now(),
|
---|
| 141 | LocalDateTime.now().plusMinutes(15),
|
---|
[9dd526f] | 142 | registeredUser
|
---|
[97fbc67] | 143 | );
|
---|
| 144 |
|
---|
| 145 | confirmationTokenService.saveConfirmationTokenRP(confirmationTokenRP);
|
---|
| 146 |
|
---|
| 147 | return token;
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | public int enableRegistriranParkirac(String email) {
|
---|
[9dd526f] | 151 | return registeredUserRepository.enableRegisteredUser(email);
|
---|
[97fbc67] | 152 | }
|
---|
[ce6ad22] | 153 | }
|
---|