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