REFACTOR code to use Optional entity
This commit is contained in:
@@ -11,6 +11,7 @@ import org.springframework.security.oauth2.core.user.OAuth2User;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/clips")
|
||||
@@ -35,12 +36,12 @@ public class ClipController {
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public ResponseEntity<APIResponse<ClipDTO>> getClipById(@PathVariable Long id) {
|
||||
Clip clip = clipService.getClipById(id);
|
||||
if (clip == null) {
|
||||
Optional<Clip> clip = clipService.getClipById(id);
|
||||
if (clip.isEmpty()) {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
ClipDTO clipDTO = convertToDTO(clip);
|
||||
ClipDTO clipDTO = convertToDTO(clip.get());
|
||||
|
||||
return ResponseEntity.ok(
|
||||
new APIResponse<>("success", "Clip retrieved successfully", clipDTO)
|
||||
|
||||
@@ -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())
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user