UPDATE backend API to return JSON-like objects
This commit is contained in:
@@ -11,9 +11,9 @@ post {
|
||||
}
|
||||
|
||||
body:multipart-form {
|
||||
file: @file(/home/dylan/Documents/Test Videos/long_normal.mp4)
|
||||
file: @file(/Users/faoite/Downloads/Why Australia's Economy is Stalling.mp4)
|
||||
}
|
||||
|
||||
script:post-response {
|
||||
bru.setEnvVar("uuid", res.body);
|
||||
bru.setEnvVar("uuid", res.body.data.uuid);
|
||||
}
|
||||
|
||||
@@ -2,36 +2,46 @@ package com.ddf.vodsystem.controllers;
|
||||
|
||||
import com.ddf.vodsystem.entities.VideoMetadata;
|
||||
import com.ddf.vodsystem.services.EditService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import com.ddf.vodsystem.entities.APIResponse;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
|
||||
@RestController
|
||||
@RequestMapping("api/v1/")
|
||||
@RequestMapping("api/v1")
|
||||
public class EditController {
|
||||
private final EditService editService;
|
||||
private static final String SUCCESS = "success";
|
||||
|
||||
@Autowired
|
||||
public EditController(EditService editService) {
|
||||
this.editService = editService;
|
||||
}
|
||||
|
||||
@PostMapping("edit/{uuid}")
|
||||
public ResponseEntity<String> edit(@PathVariable("uuid") String uuid, @ModelAttribute VideoMetadata videoMetadata) {
|
||||
public ResponseEntity<APIResponse<Void>> edit(@PathVariable("uuid") String uuid, @ModelAttribute VideoMetadata videoMetadata) {
|
||||
editService.edit(uuid, videoMetadata);
|
||||
return new ResponseEntity<>(uuid, HttpStatus.OK);
|
||||
return ResponseEntity.ok(new APIResponse<>(SUCCESS, "Editing started for UUID: " + uuid, null));
|
||||
}
|
||||
|
||||
@GetMapping("/process/{uuid}")
|
||||
public ResponseEntity<String> convert(@PathVariable("uuid") String uuid) {
|
||||
public ResponseEntity<APIResponse<Void>> convert(@PathVariable("uuid") String uuid) {
|
||||
editService.process(uuid);
|
||||
return new ResponseEntity<>(uuid, HttpStatus.OK);
|
||||
return ResponseEntity.ok(new APIResponse<>(SUCCESS, "Processing started for UUID: " + uuid, null));
|
||||
}
|
||||
|
||||
@GetMapping("/progress/{uuid}")
|
||||
public ResponseEntity<Float> getProgress(@PathVariable("uuid") String uuid) {
|
||||
return new ResponseEntity<>(editService.getProgress(uuid), HttpStatus.OK);
|
||||
public ResponseEntity<APIResponse<ProgressResponse>> getProgress(@PathVariable("uuid") String uuid) {
|
||||
float progress = editService.getProgress(uuid);
|
||||
|
||||
ProgressResponse progressResponse = new ProgressResponse(progress);
|
||||
return ResponseEntity.ok(new APIResponse<>(SUCCESS, "Progress for UUID: " + uuid, progressResponse));
|
||||
}
|
||||
|
||||
}
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public static class ProgressResponse {
|
||||
private float progress;
|
||||
}
|
||||
}
|
||||
@@ -1,45 +1,63 @@
|
||||
package com.ddf.vodsystem.controllers;
|
||||
|
||||
import com.ddf.vodsystem.entities.APIResponse;
|
||||
import com.ddf.vodsystem.exceptions.JobNotFinished;
|
||||
import com.ddf.vodsystem.exceptions.JobNotFound;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.HttpMediaTypeNotSupportedException;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
import org.springframework.web.multipart.MultipartException;
|
||||
import org.springframework.web.multipart.support.MissingServletRequestPartException;
|
||||
|
||||
@ControllerAdvice
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@RestControllerAdvice
|
||||
public class GlobalExceptionHandler {
|
||||
private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
|
||||
private static final String ERROR = "error";
|
||||
|
||||
@ExceptionHandler({ MultipartException.class })
|
||||
public ResponseEntity<String> handleMultipartException(MultipartException ex) {
|
||||
return ResponseEntity.badRequest().body("Request is not multipart/form-data.");
|
||||
public ResponseEntity<APIResponse<Void>> handleMultipartException(MultipartException ex) {
|
||||
logger.error("MultipartException: {}", ex.getMessage(), ex);
|
||||
APIResponse<Void> response = new APIResponse<>(ERROR, "Multipart request error: " + ex.getMessage(), null);
|
||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response);
|
||||
}
|
||||
|
||||
@ExceptionHandler({ MissingServletRequestPartException.class })
|
||||
public ResponseEntity<String> handleMissingPart(MissingServletRequestPartException ex) {
|
||||
return ResponseEntity.badRequest().body("Required file part is missing.");
|
||||
public ResponseEntity<APIResponse<Void>> handleMissingPart(MissingServletRequestPartException ex) {
|
||||
logger.error("MissingServletRequestPartException: {}", ex.getMessage(), ex);
|
||||
APIResponse<Void> response = new APIResponse<>(ERROR, "Required file part is missing.", null);
|
||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response);
|
||||
}
|
||||
|
||||
@ExceptionHandler({ HttpMediaTypeNotSupportedException.class })
|
||||
public ResponseEntity<String> handleUnsupportedMediaType(HttpMediaTypeNotSupportedException ex) {
|
||||
return ResponseEntity.status(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
|
||||
.body("Unsupported media type: expected multipart/form-data.");
|
||||
public ResponseEntity<APIResponse<Void>> handleUnsupportedMediaType(HttpMediaTypeNotSupportedException ex) {
|
||||
logger.error("HttpMediaTypeNotSupportedException: {}", ex.getMessage(), ex);
|
||||
APIResponse<Void> response = new APIResponse<>(ERROR, "Unsupported media type: expected multipart/form-data.", null);
|
||||
return ResponseEntity.status(HttpStatus.UNSUPPORTED_MEDIA_TYPE).body(response);
|
||||
}
|
||||
|
||||
@ExceptionHandler(IllegalArgumentException.class)
|
||||
public ResponseEntity<String> handleIllegalArgument(IllegalArgumentException ex) {
|
||||
return ResponseEntity.status(400).body(ex.getMessage());
|
||||
public ResponseEntity<APIResponse<Void>> handleIllegalArgument(IllegalArgumentException ex) {
|
||||
logger.error("IllegalArgumentException: {}", ex.getMessage(), ex);
|
||||
APIResponse<Void> response = new APIResponse<>(ERROR, ex.getMessage(), null);
|
||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response);
|
||||
}
|
||||
|
||||
@ExceptionHandler(JobNotFound.class)
|
||||
public ResponseEntity<String> handleFileNotFound(JobNotFound ex) {
|
||||
return ResponseEntity.status(404).body(ex.getMessage());
|
||||
public ResponseEntity<APIResponse<Void>> handleFileNotFound(JobNotFound ex) {
|
||||
logger.error("JobNotFound: {}", ex.getMessage(), ex);
|
||||
APIResponse<Void> response = new APIResponse<>(ERROR, ex.getMessage(), null);
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
|
||||
}
|
||||
|
||||
@ExceptionHandler(JobNotFinished.class)
|
||||
public ResponseEntity<String> handleJobNotFinished(JobNotFinished ex) {
|
||||
return ResponseEntity.status(202).body(ex.getMessage());
|
||||
public ResponseEntity<APIResponse<Void>> handleJobNotFinished(JobNotFinished ex) {
|
||||
logger.error("JobNotFinished: {}", ex.getMessage(), ex);
|
||||
APIResponse<Void> response = new APIResponse<>(ERROR, ex.getMessage(), null);
|
||||
return ResponseEntity.status(HttpStatus.ACCEPTED).body(response);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.ddf.vodsystem.controllers;
|
||||
|
||||
import com.ddf.vodsystem.entities.VideoMetadata;
|
||||
import com.ddf.vodsystem.entities.APIResponse;
|
||||
import com.ddf.vodsystem.services.JobService;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
@@ -19,26 +20,28 @@ public class MetadataController {
|
||||
}
|
||||
|
||||
@GetMapping("/original/{uuid}")
|
||||
public ResponseEntity<VideoMetadata> getMetadata(@PathVariable String uuid) {
|
||||
public ResponseEntity<APIResponse<VideoMetadata>> getMetadata(@PathVariable String uuid) {
|
||||
VideoMetadata originalMetadata = jobService.getJob(uuid).getInputVideoMetadata();
|
||||
|
||||
if (originalMetadata == null) {
|
||||
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND)
|
||||
.body(new APIResponse<>("error", "Original metadata not found", null));
|
||||
}
|
||||
|
||||
return ResponseEntity.ok()
|
||||
.body(originalMetadata);
|
||||
.body(new APIResponse<>("success", "Original metadata retrieved", originalMetadata));
|
||||
}
|
||||
|
||||
@GetMapping("/converted/{uuid}")
|
||||
public ResponseEntity<VideoMetadata> getConvertedMetadata(@PathVariable String uuid) {
|
||||
public ResponseEntity<APIResponse<VideoMetadata>> getConvertedMetadata(@PathVariable String uuid) {
|
||||
VideoMetadata convertedMetadata = jobService.getJob(uuid).getOutputVideoMetadata();
|
||||
|
||||
if (convertedMetadata == null) {
|
||||
return ResponseEntity.notFound().build();
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND)
|
||||
.body(new APIResponse<>("error", "Converted metadata not found", null));
|
||||
}
|
||||
|
||||
return ResponseEntity.ok()
|
||||
.body(convertedMetadata);
|
||||
.body(new APIResponse<>("success", "Converted metadata retrieved", convertedMetadata));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.ddf.vodsystem.controllers;
|
||||
|
||||
import com.ddf.vodsystem.entities.APIResponse;
|
||||
import com.ddf.vodsystem.services.UploadService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
@@ -19,12 +20,13 @@ public class UploadController {
|
||||
}
|
||||
|
||||
@PostMapping()
|
||||
public ResponseEntity<String> uploadVideo(@RequestParam("file") MultipartFile file) {
|
||||
public ResponseEntity<APIResponse<Object>> uploadVideo(@RequestParam("file") MultipartFile file) {
|
||||
if (file == null || file.isEmpty()) {
|
||||
throw new IllegalArgumentException("Invalid file");
|
||||
}
|
||||
|
||||
String uuid = uploadService.upload(file);
|
||||
return new ResponseEntity<>(uuid, HttpStatus.OK);
|
||||
APIResponse<Object> response = new APIResponse<>("success", "File uploaded successfully", java.util.Collections.singletonMap("uuid", uuid));
|
||||
return new ResponseEntity<>(response, HttpStatus.OK);
|
||||
}
|
||||
}
|
||||
}
|
||||
16
src/main/java/com/ddf/vodsystem/entities/APIResponse.java
Normal file
16
src/main/java/com/ddf/vodsystem/entities/APIResponse.java
Normal file
@@ -0,0 +1,16 @@
|
||||
package com.ddf.vodsystem.entities;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class APIResponse<T> {
|
||||
private String status;
|
||||
private String message;
|
||||
private T data;
|
||||
|
||||
public APIResponse(String status, String message, T data) {
|
||||
this.status = status;
|
||||
this.message = message;
|
||||
this.data = data;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user