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