chore(prod): sync with main #4
@@ -2,6 +2,7 @@ package com.ddf.vodsystem.controllers;
|
|||||||
|
|
||||||
import com.ddf.vodsystem.dto.ClipDTO;
|
import com.ddf.vodsystem.dto.ClipDTO;
|
||||||
import com.ddf.vodsystem.dto.APIResponse;
|
import com.ddf.vodsystem.dto.APIResponse;
|
||||||
|
import com.ddf.vodsystem.dto.ClipUpdateRequest;
|
||||||
import com.ddf.vodsystem.entities.Clip;
|
import com.ddf.vodsystem.entities.Clip;
|
||||||
import com.ddf.vodsystem.services.ClipService;
|
import com.ddf.vodsystem.services.ClipService;
|
||||||
import org.springframework.http.ResponseEntity;
|
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}")
|
@DeleteMapping("/{id}")
|
||||||
public ResponseEntity<APIResponse<String>> deleteClip(@PathVariable Long id) {
|
public ResponseEntity<APIResponse<String>> deleteClip(@PathVariable Long id) {
|
||||||
if (!clipService.deleteClip(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;
|
package com.ddf.vodsystem.services;
|
||||||
|
|
||||||
import com.ddf.vodsystem.dto.ClipOptions;
|
import com.ddf.vodsystem.dto.ClipOptions;
|
||||||
|
import com.ddf.vodsystem.dto.ClipUpdateRequest;
|
||||||
import com.ddf.vodsystem.entities.*;
|
import com.ddf.vodsystem.entities.*;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
@@ -11,6 +12,7 @@ import java.util.List;
|
|||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
|
|
||||||
|
import com.ddf.vodsystem.exceptions.ClipNotFound;
|
||||||
import com.ddf.vodsystem.exceptions.FFMPEGException;
|
import com.ddf.vodsystem.exceptions.FFMPEGException;
|
||||||
import com.ddf.vodsystem.exceptions.NotAuthenticated;
|
import com.ddf.vodsystem.exceptions.NotAuthenticated;
|
||||||
import com.ddf.vodsystem.exceptions.StorageException;
|
import com.ddf.vodsystem.exceptions.StorageException;
|
||||||
@@ -70,18 +72,40 @@ public class ClipService {
|
|||||||
Optional<Clip> clip = clipRepository.findById(id);
|
Optional<Clip> clip = clipRepository.findById(id);
|
||||||
|
|
||||||
if (clip.isEmpty()) {
|
if (clip.isEmpty()) {
|
||||||
logger.warn("Clip with ID {} not found", id);
|
throw new ClipNotFound("Clip with id " + id + " not found.");
|
||||||
return clip;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isAuthenticatedForClip(clip.get())) {
|
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 clip: " + id);
|
||||||
throw new NotAuthenticated("You are not authorized to access this clip");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return clip;
|
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.
|
* Deletes a clip by its ID, ensuring the user is authenticated to perform the deletion.
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user