REFACTOR code to use Optional entity

This commit is contained in:
2025-08-12 15:39:06 +02:00
parent 19a78df4c6
commit 6433294ced
7 changed files with 121 additions and 74 deletions

View File

@@ -10,6 +10,8 @@ import org.springframework.http.ResponseCookie;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.Optional;
@RestController
@RequestMapping("/api/v1/auth/")
public class UserController {
@@ -21,14 +23,14 @@ public class UserController {
@GetMapping("/user")
public ResponseEntity<APIResponse<User>> user() {
User user = userService.getLoggedInUser();
Optional<User> user = userService.getLoggedInUser();
if (user == null) {
if (user.isEmpty()) {
throw new NotAuthenticated("User not authenticated");
}
return ResponseEntity.ok(
new APIResponse<>("success", "User retrieved successfully", user)
new APIResponse<>("success", "User retrieved successfully", user.get())
);
}