source: source/MovieZilla-master/src/main/java/com/example/demo/service/ServiceImpl/UserServiceImpl.java

Last change on this file was fc7ec52, checked in by darkopopovski <darkopopovski39@…>, 2 years ago

all files

  • Property mode set to 100644
File size: 2.5 KB
Line 
1package com.example.demo.service.ServiceImpl;
2
3import com.example.demo.exceptions.InvalidUsernameOrPasswordException;
4import com.example.demo.exceptions.PasswordsDoNotMatchException;
5import com.example.demo.exceptions.UsernameAlreadyExistsException;
6import com.example.demo.model.Role;
7import com.example.demo.model.User;
8import com.example.demo.repository.UserRepository;
9import com.example.demo.service.UserService;
10import org.springframework.security.core.userdetails.UserDetails;
11import org.springframework.security.core.userdetails.UsernameNotFoundException;
12import org.springframework.security.crypto.password.PasswordEncoder;
13import org.springframework.stereotype.Service;
14
15@Service
16public 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}
Note: See TracBrowser for help on using the repository browser.