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
Files:
1 added
3 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}
  • src/main/java/com/example/moviezone/web/HomeController.java

    r43a1688 rf9cc555  
    44import com.example.moviezone.model.Customer;
    55import com.example.moviezone.model.Film;
     6import com.example.moviezone.model.Role;
    67import com.example.moviezone.model.User;
     8import com.example.moviezone.model.exceptions.PasswordsDoNotMatchException;
    79import com.example.moviezone.model.exceptions.UserNotFoundException;
    810import com.example.moviezone.service.*;
     
    9395    }
    9496
    95     @PostMapping("register")
    96     public String register(@RequestParam String username, @RequestParam String first_name, @RequestParam String last_name,
    97                          @RequestParam String password, @RequestParam String repeatedPassword,
    98                          @RequestParam String email, @RequestParam String number,
    99                          @RequestParam String role)
    100     {
    101         User user = null;
    102             user=userService.register(first_name,last_name,username,email,number,password,repeatedPassword,role);
     97    @PostMapping()
     98    public String register(@RequestParam String username,
     99                           @RequestParam String first_name,
     100                           @RequestParam String last_name,
     101                           @RequestParam String password,
     102                           @RequestParam String repeatedPassword,
     103                           @RequestParam String email,
     104                           @RequestParam String number,
     105                           @RequestParam Role role)
     106    {
     107        try {
     108           User user=userService.register(first_name,last_name,username,email,number,password,repeatedPassword,role);
    103109            return "redirect:/login";
     110        }catch (PasswordsDoNotMatchException exception)
     111        {
     112            return "redirect:/register?error=" + exception.getMessage();
     113        }
     114
    104115    }
    105116
Note: See TracChangeset for help on using the changeset viewer.