Index: sources/app/src/main/java/parkup/services/RegistriranParkiracService.java
===================================================================
--- sources/app/src/main/java/parkup/services/RegistriranParkiracService.java	(revision ce6ad22249e902dea17e6ba473846ccebdd4330c)
+++ sources/app/src/main/java/parkup/services/RegistriranParkiracService.java	(revision 97fbc67a2265c2ab4f8d16b1d71ea5d43a129fa5)
@@ -1,20 +1,36 @@
 package parkup.services;
 
+import java.time.LocalDateTime;
 import java.util.List;
 import java.util.Objects;
 import java.util.Optional;
+import java.util.UUID;
 import javax.transaction.Transactional;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.jpa.repository.Modifying;
+import org.springframework.security.core.userdetails.UserDetails;
+import org.springframework.security.core.userdetails.UserDetailsService;
+import org.springframework.security.core.userdetails.UsernameNotFoundException;
+import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
 import org.springframework.stereotype.Service;
+import parkup.configs.email.EmailValidator;
+import parkup.configs.token.ConfirmationTokenRP;
+import parkup.configs.token.ConfirmationTokenService;
+import parkup.data.Tablicka;
 import parkup.entities.RegistriranParkirac;
 import parkup.repositories.RegistriranParkiracRepository;
 
 @Service
-public class RegistriranParkiracService {
+public class RegistriranParkiracService implements UserDetailsService {
     private final RegistriranParkiracRepository registriranParkiracRepository;
+    private final BCryptPasswordEncoder bCryptPasswordEncoder;
+    private final ConfirmationTokenService confirmationTokenService;
+    private EmailValidator emailValidator;
 
     @Autowired
-    public RegistriranParkiracService(RegistriranParkiracRepository registriranParkiracRepository) {
+    public RegistriranParkiracService(RegistriranParkiracRepository registriranParkiracRepository, BCryptPasswordEncoder bCryptPasswordEncoder, ConfirmationTokenService confirmationTokenService) {
         this.registriranParkiracRepository = registriranParkiracRepository;
+        this.bCryptPasswordEncoder = bCryptPasswordEncoder;
+        this.confirmationTokenService = confirmationTokenService;
     }
 
@@ -23,16 +39,22 @@
     }
 
-    public void addRegPark(RegistriranParkirac regPark) {
+    public Optional<RegistriranParkirac> addRegPark(RegistriranParkirac regPark) {
         Optional<RegistriranParkirac> regParkOpt = this.registriranParkiracRepository.findRegistriranParkiracByEmail(regPark.getEmail());
         if (regParkOpt.isPresent()) {
             throw new IllegalStateException("Email already taken, try adding a registriranParkirac with a different valid email address");
         } else {
-            System.out.println(regPark);
-            this.registriranParkiracRepository.save(regPark);
+            if (regPark.getEmail() != null && regPark.getEmail().length() > 1 && regPark.getEmail().contains("@")) {
+                //System.out.println(regPark);
+                this.registriranParkiracRepository.save(regPark);
+            }
+            else {
+                throw new IllegalStateException("email not valid");
+            }
         }
+        return regParkOpt;
     }
 
     @Transactional
-    public void updateRegPark(int regParkId, String name, String surname, String mobile, String email) {
+    public RegistriranParkirac updateRegPark(int regParkId, String name, String surname, String mobile, String email,List<Tablicka> regTablicki) {
         Optional<RegistriranParkirac> regParkOpt = Optional.ofNullable(this.registriranParkiracRepository.findByRegParkId(regParkId));
         if (regParkOpt.isPresent()) {
@@ -58,14 +80,24 @@
                 regParkNov.setMobile(mobile);
             }
+            regParkNov.setRegTablicki(regTablicki);
+            return regParkNov;
+        }else{
+            throw new IllegalStateException("RegistriranParkirac doesn't exist, therefore can't be updated");
         }
     }
 
-    public void deleteRegPark(int regParkId) {
+    @Transactional
+    @Modifying
+    public Optional<RegistriranParkirac> deleteRegPark(int regParkId) {
         Optional<RegistriranParkirac> regPark = Optional.ofNullable(this.registriranParkiracRepository.findByRegParkId(regParkId));
         if (regPark.isPresent()) {
+            //TODO da povikamo metod od ConfirmationTokenService za brisenje na ConfirmationTokenRP *DONE
+            //TODO da se izbrisat tablicki i da ne se dupliraat istite tablicki pri update ili add na nov registriranParkirac
+            this.confirmationTokenService.deleteByRegistriranParkirac_RegParkId(regParkId);
             this.registriranParkiracRepository.deleteByRegParkId(regParkId);
         } else {
             throw new IllegalStateException("RegistriranParkirac doesn't exist, therefore can't be deleted");
         }
+        return regPark;
     }
 
@@ -74,3 +106,50 @@
         return regPark.orElse(null);
     }
+
+    @Override
+    public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
+        return registriranParkiracRepository.findRegistriranParkiracByEmail(email)
+                .orElseThrow(() ->
+                        new UsernameNotFoundException("Email not found"));
+
+    }
+
+    public String signUpRegistriranParkirac(RegistriranParkirac registriranParkirac){
+        boolean registriranParkiracExists = registriranParkiracRepository
+                .findRegistriranParkiracByEmail(registriranParkirac.getEmail())
+                .isPresent();
+        if(registriranParkiracExists){
+            // TODO check if attributes are the same and
+            // TODO if email not confirmed send confirmation email
+
+            throw new IllegalStateException("Email already taken");
+        }
+
+        String encodedPassword = bCryptPasswordEncoder
+                .encode(registriranParkirac.getPassword());
+
+        registriranParkirac.setPassword(encodedPassword);
+
+        registriranParkiracRepository.save(registriranParkirac);
+
+        // TODO: Send confirmation token
+
+        String token = UUID.randomUUID().toString();
+        ConfirmationTokenRP confirmationTokenRP = new ConfirmationTokenRP(
+                token,
+                LocalDateTime.now(),
+                LocalDateTime.now().plusMinutes(15),
+                registriranParkirac
+        );
+
+        confirmationTokenService.saveConfirmationTokenRP(confirmationTokenRP);
+
+        // TODO: SEND EMAIL
+
+        return token;
+    }
+
+    public int enableRegistriranParkirac(String email) {
+        return registriranParkiracRepository.enableRegistriranParkirac(email);
+    }
 }
