Ignore:
Timestamp:
02/06/23 17:05:56 (23 months ago)
Author:
milamihajlovska <mila.mihajlovska01@…>
Branches:
master
Children:
a69d48c
Parents:
43a1688
Message:

changes to register

Location:
src/main/java/com/example/moviezone/service
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/main/java/com/example/moviezone/service/Impl/UserServiceImpl.java

    r43a1688 rf9cc555  
    22
    33import com.example.moviezone.model.Customer;
     4import com.example.moviezone.model.Role;
    45import com.example.moviezone.model.User;
    56import com.example.moviezone.model.Worker;
     7import com.example.moviezone.model.exceptions.InvalidUsernameOrPasswordException;
    68import com.example.moviezone.model.exceptions.PasswordsDoNotMatchException;
    79import com.example.moviezone.model.exceptions.UserNotFoundException;
     10import com.example.moviezone.repository.CustomerRepository;
    811import com.example.moviezone.repository.UserRepository;
     12import com.example.moviezone.repository.WorkerRepository;
    913import com.example.moviezone.service.UserService;
    1014import org.springframework.security.crypto.password.PasswordEncoder;
     
    1822    private final UserRepository userRepository;
    1923    private final PasswordEncoder passwordEncoder;
     24    private final WorkerRepository workerRepository;
     25    private final CustomerRepository customerRepository;
    2026
    21     public UserServiceImpl(UserRepository userRepository, PasswordEncoder passwordEncoder) {
     27    public UserServiceImpl(UserRepository userRepository, PasswordEncoder passwordEncoder, WorkerRepository workerRepository, CustomerRepository customerRepository) {
    2228        this.userRepository = userRepository;
    2329        this.passwordEncoder = passwordEncoder;
     30        this.workerRepository = workerRepository;
     31        this.customerRepository = customerRepository;
    2432    }
    2533
     
    4048
    4149    @Override
    42     public User register(String first_name, String last_name, String username, String email, String number, String password,String repeatedPassword, String role) {
     50    public User register(String first_name, String last_name, String username, String email, String number, String password,String repeatedPassword, Role role) {
    4351       if(!password.equals(repeatedPassword))
    4452           throw new PasswordsDoNotMatchException();
    45        else
    46         if(role.equals("worker"))
     53       if (username==null || username.isEmpty()  || password==null || password.isEmpty())
     54            throw new InvalidUsernameOrPasswordException();
     55
     56       if(role.equals(Role.ROLE_ADMIN))
    4757        {
    48             return userRepository.save(new Worker(passwordEncoder.encode(password),first_name,last_name,username,email,number));
     58            Worker worker=new Worker(passwordEncoder.encode(password),first_name,last_name,username,email,number);
     59            workerRepository.save(worker);
     60            return userRepository.save(worker);
    4961        }
    5062        else
    51             return userRepository.save(new Customer(passwordEncoder.encode(password),first_name,last_name,username,email,number));
     63       {
     64           Customer customer=new Customer(passwordEncoder.encode(password),first_name,last_name,username,email,number);
     65          customerRepository.save(customer);
     66           return userRepository.save(customer);
     67
     68       }
     69
    5270    }
    5371
  • src/main/java/com/example/moviezone/service/UserService.java

    r43a1688 rf9cc555  
    11package com.example.moviezone.service;
    22
     3import com.example.moviezone.model.Role;
    34import com.example.moviezone.model.User;
    45
     
    1011    User findByUsername(String username);
    1112
    12   User register(String first_name, String last_name,String username, String email, String number, String password,String repeatedPassword, String role);
     13  User register(String first_name, String last_name,String username, String email, String number, String password,String repeatedPassword, Role role);
    1314    User login(String username,String password);
    1415}
Note: See TracChangeset for help on using the changeset viewer.