You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
1.0 KiB
34 lines
1.0 KiB
10 months ago
|
package org.example.services;
|
||
|
|
||
|
import org.example.models.User;
|
||
|
import org.example.repositories.UserRepository;
|
||
|
import org.example.requests.CreateUserRequest;
|
||
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||
|
import org.springframework.stereotype.Service;
|
||
|
|
||
|
@Service
|
||
|
public class UserService {
|
||
|
private final UserRepository userRepository;
|
||
|
|
||
|
private final PasswordEncoder passwordEncoder;
|
||
|
|
||
|
public UserService(UserRepository userRepository, PasswordEncoder passwordEncoder) {
|
||
|
this.userRepository = userRepository;
|
||
|
this.passwordEncoder = passwordEncoder;
|
||
|
}
|
||
|
|
||
|
public User createUser(CreateUserRequest request) {
|
||
|
User user = new User();
|
||
|
|
||
|
user.setName(request.getName());
|
||
|
// TODO: make sure that this username doesn't exist.
|
||
|
user.setUsername(request.getUsername());
|
||
|
user.setRoles(request.getRoles());
|
||
|
user.setPassword(passwordEncoder.encode(request.getPassword()));
|
||
|
|
||
|
userRepository.save(user);
|
||
|
|
||
|
return user;
|
||
|
}
|
||
|
}
|