fix(clips): remove redundant Optional returns
This commit is contained in:
@@ -13,7 +13,6 @@ import org.springframework.http.ResponseEntity;
|
|||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/api/v1/clips")
|
@RequestMapping("/api/v1/clips")
|
||||||
@@ -43,12 +42,8 @@ public class ClipController {
|
|||||||
|
|
||||||
@GetMapping("/{id}")
|
@GetMapping("/{id}")
|
||||||
public ResponseEntity<APIResponse<ClipResponse>> getClipById(@PathVariable Long id) {
|
public ResponseEntity<APIResponse<ClipResponse>> getClipById(@PathVariable Long id) {
|
||||||
Optional<Clip> clip = clipService.getClipById(id);
|
Clip clip = clipService.getClipById(id);
|
||||||
if (clip.isEmpty()) {
|
ClipResponse clipDTO = convertToDTO(clip);
|
||||||
return ResponseEntity.notFound().build();
|
|
||||||
}
|
|
||||||
|
|
||||||
ClipResponse clipDTO = convertToDTO(clip.get());
|
|
||||||
|
|
||||||
return ResponseEntity.ok(
|
return ResponseEntity.ok(
|
||||||
new APIResponse<>(SUCCESS,
|
new APIResponse<>(SUCCESS,
|
||||||
|
|||||||
@@ -65,11 +65,11 @@ public class ClipService {
|
|||||||
* Returns the clip with the given ID, verifying ownership by the current user.
|
* Returns the clip with the given ID, verifying ownership by the current user.
|
||||||
*
|
*
|
||||||
* @param id the ID of the clip to retrieve
|
* @param id the ID of the clip to retrieve
|
||||||
* @return an Optional containing the clip; never empty — throws instead of returning empty
|
* @return the matching {@link Clip} entity
|
||||||
* @throws ClipNotFound if no clip with {@code id} exists
|
* @throws ClipNotFound if no clip with {@code id} exists
|
||||||
* @throws NotAuthenticated if the current user does not own the clip
|
* @throws NotAuthenticated if the current user does not own the clip
|
||||||
*/
|
*/
|
||||||
public Optional<Clip> getClipById(Long id) {
|
public Clip getClipById(Long id) {
|
||||||
Optional<Clip> clip = clipRepository.findById(id);
|
Optional<Clip> clip = clipRepository.findById(id);
|
||||||
|
|
||||||
if (clip.isEmpty()) {
|
if (clip.isEmpty()) {
|
||||||
@@ -80,7 +80,7 @@ public class ClipService {
|
|||||||
throw new NotAuthenticated("You are not authorized to access clip: " + id);
|
throw new NotAuthenticated("You are not authorized to access clip: " + id);
|
||||||
}
|
}
|
||||||
|
|
||||||
return clip;
|
return clip.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -95,13 +95,7 @@ public class ClipService {
|
|||||||
* @throws NotAuthenticated if the current user does not own the clip
|
* @throws NotAuthenticated if the current user does not own the clip
|
||||||
*/
|
*/
|
||||||
public Clip updateClip(Long id, ClipUpdateRequest newFields) {
|
public Clip updateClip(Long id, ClipUpdateRequest newFields) {
|
||||||
Optional<Clip> possibleClip = clipRepository.findById(id);
|
Clip clip = getClipById(id);
|
||||||
|
|
||||||
if (possibleClip.isEmpty()) {
|
|
||||||
throw new ClipNotFound("Clip with id " + id + " not found.");
|
|
||||||
}
|
|
||||||
|
|
||||||
Clip clip = possibleClip.get();
|
|
||||||
|
|
||||||
if (!isAuthenticatedForClip(clip)) {
|
if (!isAuthenticatedForClip(clip)) {
|
||||||
throw new NotAuthenticated("You are not authorized to access clip: " + id);
|
throw new NotAuthenticated("You are not authorized to access clip: " + id);
|
||||||
@@ -127,13 +121,8 @@ public class ClipService {
|
|||||||
* @throws NotAuthenticated if the current user does not own the clip
|
* @throws NotAuthenticated if the current user does not own the clip
|
||||||
*/
|
*/
|
||||||
public boolean deleteClip(Long id) {
|
public boolean deleteClip(Long id) {
|
||||||
Optional<Clip> possibleClip = getClipById(id);
|
Clip clip = getClipById(id);
|
||||||
if (possibleClip.isEmpty()) {
|
|
||||||
logger.warn("Clip with ID {} not found for deletion", id);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
Clip clip = possibleClip.get();
|
|
||||||
if (!isAuthenticatedForClip(clip)) {
|
if (!isAuthenticatedForClip(clip)) {
|
||||||
throw new NotAuthenticated("You are not authorized to delete this clip");
|
throw new NotAuthenticated("You are not authorized to delete this clip");
|
||||||
}
|
}
|
||||||
@@ -169,13 +158,7 @@ public class ClipService {
|
|||||||
* @throws NotAuthenticated if the current user does not own the clip
|
* @throws NotAuthenticated if the current user does not own the clip
|
||||||
*/
|
*/
|
||||||
public Resource downloadClip(Long id) {
|
public Resource downloadClip(Long id) {
|
||||||
Optional<Clip> possibleClip = getClipById(id);
|
Clip clip = getClipById(id);
|
||||||
|
|
||||||
if (possibleClip.isEmpty()) {
|
|
||||||
throw new ClipNotFound("Clip " + id + " doesn't exist");
|
|
||||||
}
|
|
||||||
|
|
||||||
Clip clip = possibleClip.get();
|
|
||||||
|
|
||||||
if (!isAuthenticatedForClip(clip)) {
|
if (!isAuthenticatedForClip(clip)) {
|
||||||
throw new NotAuthenticated("Not authenticated for this clip");
|
throw new NotAuthenticated("Not authenticated for this clip");
|
||||||
@@ -200,13 +183,7 @@ public class ClipService {
|
|||||||
* @throws NotAuthenticated if the current user does not own the clip
|
* @throws NotAuthenticated if the current user does not own the clip
|
||||||
*/
|
*/
|
||||||
public Resource downloadThumbnail(Long id) {
|
public Resource downloadThumbnail(Long id) {
|
||||||
Optional<Clip> possibleClip = getClipById(id);
|
Clip clip = getClipById(id);
|
||||||
|
|
||||||
if (possibleClip.isEmpty()) {
|
|
||||||
throw new ClipNotFound("Clip " + id + " doesn't exist");
|
|
||||||
}
|
|
||||||
|
|
||||||
Clip clip = possibleClip.get();
|
|
||||||
|
|
||||||
if (!isAuthenticatedForClip(clip)) {
|
if (!isAuthenticatedForClip(clip)) {
|
||||||
throw new NotAuthenticated("Not authenticated for this clip thumbnail");
|
throw new NotAuthenticated("Not authenticated for this clip thumbnail");
|
||||||
|
|||||||
Reference in New Issue
Block a user