ADD ClipController for clip retrieval
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package com.ddf.vodsystem.controllers;
|
package com.ddf.vodsystem.controllers;
|
||||||
|
|
||||||
import com.ddf.vodsystem.entities.APIResponse;
|
import com.ddf.vodsystem.entities.APIResponse;
|
||||||
|
import com.ddf.vodsystem.exceptions.NotAuthenticated;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
||||||
@@ -14,21 +15,17 @@ import java.util.Map;
|
|||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/api/v1/auth/")
|
@RequestMapping("/api/v1/auth/")
|
||||||
public class AuthController {
|
public class AuthController {
|
||||||
|
|
||||||
@GetMapping("/user")
|
@GetMapping("/user")
|
||||||
public ResponseEntity<APIResponse<Map<String, Object>>> user(@AuthenticationPrincipal OAuth2User principal) {
|
public ResponseEntity<APIResponse<Map<String, Object>>> user(@AuthenticationPrincipal OAuth2User principal) {
|
||||||
if (principal == null) {
|
if (principal == null) {
|
||||||
return ResponseEntity.status(HttpStatus.FORBIDDEN).
|
throw new NotAuthenticated("User is not authenticated");
|
||||||
body(new APIResponse<>(
|
|
||||||
"error",
|
|
||||||
"User not authenticated",
|
|
||||||
null
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (principal.getAttribute("email") == null
|
if (
|
||||||
|
principal.getAttribute("email") == null
|
||||||
|| principal.getAttribute("name") == null
|
|| principal.getAttribute("name") == null
|
||||||
|| principal.getAttribute("picture") == null) {
|
|| principal.getAttribute("picture") == null)
|
||||||
|
{
|
||||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST).
|
||||||
body(new APIResponse<>(
|
body(new APIResponse<>(
|
||||||
"error",
|
"error",
|
||||||
|
|||||||
@@ -0,0 +1,37 @@
|
|||||||
|
package com.ddf.vodsystem.controllers;
|
||||||
|
|
||||||
|
import com.ddf.vodsystem.entities.APIResponse;
|
||||||
|
import com.ddf.vodsystem.entities.Clip;
|
||||||
|
import com.ddf.vodsystem.exceptions.NotAuthenticated;
|
||||||
|
import com.ddf.vodsystem.services.ClipService;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
||||||
|
import org.springframework.security.oauth2.core.user.OAuth2User;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
@RequestMapping("/api/v1/clips")
|
||||||
|
public class ClipController {
|
||||||
|
private final ClipService clipService;
|
||||||
|
|
||||||
|
public ClipController(ClipService clipService) {
|
||||||
|
this.clipService = clipService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/")
|
||||||
|
public ResponseEntity<APIResponse<List<Clip>>> getClips(@AuthenticationPrincipal OAuth2User principal) {
|
||||||
|
if (principal == null) {
|
||||||
|
throw new NotAuthenticated("User is not authenticated");
|
||||||
|
}
|
||||||
|
|
||||||
|
List<Clip> clips = clipService.getClipsByUser();
|
||||||
|
return ResponseEntity.ok(
|
||||||
|
new APIResponse<>("success", "Clips retrieved successfully", clips)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,6 +4,7 @@ import com.ddf.vodsystem.entities.APIResponse;
|
|||||||
import com.ddf.vodsystem.exceptions.FFMPEGException;
|
import com.ddf.vodsystem.exceptions.FFMPEGException;
|
||||||
import com.ddf.vodsystem.exceptions.JobNotFinished;
|
import com.ddf.vodsystem.exceptions.JobNotFinished;
|
||||||
import com.ddf.vodsystem.exceptions.JobNotFound;
|
import com.ddf.vodsystem.exceptions.JobNotFound;
|
||||||
|
import com.ddf.vodsystem.exceptions.NotAuthenticated;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.HttpMediaTypeNotSupportedException;
|
import org.springframework.web.HttpMediaTypeNotSupportedException;
|
||||||
@@ -68,4 +69,11 @@ public class GlobalExceptionHandler {
|
|||||||
APIResponse<Void> response = new APIResponse<>(ERROR, "FFMPEG Error: Please upload a valid file", null);
|
APIResponse<Void> response = new APIResponse<>(ERROR, "FFMPEG Error: Please upload a valid file", null);
|
||||||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(NotAuthenticated.class)
|
||||||
|
public ResponseEntity<APIResponse<Void>> handleNotAuthenticated(NotAuthenticated ex) {
|
||||||
|
logger.error("NotAuthenticated: {}", ex.getMessage(), ex);
|
||||||
|
APIResponse<Void> response = new APIResponse<>(ERROR, "User is not authenticated", null);
|
||||||
|
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(response);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,8 +1,9 @@
|
|||||||
package com.ddf.vodsystem.entities;
|
package com.ddf.vodsystem.entities;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.ToString;
|
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
@@ -16,7 +17,7 @@ public class Clip {
|
|||||||
private Long id;
|
private Long id;
|
||||||
|
|
||||||
@ManyToOne(fetch = FetchType.LAZY)
|
@ManyToOne(fetch = FetchType.LAZY)
|
||||||
@ToString.Exclude
|
@JsonIgnore
|
||||||
@JoinColumn(name = "user_id", nullable = false)
|
@JoinColumn(name = "user_id", nullable = false)
|
||||||
private User user;
|
private User user;
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package com.ddf.vodsystem.exceptions;
|
||||||
|
|
||||||
|
public class NotAuthenticated extends RuntimeException {
|
||||||
|
public NotAuthenticated(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,9 +1,15 @@
|
|||||||
package com.ddf.vodsystem.repositories;
|
package com.ddf.vodsystem.repositories;
|
||||||
|
|
||||||
import com.ddf.vodsystem.entities.Clip;
|
import com.ddf.vodsystem.entities.Clip;
|
||||||
|
import com.ddf.vodsystem.entities.User;
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.data.jpa.repository.Query;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public interface ClipRepository extends JpaRepository<Clip, Long> {
|
public interface ClipRepository extends JpaRepository<Clip, Long> {
|
||||||
|
@Query("SELECT c FROM Clip c WHERE c.user = ?1")
|
||||||
|
List<Clip> findByUser(User user);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import com.ddf.vodsystem.entities.*;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import com.ddf.vodsystem.repositories.ClipRepository;
|
import com.ddf.vodsystem.repositories.ClipRepository;
|
||||||
import com.ddf.vodsystem.security.CustomOAuth2User;
|
import com.ddf.vodsystem.security.CustomOAuth2User;
|
||||||
@@ -60,6 +61,17 @@ public class ClipService {
|
|||||||
logger.info("FFMPEG finished successfully for job: {}", job.getUuid());
|
logger.info("FFMPEG finished successfully for job: {}", job.getUuid());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<Clip> getClipsByUser() {
|
||||||
|
User user = getUser();
|
||||||
|
|
||||||
|
if (user == null) {
|
||||||
|
logger.warn("No authenticated user found");
|
||||||
|
return List.of();
|
||||||
|
}
|
||||||
|
|
||||||
|
return clipRepository.findByUser(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private User getUser() {
|
private User getUser() {
|
||||||
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
|
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
|
||||||
|
|||||||
Reference in New Issue
Block a user