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

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

login and register in HomeController

  • Property mode set to 100644
File size: 2.1 KB
Line 
1package com.example.moviezone.service.Impl;
2
3import com.example.moviezone.model.Customer;
4import com.example.moviezone.model.User;
5import com.example.moviezone.model.Worker;
6import com.example.moviezone.model.exceptions.PasswordsDoNotMatchException;
7import com.example.moviezone.model.exceptions.UserNotFoundException;
8import com.example.moviezone.repository.UserRepository;
9import com.example.moviezone.service.UserService;
10import org.springframework.security.crypto.password.PasswordEncoder;
11import org.springframework.stereotype.Service;
12
13import java.util.List;
14
15@Service
16public class UserServiceImpl implements UserService {
17
18 private final UserRepository userRepository;
19 private final PasswordEncoder passwordEncoder;
20
21 public UserServiceImpl(UserRepository userRepository, PasswordEncoder passwordEncoder) {
22 this.userRepository = userRepository;
23 this.passwordEncoder = passwordEncoder;
24 }
25
26 @Override
27 public List<User> findAllUsers() {
28 return userRepository.findAll();
29 }
30
31 @Override
32 public User findById(Integer id) {
33 return userRepository.findById(id).orElseThrow(UserNotFoundException::new);
34 }
35
36 @Override
37 public User findByUsername(String username) {
38 return userRepository.findByUsername(username);
39 }
40
41 @Override
42 public User register(String first_name, String last_name, String username, String email, String number, String password,String repeatedPassword, String role) {
43 if(!password.equals(repeatedPassword))
44 throw new PasswordsDoNotMatchException();
45 else
46 if(role.equals("worker"))
47 {
48 return userRepository.save(new Worker(passwordEncoder.encode(password),first_name,last_name,username,email,number));
49 }
50 else
51 return userRepository.save(new Customer(passwordEncoder.encode(password),first_name,last_name,username,email,number));
52 }
53
54 @Override
55 public User login(String username, String password) {
56 return userRepository.findAllByUsernameAndPassword(username,password).stream().findFirst().orElseThrow(UserNotFoundException::new);
57 }
58
59
60}
Note: See TracBrowser for help on using the repository browser.