1 | package com.example.moviezone.service.Impl;
|
---|
2 |
|
---|
3 | import com.example.moviezone.model.Customer;
|
---|
4 | import com.example.moviezone.model.Role;
|
---|
5 | import com.example.moviezone.model.User;
|
---|
6 | import com.example.moviezone.model.Worker;
|
---|
7 | import com.example.moviezone.model.exceptions.InvalidUsernameOrPasswordException;
|
---|
8 | import com.example.moviezone.model.exceptions.PasswordsDoNotMatchException;
|
---|
9 | import com.example.moviezone.model.exceptions.UserNotFoundException;
|
---|
10 | import com.example.moviezone.repository.CustomerRepository;
|
---|
11 | import com.example.moviezone.repository.UserRepository;
|
---|
12 | import com.example.moviezone.repository.WorkerRepository;
|
---|
13 | import com.example.moviezone.service.UserService;
|
---|
14 | import org.springframework.security.crypto.password.PasswordEncoder;
|
---|
15 | import org.springframework.stereotype.Service;
|
---|
16 |
|
---|
17 | import java.util.List;
|
---|
18 |
|
---|
19 | @Service
|
---|
20 | public class UserServiceImpl implements UserService {
|
---|
21 |
|
---|
22 | private final UserRepository userRepository;
|
---|
23 | private final PasswordEncoder passwordEncoder;
|
---|
24 | private final WorkerRepository workerRepository;
|
---|
25 | private final CustomerRepository customerRepository;
|
---|
26 |
|
---|
27 | public UserServiceImpl(UserRepository userRepository, PasswordEncoder passwordEncoder, WorkerRepository workerRepository, CustomerRepository customerRepository) {
|
---|
28 | this.userRepository = userRepository;
|
---|
29 | this.passwordEncoder = passwordEncoder;
|
---|
30 | this.workerRepository = workerRepository;
|
---|
31 | this.customerRepository = customerRepository;
|
---|
32 | }
|
---|
33 |
|
---|
34 | @Override
|
---|
35 | public List<User> findAllUsers() {
|
---|
36 | return userRepository.findAll();
|
---|
37 | }
|
---|
38 |
|
---|
39 | @Override
|
---|
40 | public User findById(Integer id) {
|
---|
41 | return userRepository.findById(id).orElseThrow(UserNotFoundException::new);
|
---|
42 | }
|
---|
43 |
|
---|
44 | @Override
|
---|
45 | public User findByUsername(String username) {
|
---|
46 | return userRepository.findByUsername(username);
|
---|
47 | }
|
---|
48 |
|
---|
49 | @Override
|
---|
50 | public User register(String first_name, String last_name, String username, String email, String number, String password,String repeatedPassword, Role role) {
|
---|
51 | if(!password.equals(repeatedPassword))
|
---|
52 | throw new PasswordsDoNotMatchException();
|
---|
53 | if (username==null || username.isEmpty() || password==null || password.isEmpty())
|
---|
54 | throw new InvalidUsernameOrPasswordException();
|
---|
55 |
|
---|
56 | if(role.equals(Role.ROLE_ADMIN))
|
---|
57 | {
|
---|
58 | Worker worker=new Worker(passwordEncoder.encode(password),first_name,last_name,username,email,number);
|
---|
59 | workerRepository.save(worker);
|
---|
60 | return userRepository.save(worker);
|
---|
61 | }
|
---|
62 | else
|
---|
63 | {
|
---|
64 | Customer customer=new Customer(passwordEncoder.encode(password),first_name,last_name,username,email,number);
|
---|
65 | customerRepository.save(customer);
|
---|
66 | return userRepository.save(customer);
|
---|
67 |
|
---|
68 | }
|
---|
69 |
|
---|
70 | }
|
---|
71 |
|
---|
72 | @Override
|
---|
73 | public User login(String username, String password) {
|
---|
74 | return userRepository.findAllByUsernameAndPassword(username,password).stream().findFirst().orElseThrow(UserNotFoundException::new);
|
---|
75 | }
|
---|
76 |
|
---|
77 |
|
---|
78 | }
|
---|