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

Last change on this file 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
RevLine 
[11dea8f]1package com.example.moviezone.service.Impl;
2
[5444409]3import com.example.moviezone.model.*;
[f9cc555]4import com.example.moviezone.model.exceptions.InvalidUsernameOrPasswordException;
[e097c1c]5import com.example.moviezone.model.exceptions.PasswordsDoNotMatchException;
[1b248e4]6import com.example.moviezone.model.exceptions.UserNotFoundException;
[f9cc555]7import com.example.moviezone.repository.CustomerRepository;
[1b248e4]8import com.example.moviezone.repository.UserRepository;
[f9cc555]9import com.example.moviezone.repository.WorkerRepository;
[11dea8f]10import com.example.moviezone.service.UserService;
[27adfc8]11import org.springframework.security.crypto.password.PasswordEncoder;
[11dea8f]12import org.springframework.stereotype.Service;
13
14import java.util.List;
15
16@Service
17public class UserServiceImpl implements UserService {
18
[1b248e4]19 private final UserRepository userRepository;
[27adfc8]20 private final PasswordEncoder passwordEncoder;
[f9cc555]21 private final WorkerRepository workerRepository;
22 private final CustomerRepository customerRepository;
[1b248e4]23
[f9cc555]24 public UserServiceImpl(UserRepository userRepository, PasswordEncoder passwordEncoder, WorkerRepository workerRepository, CustomerRepository customerRepository) {
[1b248e4]25 this.userRepository = userRepository;
[27adfc8]26 this.passwordEncoder = passwordEncoder;
[f9cc555]27 this.workerRepository = workerRepository;
28 this.customerRepository = customerRepository;
[1b248e4]29 }
[11dea8f]30
31 @Override
32 public List<User> findAllUsers() {
[1b248e4]33 return userRepository.findAll();
[11dea8f]34 }
35
36 @Override
[1b248e4]37 public User findById(Integer id) {
38 return userRepository.findById(id).orElseThrow(UserNotFoundException::new);
[11dea8f]39 }
[ac25203]40
41 @Override
42 public User findByUsername(String username) {
43 return userRepository.findByUsername(username);
44 }
45
[6032d44]46 @Override
[d7f5da9]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();
[f9cc555]52
53 if(role.equals(Role.ROLE_ADMIN))
[6032d44]54 {
[d7f5da9]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));
[6032d44]58 }
59 else
[f9cc555]60 {
[d7f5da9]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));
[f9cc555]64
65 }
66
[6032d44]67 }
68
69 @Override
70 public User login(String username, String password) {
71 return userRepository.findAllByUsernameAndPassword(username,password).stream().findFirst().orElseThrow(UserNotFoundException::new);
72 }
[27adfc8]73
[5444409]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
[ac25203]80
[11dea8f]81}
Note: See TracBrowser for help on using the repository browser.