source: src/main/java/com/example/moviezone/service/Impl/UserServiceImpl.java@ fc448f5

Last change on this file since fc448f5 was d7f5da9, checked in by milamihajlovska <mila.mihajlovska01@…>, 22 months ago

changes for login (not working)

  • Property mode set to 100644
File size: 3.0 KB
Line 
1package com.example.moviezone.service.Impl;
2
3import com.example.moviezone.model.Customer;
4import com.example.moviezone.model.Role;
5import com.example.moviezone.model.User;
6import com.example.moviezone.model.Worker;
7import com.example.moviezone.model.exceptions.InvalidUsernameOrPasswordException;
8import com.example.moviezone.model.exceptions.PasswordsDoNotMatchException;
9import com.example.moviezone.model.exceptions.UserNotFoundException;
10import com.example.moviezone.repository.CustomerRepository;
11import com.example.moviezone.repository.UserRepository;
12import com.example.moviezone.repository.WorkerRepository;
13import com.example.moviezone.service.UserService;
14import org.springframework.security.crypto.password.PasswordEncoder;
15import org.springframework.stereotype.Service;
16
17import java.util.List;
18
19@Service
20public 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 void register(String first_name, String last_name, String username, String email, String number, String password, 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// User user= new User(passwordEncoder.encode(password),first_name,last_name,username,email,number);
59// workerRepository.save((Worker) user);
60 userRepository.save(new Worker(password,first_name,last_name,email,number,username));
61 }
62 else
63 {
64// Customer customer=new Customer(passwordEncoder.encode(password),first_name,last_name,username,email,number);
65// customerRepository.save(customer);
66 userRepository.save(new Customer(password,first_name,last_name,email,number,username));
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}
Note: See TracBrowser for help on using the repository browser.