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

Last change on this file since 1e7126f was 5444409, checked in by milamihajlovska <mila.mihajlovska01@…>, 21 months ago

update register and registerWorker

  • Property mode set to 100644
File size: 3.3 KB
Line 
1package com.example.moviezone.service.Impl;
2
3import com.example.moviezone.model.*;
4import com.example.moviezone.model.exceptions.InvalidUsernameOrPasswordException;
5import com.example.moviezone.model.exceptions.PasswordsDoNotMatchException;
6import com.example.moviezone.model.exceptions.UserNotFoundException;
7import com.example.moviezone.repository.CustomerRepository;
8import com.example.moviezone.repository.UserRepository;
9import com.example.moviezone.repository.WorkerRepository;
10import com.example.moviezone.service.UserService;
11import org.springframework.security.crypto.password.PasswordEncoder;
12import org.springframework.stereotype.Service;
13
14import java.util.List;
15
16@Service
17public 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}
Note: See TracBrowser for help on using the repository browser.