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

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

login method

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