1 | package com.example.moviezone.service.Impl;
|
---|
2 |
|
---|
3 | import com.example.moviezone.model.Customer;
|
---|
4 | import com.example.moviezone.model.User;
|
---|
5 | import com.example.moviezone.model.Worker;
|
---|
6 | import com.example.moviezone.model.exceptions.UserNotFoundException;
|
---|
7 | import com.example.moviezone.repository.UserRepository;
|
---|
8 | import com.example.moviezone.service.UserService;
|
---|
9 | import org.springframework.security.crypto.password.PasswordEncoder;
|
---|
10 | import org.springframework.stereotype.Service;
|
---|
11 |
|
---|
12 | import java.util.List;
|
---|
13 |
|
---|
14 | @Service
|
---|
15 | public class UserServiceImpl implements UserService {
|
---|
16 |
|
---|
17 | private final UserRepository userRepository;
|
---|
18 | private final PasswordEncoder passwordEncoder;
|
---|
19 |
|
---|
20 | public UserServiceImpl(UserRepository userRepository, PasswordEncoder passwordEncoder) {
|
---|
21 | this.userRepository = userRepository;
|
---|
22 | this.passwordEncoder = passwordEncoder;
|
---|
23 | }
|
---|
24 |
|
---|
25 | @Override
|
---|
26 | public List<User> findAllUsers() {
|
---|
27 | return userRepository.findAll();
|
---|
28 | }
|
---|
29 |
|
---|
30 | @Override
|
---|
31 | public User findById(Integer id) {
|
---|
32 | return userRepository.findById(id).orElseThrow(UserNotFoundException::new);
|
---|
33 | }
|
---|
34 |
|
---|
35 | @Override
|
---|
36 | public User findByUsername(String username) {
|
---|
37 | return userRepository.findByUsername(username);
|
---|
38 | }
|
---|
39 |
|
---|
40 | @Override
|
---|
41 | public User register(String first_name, String last_name, String username, String email, String number, String password, String role) {
|
---|
42 | if(role.equals("worker"))
|
---|
43 | {
|
---|
44 | return userRepository.save(new Worker(passwordEncoder.encode(password),first_name,last_name,username,email,number));
|
---|
45 | }
|
---|
46 | else
|
---|
47 | return userRepository.save(new Customer(passwordEncoder.encode(password),first_name,last_name,username,email,number));
|
---|
48 | }
|
---|
49 |
|
---|
50 | @Override
|
---|
51 | public User login(String username, String password) {
|
---|
52 | return userRepository.findAllByUsernameAndPassword(username,password).stream().findFirst().orElseThrow(UserNotFoundException::new);
|
---|
53 | }
|
---|
54 |
|
---|
55 |
|
---|
56 | }
|
---|