src/main/java/com/feljtech/istudybucket/business/service/impl/AuthServiceImpl.java
/**
     * implementation for user registeration
     * @param registerRequest : request body for registration
     * @return response entity based on success
     */
implementation for user registeration
@param registerRequest : request body for registration
@return response entity based on success
implementation for user registeration @param registerRequest : request body for registration @return response entity based on success
@Override
    @Transactional
    public ResponseEntity<?> registerAccount(RegisterRequest registerRequest) {
        AtomicBoolean userAlreadyExists = new AtomicBoolean(false);
        String newUsername = registerRequest.getUsername();
        String newEmail = registerRequest.getEmail();
        Optional<User> userOptional = userRepository.findByUsernameOrEmail(newUsername, newEmail);
        userOptional.ifPresentOrElse(
                user -> userAlreadyExists.set(true),
                () -> {
                    userAlreadyExists.set(false);
                    User newUser = User.builder() 
                            .username(registerRequest.getUsername())
                            .email(registerRequest.getEmail())
                            .password(this.encodePassword(registerRequest.getPassword())) 
                            .creationDate(Instant.now())
                            .userVerified(Boolean.FALSE)
                            .userRole(UserRole.USER)
                            .build();
                    userRepository.save(newUser); 
                    String verToken = this.generateVerificationToken(newUser);
                    VerificationEmail verificationEmail = VerificationEmail.builder()
                            .message("verifiy")
                            .subject("iSB-verification")
                            .recipient(newUser.getEmail())
                            .build();
                    verificationEmail.setVerificationUrl(BASE_URL.concat(verToken));
                    verificationEmail.setRecipientName(newUser.getUsername());
                    mailService.sendVerificationEmail(verificationEmail);
                }
        );
        return userAlreadyExists.get() ?
                new ResponseEntity<>("User already exists", HttpStatus.CONFLICT):
                new ResponseEntity<>("Registration successful", HttpStatus.OK);
    }
@ Override @ Transactional public ResponseEntity < ? > registerAccount ( RegisterRequest registerRequest ) { AtomicBoolean userAlreadyExists = new AtomicBoolean ( false ) ; String newUsername = registerRequest . getUsername ( ) ; String newEmail = registerRequest . getEmail ( ) ; Optional < User > userOptional = userRepository . findByUsernameOrEmail ( newUsername , newEmail ) ; userOptional . ifPresentOrElse ( user -> userAlreadyExists . set ( true ) , ( ) -> { userAlreadyExists . set ( false ) ; User newUser = User . builder ( ) . username ( registerRequest . getUsername ( ) ) . email ( registerRequest . getEmail ( ) ) . password ( this . encodePassword ( registerRequest . getPassword ( ) ) ) . creationDate ( Instant . now ( ) ) . userVerified ( Boolean . FALSE ) . userRole ( UserRole . USER ) . build ( ) ; userRepository . save ( newUser ) ; String verToken = this . generateVerificationToken ( newUser ) ; VerificationEmail verificationEmail = VerificationEmail . builder ( ) . message ( "verifiy" ) . subject ( "iSB-verification" ) . recipient ( newUser . getEmail ( ) ) . build ( ) ; verificationEmail . setVerificationUrl ( BASE_URL . concat ( verToken ) ) ; verificationEmail . setRecipientName ( newUser . getUsername ( ) ) ; mailService . sendVerificationEmail ( verificationEmail ) ; } ) ; return userAlreadyExists . get ( ) ? new ResponseEntity < > ( "User already exists" , HttpStatus . CONFLICT ) : new ResponseEntity < > ( "Registration successful" , HttpStatus . OK ) ; }
implementation for user registeration
 @param registerRequest : request body for registration
 @return response entity based on success
implementation for user registeration @param registerRequest : request body for registration @return response entity based on success
// query the database to find out if the user already exists // build the new User object from the register form // encode password // save user to database // first, generate the DefaultEmail object // add the verification url and recipient name to the email object // send the email through MailService