package de.fau.amos.virtualledger.server.auth;

import de.fau.amos.virtualledger.server.model.User;
import de.fau.amos.virtualledger.server.persistence.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Controller / Service class for authentication
 */
@Component
public class AuthenticationController {

    private UserRepository userRepository;

    @Autowired
    public AuthenticationController(UserRepository userRepository) {
        this.userRepository = userRepository;
    }
    protected AuthenticationController() {
    }

    /**
     * register a new user, if attributes are null or don't follow the specific
     * pattern, an exception is thrown
     * 
     * @param user
     * @return
     * @throws VirtualLedgerAuthenticationException
     */
    public String register(User user) throws VirtualLedgerAuthenticationException {
        if (user == null || user.getEmail() == null
                || user.getFirstName() == null || user.getLastName() == null) {
            throw new VirtualLedgerAuthenticationException(
                    "Please check your inserts! At least one was not formatted correctly!");
        }
        if (this.userRepository.existsUserWithEmail(user.getEmail())) {
            throw new VirtualLedgerAuthenticationException("There already exists an account with this Email address.");
        }
        this.userRepository.createUser(user);

        return "You were registered! " + user.getEmail();
    }
}

@@ -923,16 +923,44 @@
 xception
+, UserAlreadyExistsException
  %7B%0A     
@@ -1347,103 +1347,35 @@
 new 
-VirtualLedgerAuthenticationException(%22There a
+UserA
 lready
- e
+E
 xists
- an account with this Email address.%22
+Exception(
 );%0A 

throw UserAlreadyExistsException in AuthenticationController.register() instead of non saying exception