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

Last change on this file since 1ad8e64 was 1ad8e64, checked in by Ema <ema_spirova@…>, 3 years ago

spring security

  • Property mode set to 100644
File size: 1.3 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.security.crypto.bcrypt.BCryptPasswordEncoder;
10import org.springframework.stereotype.Service;
11
12
13@Service
14public class UserServiceImpl implements UserService {
15
16 @Autowired
17 private JpaUserRepository userRepository;
18
19 @Autowired
20 private BCryptPasswordEncoder bCryptPasswordEncoder;
21
22 public User saveUser (User newUser){
23 try{
24 newUser.setPassword(bCryptPasswordEncoder.encode(newUser.getPassword()));
25 //Username has to be unique (exception)
26 newUser.setUsername(newUser.getUsername());
27 // Make sure that password and confirmPassword match
28 // We don't persist or show the confirmPassword
29 newUser.setConfirmPassword("");
30 return this.userRepository.save(newUser);
31 }catch(Exception e){
32 throw new UsernameAlreadyExistsException("Username '"+newUser.getUsername()+ "' already exists");
33 }
34
35 }
36
37}
Note: See TracBrowser for help on using the repository browser.