package com.project.beautycenter.service.impl; import com.project.beautycenter.model.Users; import com.project.beautycenter.model.exceptions.InvalidArgumentException; import com.project.beautycenter.model.exceptions.InvalidUsernameException; import com.project.beautycenter.model.exceptions.InvalidUsernameOrPasswordException; import com.project.beautycenter.repository.UsersRepository; import com.project.beautycenter.service.UsersService; import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.userdetails.User; 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.password.PasswordEncoder; import org.springframework.stereotype.Service; import javax.management.relation.Role; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; @Service public class UsersServiceImpl implements UsersService { private final UsersRepository usersRepository; private final PasswordEncoder passwordEncoder; public UsersServiceImpl(UsersRepository usersRepository, PasswordEncoder passwordEncoder) { this.usersRepository = usersRepository; this.passwordEncoder = passwordEncoder; } @Override public List findAll() { return this.usersRepository.findAll(); } @Override public Users findbyId(Integer id) { return this.usersRepository.findById(id).orElseThrow(NullPointerException::new); } @Override public Users create(String username, String password) { String encryptedpassword = this.passwordEncoder.encode(password); Users users = new Users(username, password); return this.usersRepository.save(users); } public Users create(Users user) { Users users = new Users(user.getUsername(), passwordEncoder.encode(user.getPassword())); return this.usersRepository.save(users); } @Override public Users login(String username, String password) { if (username == null || username.isEmpty() || password == null || password.isEmpty()) { throw new InvalidArgumentException(); } return usersRepository.findByUsernameAndUpassword(username, password); } }