source: src/main/java/com/project/beautycenter/service/impl/UsersServiceImpl.java

Last change on this file was 850b344, checked in by Tamara Simikj <tamara.simic12@…>, 2 years ago

Initial commit

  • Property mode set to 100644
File size: 2.4 KB
RevLine 
[850b344]1package com.project.beautycenter.service.impl;
2import com.project.beautycenter.model.Users;
3import com.project.beautycenter.model.exceptions.InvalidArgumentException;
4import com.project.beautycenter.model.exceptions.InvalidUsernameException;
5import com.project.beautycenter.model.exceptions.InvalidUsernameOrPasswordException;
6import com.project.beautycenter.repository.UsersRepository;
7import com.project.beautycenter.service.UsersService;
8import org.springframework.security.core.authority.SimpleGrantedAuthority;
9import org.springframework.security.core.userdetails.User;
10import org.springframework.security.core.userdetails.UserDetails;
11import org.springframework.security.core.userdetails.UserDetailsService;
12import org.springframework.security.core.userdetails.UsernameNotFoundException;
13import org.springframework.security.crypto.password.PasswordEncoder;
14import org.springframework.stereotype.Service;
15
16import javax.management.relation.Role;
17import java.util.List;
18import java.util.stream.Collectors;
19import java.util.stream.Stream;
20
21@Service
22public class UsersServiceImpl implements UsersService {
23 private final UsersRepository usersRepository;
24 private final PasswordEncoder passwordEncoder;
25
26 public UsersServiceImpl(UsersRepository usersRepository, PasswordEncoder passwordEncoder) {
27 this.usersRepository = usersRepository;
28 this.passwordEncoder = passwordEncoder;
29 }
30
31 @Override
32 public List<Users> findAll() {
33 return this.usersRepository.findAll();
34 }
35
36 @Override
37 public Users findbyId(Integer id) {
38 return this.usersRepository.findById(id).orElseThrow(NullPointerException::new);
39 }
40
41 @Override
42 public Users create(String username, String password) {
43 String encryptedpassword = this.passwordEncoder.encode(password);
44 Users users = new Users(username, password);
45 return this.usersRepository.save(users);
46 }
47
48 public Users create(Users user) {
49 Users users = new Users(user.getUsername(), passwordEncoder.encode(user.getPassword()));
50 return this.usersRepository.save(users);
51 }
52
53 @Override
54 public Users login(String username, String password) {
55 if (username == null || username.isEmpty() || password == null || password.isEmpty()) {
56 throw new InvalidArgumentException();
57 }
58 return usersRepository.findByUsernameAndUpassword(username, password);
59 }
60
61}
62
Note: See TracBrowser for help on using the repository browser.