feat(clips): add edit functionality for title & description
This commit is contained in:
@@ -2,6 +2,7 @@ package com.ddf.vodsystem.controllers;
|
||||
|
||||
import com.ddf.vodsystem.dto.ClipDTO;
|
||||
import com.ddf.vodsystem.dto.APIResponse;
|
||||
import com.ddf.vodsystem.dto.ClipUpdateRequest;
|
||||
import com.ddf.vodsystem.entities.Clip;
|
||||
import com.ddf.vodsystem.services.ClipService;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
@@ -52,6 +53,17 @@ public class ClipController {
|
||||
);
|
||||
}
|
||||
|
||||
@PatchMapping("/{id}")
|
||||
public ResponseEntity<APIResponse<ClipDTO>> updateClip(@PathVariable Long id,
|
||||
@RequestBody ClipUpdateRequest updateFields) {
|
||||
Clip clip = clipService.updateClip(id, updateFields);
|
||||
ClipDTO clipDTO = convertToDTO(clip);
|
||||
|
||||
return ResponseEntity.ok(
|
||||
new APIResponse<>(SUCCESS, "Clip updated successfully", clipDTO)
|
||||
);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public ResponseEntity<APIResponse<String>> deleteClip(@PathVariable Long id) {
|
||||
if (!clipService.deleteClip(id)) {
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.ddf.vodsystem.dto;
|
||||
|
||||
public record ClipUpdateRequest (
|
||||
String title,
|
||||
String description
|
||||
) {}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.ddf.vodsystem.exceptions;
|
||||
|
||||
public class ClipNotFound extends RuntimeException {
|
||||
public ClipNotFound(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.ddf.vodsystem.services;
|
||||
|
||||
import com.ddf.vodsystem.dto.ClipOptions;
|
||||
import com.ddf.vodsystem.dto.ClipUpdateRequest;
|
||||
import com.ddf.vodsystem.entities.*;
|
||||
|
||||
import java.io.File;
|
||||
@@ -11,6 +12,7 @@ import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
import com.ddf.vodsystem.exceptions.ClipNotFound;
|
||||
import com.ddf.vodsystem.exceptions.FFMPEGException;
|
||||
import com.ddf.vodsystem.exceptions.NotAuthenticated;
|
||||
import com.ddf.vodsystem.exceptions.StorageException;
|
||||
@@ -70,18 +72,40 @@ public class ClipService {
|
||||
Optional<Clip> clip = clipRepository.findById(id);
|
||||
|
||||
if (clip.isEmpty()) {
|
||||
logger.warn("Clip with ID {} not found", id);
|
||||
return clip;
|
||||
throw new ClipNotFound("Clip with id " + id + " not found.");
|
||||
}
|
||||
|
||||
if (!isAuthenticatedForClip(clip.get())) {
|
||||
logger.warn("User is not authorized to access clip with ID {}", id);
|
||||
throw new NotAuthenticated("You are not authorized to access this clip");
|
||||
throw new NotAuthenticated("You are not authorized to access clip: " + id);
|
||||
}
|
||||
|
||||
return clip;
|
||||
}
|
||||
|
||||
public Clip updateClip(Long id, ClipUpdateRequest newFields) {
|
||||
Optional<Clip> possibleClip = clipRepository.findById(id);
|
||||
|
||||
if (possibleClip.isEmpty()) {
|
||||
throw new ClipNotFound("Clip with id " + id + " not found.");
|
||||
}
|
||||
|
||||
Clip clip = possibleClip.get();
|
||||
|
||||
if (!isAuthenticatedForClip(clip)) {
|
||||
throw new NotAuthenticated("You are not authorized to access clip: " + id);
|
||||
}
|
||||
|
||||
if (newFields.title() != null) {
|
||||
clip.setTitle(newFields.title());
|
||||
}
|
||||
|
||||
if (newFields.description() != null) {
|
||||
clip.setDescription(newFields.description());
|
||||
}
|
||||
|
||||
return clipRepository.saveAndFlush(clip);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a clip by its ID, ensuring the user is authenticated to perform the deletion.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user