import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private UserService userService;
// ✅ Create User
@PostMapping
public User addUser(@RequestBody User user) {
return userService.saveUser(user);
}
// ✅ Get All Users
@GetMapping
public List getUsers() {
return userService.getAllUsers();
}
// ✅ Get Userby id
@GetMapping("/{id}")
public User getUser(@PathVariable Long id) {
return userService.getUserById(id);
}
// ✅ Update User
@PutMapping
public User updateUser(@RequestBody User user) {
return userService.updateUser(user);
}
// ✅ Delete User By id
@DeleteMapping("/{id}")
public String deleteUser(@PathVariable Long id) {
return userService.deleteUser(id);
}
// ✅ Create User connect with existing profile
@PutMapping("/existingProfile/{profileId}")
public User updateUserWithExistingProfile(@PathVariable Long profileId, @RequestBody User user) {
return userService.createUserWithExistingProfile(profileId, user);
}
// ✅ Update User connect with existing profile
@PutMapping("/existingUserProfile/{profileId}/{userId}")
public User updateUserProfile(@PathVariable Long profileId, @PathVariable Long userId) {
return userService.updateExistingUserProfile(profileId, userId);
}
// ✅ Update profile connected with user
@PutMapping("/{userId}/profile/update-name")
public User updateProfileName(@PathVariable Long userId, @RequestParam String newProfileName) {
return userService.updateUserProfileName(userId, newProfileName);
}
// ✅ Delete profile connected with user set null in profile_id column
@DeleteMapping("/{userId}/profile")
public User removeProfile(@PathVariable Long userId) {
return userService.removeUserProfile(userId);
}
}
UserService.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
@Autowired
private ProfileRepository profileRepository;
// ✅ Create User
public User saveUser(User user) {
return userRepository.save(user);
}
// ✅ Get All Users
public List getAllUsers() {
return userRepository.findAll();
}
// ✅ Get User by id
public User getUserById(Long id) {
return userRepository.findById(id).orElse(null);
}
// ✅ Update User
public User updateUser(User user) {
return userRepository.save(user);
}
// ✅ Delete User By id
public String deleteUser(Long id) {
userRepository.deleteById(id);
return "User deleted!";
}
// ✅ Create User connect with existing profile
public User createUserWithExistingProfile(Long profileId, User user) {
if (profileId != null) {
Optional existingProfile = profileRepository.findById(profileId);
existingProfile.ifPresent(user::setProfile);
}
return userRepository.save(user);
}
// ✅ Update User connect with existing profile
public User updateExistingUserProfile(Long profileId, Long userId) {
if (profileId != null && userId != null) {
Optional existingProfile = profileRepository.findById(profileId);
Optional existingUser = userRepository.findById(userId);
if (existingUser.isPresent() && existingProfile.isPresent()) {
User user = existingUser.get();
user.setProfile(existingProfile.get());
return userRepository.save(user);
}
}
return null;
}
// ✅ Update profile connected with user
public User updateUserProfileName(Long userId, String newProfileName) {
Optional existingUser = userRepository.findById(userId);
if (existingUser.isPresent()) {
User user = existingUser.get();
Profile profile = user.getProfile();
if (profile != null) {
profile.setBio(newProfileName); // Update profile name (bio)
profileRepository.save(profile); // Save updated profile
return user;
} else {
throw new RuntimeException("Profile not found for User ID: " + userId);
}
} else {
throw new RuntimeException("User not found with ID: " + userId);
}
}
// ✅ Delete profile connected with user set null in profile_id column
public User removeUserProfile(Long userId) {
Optional existingUser = userRepository.findById(userId);
if (existingUser.isPresent()) {
User user = existingUser.get();
Profile profile = user.getProfile();
if (profile != null) {
user.setProfile(null); // Remove profile from user
userRepository.save(user); // Save updated user (profile detached)
return user;
} else {
throw new RuntimeException("Profile not found for User ID: " + userId);
}
} else {
throw new RuntimeException("User not found with ID: " + userId);
}
}
}
UserRepository.java
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository {
}
Testing the REST API with Postman:
Once the application is running, test the endpoints:
Create User With Profile http://localhost:8080/users
Get All Users With Profile http://localhost:8080/users
Get Users With Profile by user id http://localhost:8080/users/{userID}
Update Users http://localhost:8080/users
Delete Users http://localhost:8080/users/{userID}
Create Users with existing profile id http://localhost:8080/users/existingProfile/{profileID}
Update Existing Users With Existing Profile http://localhost:8080/users/existingUserProfile/{profileID}/{userID}
Update Profile by Using User id http://localhost:8080/users/{userID}/profile/update-name?newProfileName=Mobile Developer
Delete Profile by Using User id http://localhost:8080/users/{userID}/profile
We use cookies and similar technologies to enhance your browsing experience, serve personalized content and advertisements, and analyze our traffic.
By clicking "Accept All", you consent to our use of cookies and the processing of your personal data for these purposes.
You can manage your preferences or learn more in our
Privacy Policy and
Cookie Policy.