Changeset e0ef1b1 in Git for src/main/java/com/wediscussmovies/project/service/impl/UserServiceImpl.java
- Timestamp:
- 02/04/22 23:22:13 (3 years ago)
- Branches:
- main
- Children:
- 5b447b0
- Parents:
- 3ded84d
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/main/java/com/wediscussmovies/project/service/impl/UserServiceImpl.java
r3ded84d re0ef1b1 1 1 package com.wediscussmovies.project.service.impl; 2 2 3 import com.wediscussmovies.project.model.*; 4 import com.wediscussmovies.project.model.exception.PasswordsDontMatchException; 5 import com.wediscussmovies.project.model.exception.UserWithEmailAlreadyExists; 6 import com.wediscussmovies.project.model.exception.UserWithUsernameAlreadyExists; 3 import com.wediscussmovies.project.model.exception.InvalidArgumentsException; 4 import com.wediscussmovies.project.model.exception.PasswordsDoNotMatchException; 5 import com.wediscussmovies.project.model.exception.UserNotExistException; 6 import com.wediscussmovies.project.model.User; 7 import com.wediscussmovies.project.model.exception.UsernameAlreadyExistsException; 7 8 import com.wediscussmovies.project.repository.UserRepository; 8 9 import com.wediscussmovies.project.service.UserService; 10 import org.springframework.security.core.userdetails.UserDetails; 11 import org.springframework.security.core.userdetails.UsernameNotFoundException; 12 import org.springframework.security.crypto.password.PasswordEncoder; 9 13 import org.springframework.stereotype.Service; 10 11 import javax.servlet.http.HttpServletRequest;12 import java.util.Optional;13 14 14 15 @Service 15 16 public class UserServiceImpl implements UserService { 17 16 18 private final UserRepository userRepository; 19 private final PasswordEncoder passwordEncoder; 17 20 18 public UserServiceImpl(UserRepository userRepository) { 21 22 public UserServiceImpl(UserRepository userRepository, PasswordEncoder passwordEncoder) { 19 23 this.userRepository = userRepository; 24 this.passwordEncoder = passwordEncoder; 20 25 } 21 26 22 27 @Override 23 public Optional<User> login(String email, String password) { 24 Optional<User> userEmail = userRepository.findByEmailAndPassword(email, password); 25 Optional<User> userUsername = userRepository.findByUsernameAndPassword(email, password); 26 if(userEmail.isPresent()) 27 return userEmail; 28 else if(userUsername.isPresent()) 29 return userUsername; 30 else 31 return Optional.empty(); 28 public User findByUsername(String username) { 29 return this.userRepository.findByUsername(username).orElseThrow(() -> new UserNotExistException(username)); 30 } 31 32 33 34 @Override 35 public User register( String email, String username, String password, String confirmPassword 36 , String name, String surname) { 37 38 if ( username.isEmpty() || password.isEmpty()) 39 throw new InvalidArgumentsException(); 40 if (!password.equals(confirmPassword)) 41 throw new PasswordsDoNotMatchException(); 42 if(this.userRepository.findByUsername(username).isPresent()) 43 throw new UsernameAlreadyExistsException(username); 44 User user = new User(email,username,passwordEncoder.encode(password),name,surname); 45 return userRepository.save(user); 46 32 47 } 33 48 34 49 @Override 35 public Optional<User> register(HttpServletRequest request, String email, String password, String confirmPassword, 36 String username, String name, String surname) { 37 if(email == null || email.isEmpty() || password == null || password.isEmpty() || 38 confirmPassword == null || confirmPassword.isEmpty() || username == null || username.isEmpty() || 39 name == null || name.isEmpty() || surname == null || surname.isEmpty()) { 40 request.getSession().setAttribute("error", "Not all of the fields had a value in them, check again."); 41 return Optional.empty(); 42 } 43 if(userRepository.findByUsername(username).isPresent()){ 44 request.getSession().setAttribute("error", new UserWithUsernameAlreadyExists(username).getMessage()); 45 return Optional.empty(); 46 } 47 if(userRepository.findByEmail(email).isPresent()){ 48 request.getSession().setAttribute("error", new UserWithEmailAlreadyExists(email).getMessage()); 49 return Optional.empty(); 50 } 51 if(!password.equals(confirmPassword)){ 52 request.getSession().setAttribute("error", new PasswordsDontMatchException().getMessage()); 53 return Optional.empty(); 54 } 55 User user = new User(email, username, password, name, surname); 56 return Optional.of(userRepository.save(user)); 50 public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { 51 return this.findByUsername(username); 57 52 } 58 53 }
Note:
See TracChangeset
for help on using the changeset viewer.