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); } }