[ce6ad22] | 1 | package parkup.services;
|
---|
| 2 |
|
---|
[f6bc52d] | 3 | import java.time.LocalDateTime;
|
---|
| 4 | import java.util.*;
|
---|
[ce6ad22] | 5 | import javax.transaction.Transactional;
|
---|
| 6 | import org.springframework.beans.factory.annotation.Autowired;
|
---|
[f6bc52d] | 7 | import org.springframework.data.jpa.repository.Modifying;
|
---|
| 8 | import org.springframework.security.core.userdetails.UserDetails;
|
---|
| 9 | import org.springframework.security.core.userdetails.UserDetailsService;
|
---|
| 10 | import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
---|
| 11 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
---|
[ce6ad22] | 12 | import org.springframework.stereotype.Service;
|
---|
[f6bc52d] | 13 | import parkup.configs.token.ConfirmationTokenW;
|
---|
| 14 | import parkup.configs.token.ConfirmationTokenService;
|
---|
[ce6ad22] | 15 | import parkup.entities.Vraboten;
|
---|
| 16 | import parkup.repositories.VrabotenRepository;
|
---|
[f6bc52d] | 17 | import parkup.data.VrabotenDemo;
|
---|
[ce6ad22] | 18 |
|
---|
| 19 | @Service
|
---|
[f6bc52d] | 20 | public class VrabotenService implements UserDetailsService {
|
---|
[ce6ad22] | 21 | private final VrabotenRepository vrabotenRepository;
|
---|
[f6bc52d] | 22 | private final BCryptPasswordEncoder bCryptPasswordEncoder;
|
---|
| 23 | private final ConfirmationTokenService confirmationTokenService;
|
---|
[ce6ad22] | 24 |
|
---|
| 25 | @Autowired
|
---|
[f6bc52d] | 26 | public VrabotenService(VrabotenRepository vrabotenRepository, BCryptPasswordEncoder bCryptPasswordEncoder, ConfirmationTokenService confirmationTokenService) {
|
---|
[ce6ad22] | 27 | this.vrabotenRepository = vrabotenRepository;
|
---|
[f6bc52d] | 28 | this.bCryptPasswordEncoder = bCryptPasswordEncoder;
|
---|
| 29 | this.confirmationTokenService = confirmationTokenService;
|
---|
[ce6ad22] | 30 | }
|
---|
| 31 |
|
---|
| 32 | public List<Vraboten> getVraboteni() {
|
---|
| 33 | return this.vrabotenRepository.findAll();
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | public Vraboten findById(int vrabotenId) {
|
---|
| 37 | Optional<Vraboten> vraboten = Optional.ofNullable(this.vrabotenRepository.findByVrabotenId(vrabotenId));
|
---|
| 38 | return vraboten.orElse(null);
|
---|
| 39 | }
|
---|
| 40 |
|
---|
[f6bc52d] | 41 | public List<VrabotenDemo> getAllVraboteniDemos() {
|
---|
| 42 | List<Vraboten> vraboteni = this.vrabotenRepository.findAll();
|
---|
| 43 | List<VrabotenDemo> vrabotenDemos = new ArrayList<>();
|
---|
| 44 | for (Vraboten v : vraboteni){
|
---|
| 45 | VrabotenDemo vd = new VrabotenDemo(v.getFirstName(), v.getLastName(), v.getEmail());
|
---|
| 46 | vrabotenDemos.add(vd);
|
---|
| 47 | }
|
---|
| 48 | return vrabotenDemos;
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | public Optional<Vraboten> addVraboten(Vraboten vraboten) {
|
---|
[ce6ad22] | 52 | Optional<Vraboten> vrabotenOpt = this.vrabotenRepository.findVrabotenByEmail(vraboten.getEmail());
|
---|
| 53 | if (vrabotenOpt.isPresent()) {
|
---|
| 54 | throw new IllegalStateException("Email already taken, try adding a vraboten with a different valid email address");
|
---|
| 55 | } else {
|
---|
[f6bc52d] | 56 | if (vraboten.getEmail() != null && vraboten.getEmail().length() > 1 && vraboten.getEmail().contains("@")) {
|
---|
| 57 | //System.out.println(vraboten);
|
---|
| 58 | this.vrabotenRepository.save(vraboten);
|
---|
| 59 | }
|
---|
| 60 | else{
|
---|
| 61 | throw new IllegalStateException("email not valid");
|
---|
| 62 | }
|
---|
[ce6ad22] | 63 | }
|
---|
[f6bc52d] | 64 | return vrabotenOpt;
|
---|
[ce6ad22] | 65 | }
|
---|
| 66 |
|
---|
| 67 | @Transactional
|
---|
[f6bc52d] | 68 | public Vraboten updateVraboten(int vrabotenId, String firstName, String lastName, String mobile, String email, String status) {
|
---|
[ce6ad22] | 69 | Optional<Vraboten> vrabotenOpt = Optional.ofNullable(this.vrabotenRepository.findByVrabotenId(vrabotenId));
|
---|
| 70 | if (vrabotenOpt.isPresent()) {
|
---|
| 71 | Vraboten vrabotenNov = this.vrabotenRepository.findByVrabotenId(vrabotenId);
|
---|
| 72 | if (email != null && email.length() > 1 && email.contains("@") && !Objects.equals(vrabotenNov.getEmail(), email)) {
|
---|
| 73 | Optional<Vraboten> userOpt1 = this.vrabotenRepository.findVrabotenByEmail(email);
|
---|
| 74 | if (userOpt1.isPresent()) {
|
---|
| 75 | throw new IllegalStateException("email taken");
|
---|
| 76 | }
|
---|
| 77 | vrabotenNov.setEmail(email);
|
---|
[f6bc52d] | 78 | } else {
|
---|
| 79 | throw new IllegalStateException("email not valid");
|
---|
[ce6ad22] | 80 | }
|
---|
| 81 |
|
---|
| 82 | if (firstName != null && firstName.length() > 1 && !Objects.equals(vrabotenNov.getFirstName(), firstName)) {
|
---|
| 83 | vrabotenNov.setFirstName(firstName);
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | if (lastName != null && lastName.length() > 1 && !Objects.equals(vrabotenNov.getLastName(), lastName)) {
|
---|
| 87 | vrabotenNov.setLastName(lastName);
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | if (mobile != null && mobile.length() > 0 && !Objects.equals(vrabotenNov.getMobile(), mobile)) {
|
---|
| 91 | vrabotenNov.setMobile(mobile);
|
---|
| 92 | }
|
---|
[f6bc52d] | 93 |
|
---|
| 94 | if (mobile != null && status.length() > 0 && !Objects.equals(vrabotenNov.getStatus(), status)) {
|
---|
| 95 | if(status.equals("raboti") || status.equals("ne raboti") || status.equals("na odmor") || status.equals("na boleduvanje")){
|
---|
| 96 | vrabotenNov.setStatus(status);
|
---|
| 97 | }else{
|
---|
| 98 | throw new IllegalStateException("Please enter one of the following statuses: 'raboti', 'ne raboti', 'na odmor', 'na boleduvanje'");
|
---|
| 99 | }
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | return vrabotenNov;
|
---|
[ce6ad22] | 103 | }
|
---|
[f6bc52d] | 104 | else{
|
---|
| 105 | throw new IllegalStateException("Vraboten doesn't exist, therefore can't be updated");
|
---|
| 106 | }
|
---|
| 107 | }//za menjanje password da se implementira
|
---|
[ce6ad22] | 108 |
|
---|
[f6bc52d] | 109 | @Modifying
|
---|
| 110 | @Transactional
|
---|
| 111 | public Optional<Vraboten> deleteVraboten(int vrabotenId) {
|
---|
[ce6ad22] | 112 | Optional<Vraboten> vrabotenOpt = Optional.ofNullable(this.vrabotenRepository.findByVrabotenId(vrabotenId));
|
---|
| 113 | if (vrabotenOpt.isPresent()) {
|
---|
[f6bc52d] | 114 | //TODO da povikamo metod od ConfirmationTokenService za brisenje na ConfirmationTokenW
|
---|
| 115 | this.confirmationTokenService.deleteByVraboten_VrabotenId(vrabotenId);
|
---|
[ce6ad22] | 116 | this.vrabotenRepository.deleteByVrabotenId(vrabotenId);
|
---|
| 117 | } else {
|
---|
| 118 | throw new IllegalStateException("Vraboten doesn't exist, therefore can't be deleted");
|
---|
| 119 | }
|
---|
[f6bc52d] | 120 | return vrabotenOpt;
|
---|
[ce6ad22] | 121 | }
|
---|
[f6bc52d] | 122 |
|
---|
| 123 | @Transactional
|
---|
| 124 | public void lockVrabotenAcc(int vrabotenId) {
|
---|
| 125 | Vraboten vrabotenOpt = this.vrabotenRepository.findByVrabotenId(vrabotenId);
|
---|
| 126 | if (vrabotenOpt!=null) {
|
---|
| 127 | vrabotenOpt.lockVraboten();
|
---|
| 128 | } else {
|
---|
| 129 | throw new IllegalStateException("Vraboten doesn't exist, therefore his account can't be locked/unlocked");
|
---|
| 130 | }
|
---|
| 131 | }//dali treba da stoi @Transactional gore nad funkcijata?
|
---|
| 132 |
|
---|
| 133 | @Override
|
---|
| 134 | public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
|
---|
| 135 | return vrabotenRepository.findVrabotenByEmail(email)
|
---|
| 136 | .orElseThrow(() ->
|
---|
| 137 | new UsernameNotFoundException("Email not found"));
|
---|
| 138 | }
|
---|
| 139 | public String signUpVraboten(Vraboten vraboten){
|
---|
| 140 | boolean vrabotenExists = vrabotenRepository
|
---|
| 141 | .findVrabotenByEmail(vraboten.getEmail())
|
---|
| 142 | .isPresent();
|
---|
| 143 | if(vrabotenExists){
|
---|
| 144 | // TODO check if attributes are the same and
|
---|
| 145 | // TODO if email not confirmed send confirmation email
|
---|
| 146 |
|
---|
| 147 | throw new IllegalStateException("Email already taken");
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | String encodedPassword = bCryptPasswordEncoder
|
---|
| 151 | .encode(vraboten.getPassword());
|
---|
| 152 |
|
---|
| 153 | vraboten.setPassword(encodedPassword);
|
---|
| 154 |
|
---|
| 155 | vrabotenRepository.save(vraboten);
|
---|
| 156 |
|
---|
| 157 | // TODO: Send confirmation token
|
---|
| 158 |
|
---|
| 159 | String token = UUID.randomUUID().toString();
|
---|
| 160 | ConfirmationTokenW confirmationTokenW = new ConfirmationTokenW(
|
---|
| 161 | token,
|
---|
| 162 | LocalDateTime.now(),
|
---|
| 163 | LocalDateTime.now().plusMinutes(15),
|
---|
| 164 | vraboten
|
---|
| 165 | );
|
---|
| 166 |
|
---|
| 167 | confirmationTokenService.saveConfirmationTokenW(confirmationTokenW);
|
---|
| 168 |
|
---|
| 169 | // TODO: SEND EMAIL
|
---|
| 170 |
|
---|
| 171 | return token;
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | public int enableVraboten(String email) {
|
---|
| 175 | return vrabotenRepository.enableVraboten(email);
|
---|
| 176 | }
|
---|
| 177 |
|
---|
[ce6ad22] | 178 | } |
---|