source: trip-planner/src/main/java/finki/diplomska/tripplanner/service/impl/UserServiceImpl.java

Last change on this file was b738035, checked in by Ema <ema_spirova@…>, 2 years ago

signup/login server errors on front and remove location from planner

  • Property mode set to 100644
File size: 1.8 KB
Line 
1package finki.diplomska.tripplanner.service.impl;
2
3import finki.diplomska.tripplanner.models.User;
4import finki.diplomska.tripplanner.models.dto.UserDto;
5import finki.diplomska.tripplanner.models.exceptions.UsernameAlreadyExistsException;
6import finki.diplomska.tripplanner.repository.jpa.JpaUserRepository;
7import finki.diplomska.tripplanner.service.UserService;
8import org.springframework.beans.factory.annotation.Autowired;
9import org.springframework.http.ResponseEntity;
10import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
11import org.springframework.stereotype.Service;
12
13import java.util.List;
14import java.util.Optional;
15
16
17@Service
18public class UserServiceImpl implements UserService {
19
20 @Autowired
21 private JpaUserRepository userRepository;
22
23 @Autowired
24 private BCryptPasswordEncoder bCryptPasswordEncoder;
25
26 public User saveUser (User newUser){
27 try{
28 newUser.setPassword(bCryptPasswordEncoder.encode(newUser.getPassword()));
29 //Username has to be unique (exception)
30 newUser.setUsername(newUser.getUsername());
31 // Make sure that password and confirmPassword match
32 // We don't persist or show the confirmPassword
33 newUser.setConfirmPassword("");
34 return this.userRepository.save(newUser);
35 }catch(Exception e){
36 throw new UsernameAlreadyExistsException("Username '"+newUser.getUsername()+ "' already exists");
37 }
38 }
39
40 @Override
41 public List<String> getAllUsernames() {
42 return this.userRepository.getAllUsernames();
43 }
44
45 @Override
46 public Optional<String> getPassword(UserDto userDto) {
47 this.userRepository.getPassword(userDto.getUsername());
48 return null;
49 }
50
51 @Override
52 public Optional<User> findById(Long id) {
53 return this.userRepository.findById(id);
54 }
55
56}
Note: See TracBrowser for help on using the repository browser.