1 | package com.project.beautycenter.service.impl;
|
---|
2 | import com.project.beautycenter.model.Users;
|
---|
3 | import com.project.beautycenter.model.exceptions.InvalidArgumentException;
|
---|
4 | import com.project.beautycenter.model.exceptions.InvalidUsernameException;
|
---|
5 | import com.project.beautycenter.model.exceptions.InvalidUsernameOrPasswordException;
|
---|
6 | import com.project.beautycenter.repository.UsersRepository;
|
---|
7 | import com.project.beautycenter.service.UsersService;
|
---|
8 | import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
---|
9 | import org.springframework.security.core.userdetails.User;
|
---|
10 | import org.springframework.security.core.userdetails.UserDetails;
|
---|
11 | import org.springframework.security.core.userdetails.UserDetailsService;
|
---|
12 | import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
---|
13 | import org.springframework.security.crypto.password.PasswordEncoder;
|
---|
14 | import org.springframework.stereotype.Service;
|
---|
15 |
|
---|
16 | import javax.management.relation.Role;
|
---|
17 | import java.util.List;
|
---|
18 | import java.util.stream.Collectors;
|
---|
19 | import java.util.stream.Stream;
|
---|
20 |
|
---|
21 | @Service
|
---|
22 | public class UsersServiceImpl implements UsersService {
|
---|
23 | private final UsersRepository usersRepository;
|
---|
24 | private final PasswordEncoder passwordEncoder;
|
---|
25 |
|
---|
26 | public UsersServiceImpl(UsersRepository usersRepository, PasswordEncoder passwordEncoder) {
|
---|
27 | this.usersRepository = usersRepository;
|
---|
28 | this.passwordEncoder = passwordEncoder;
|
---|
29 | }
|
---|
30 |
|
---|
31 | @Override
|
---|
32 | public List<Users> findAll() {
|
---|
33 | return this.usersRepository.findAll();
|
---|
34 | }
|
---|
35 |
|
---|
36 | @Override
|
---|
37 | public Users findbyId(Integer id) {
|
---|
38 | return this.usersRepository.findById(id).orElseThrow(NullPointerException::new);
|
---|
39 | }
|
---|
40 |
|
---|
41 | @Override
|
---|
42 | public Users create(String username, String password) {
|
---|
43 | String encryptedpassword = this.passwordEncoder.encode(password);
|
---|
44 | Users users = new Users(username, password);
|
---|
45 | return this.usersRepository.save(users);
|
---|
46 | }
|
---|
47 |
|
---|
48 | public Users create(Users user) {
|
---|
49 | Users users = new Users(user.getUsername(), passwordEncoder.encode(user.getPassword()));
|
---|
50 | return this.usersRepository.save(users);
|
---|
51 | }
|
---|
52 |
|
---|
53 | @Override
|
---|
54 | public Users login(String username, String password) {
|
---|
55 | if (username == null || username.isEmpty() || password == null || password.isEmpty()) {
|
---|
56 | throw new InvalidArgumentException();
|
---|
57 | }
|
---|
58 | return usersRepository.findByUsernameAndUpassword(username, password);
|
---|
59 | }
|
---|
60 |
|
---|
61 | }
|
---|
62 |
|
---|