1 | package com.example.demo.service.ServiceImpl;
|
---|
2 |
|
---|
3 | import com.example.demo.exceptions.InvalidUsernameOrPasswordException;
|
---|
4 | import com.example.demo.exceptions.PasswordsDoNotMatchException;
|
---|
5 | import com.example.demo.exceptions.UsernameAlreadyExistsException;
|
---|
6 | import com.example.demo.model.Role;
|
---|
7 | import com.example.demo.model.User;
|
---|
8 | import com.example.demo.repository.UserRepository;
|
---|
9 | import com.example.demo.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;
|
---|
13 | import org.springframework.stereotype.Service;
|
---|
14 |
|
---|
15 | @Service
|
---|
16 | public class UserServiceImpl implements UserService {
|
---|
17 |
|
---|
18 | private final UserRepository userRepository;
|
---|
19 | private final PasswordEncoder passwordEncoder;
|
---|
20 |
|
---|
21 | public UserServiceImpl(UserRepository userRepository, PasswordEncoder passwordEncoder) {
|
---|
22 | this.userRepository = userRepository;
|
---|
23 | this.passwordEncoder = passwordEncoder;
|
---|
24 | }
|
---|
25 |
|
---|
26 | @Override
|
---|
27 | public UserDetails loadUserByUsername(String s) {
|
---|
28 | return userRepository.findByUsername(s)
|
---|
29 | .orElseThrow(()->new UsernameNotFoundException(s));
|
---|
30 | }
|
---|
31 |
|
---|
32 |
|
---|
33 | // @Override
|
---|
34 | // public User register(Integer user_id, String user_username, String user_password, String user_repeatedPassword,
|
---|
35 | // String user_phone_number, String user_email, String user_name, String user_surname, Role user_role)
|
---|
36 | // {
|
---|
37 | // User user = new User(user_id,user_username,user_password,user_repeatedPassword,user_phone_number,user_email,user_name,user_surname,user_role);
|
---|
38 | // return userRepository.save(user);
|
---|
39 | //
|
---|
40 | // }
|
---|
41 |
|
---|
42 | @Override
|
---|
43 | public User register(Integer user_id, String user_username, String user_password, String user_repeatedPassword,
|
---|
44 | String user_phone_number, String user_email, String user_name, String user_surname, Role user_role)
|
---|
45 | {
|
---|
46 | if (user_username==null || user_username.isEmpty() || user_password==null || user_password.isEmpty())
|
---|
47 | throw new InvalidUsernameOrPasswordException();
|
---|
48 | if (!user_password.equals(user_repeatedPassword))
|
---|
49 | throw new PasswordsDoNotMatchException();
|
---|
50 | if(this.userRepository.findByUsername(user_username).isPresent())
|
---|
51 | throw new UsernameAlreadyExistsException(user_username);
|
---|
52 | User user = new User(user_username,passwordEncoder.encode(user_password),user_phone_number,user_email,user_name,user_surname,user_role);
|
---|
53 | return userRepository.save(user);
|
---|
54 | }
|
---|
55 |
|
---|
56 | }
|
---|