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.
42 lines
1.5 KiB
42 lines
1.5 KiB
package org.example.services;
|
|
|
|
import org.example.models.User;
|
|
import org.example.repositories.PostRepository;
|
|
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;
|
|
private final PostRepository postRepository;
|
|
|
|
public UserService(UserRepository userRepository, PasswordEncoder passwordEncoder, PostRepository postRepository) {
|
|
this.userRepository = userRepository;
|
|
this.passwordEncoder = passwordEncoder;
|
|
this.postRepository = postRepository;
|
|
}
|
|
|
|
public User createUser(CreateUserRequest request) {
|
|
if (userRepository.existsByUsername(request.getUsername())) {
|
|
throw new IllegalArgumentException("Username already exists. Please choose a different username.");
|
|
}
|
|
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;
|
|
}
|
|
public void addFriend(String username1, String username2) {
|
|
userRepository.addFriend(username1,username2);
|
|
}
|
|
}
|
|
|