VideoController: - Add /all & /{id} paths; VideoService: - Add getVideoById, throws exception on service layer.; - Add getVideos, returns all videos from DB

This commit is contained in:
Reeverflow
2026-02-15 16:58:36 +01:00
parent df838920b6
commit cbed4362a8
5 changed files with 37 additions and 23 deletions

1
.gitignore vendored
View File

@@ -31,3 +31,4 @@ build/
### VS Code ###
.vscode/
After this video, you will speak with confidence \[3yMHLJbXfFc].webm

View File

@@ -2,11 +2,16 @@ package com.example.video_downloader.controllers;
import com.example.video_downloader.dto.SaveNewVideoRequest;
import com.example.video_downloader.entity.Video;
import com.example.video_downloader.repositories.VideoRepository;
import com.example.video_downloader.services.VideoService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;
import java.util.List;
import java.util.Optional;
@RestController
@RequestMapping(path="/video")
@@ -31,4 +36,15 @@ public class VideoController {
return response;
}
@GetMapping(path = "/all")
public ResponseEntity<List<Video>> getAllVideos(){
return ResponseEntity.ok(videoService.getVideos());
}
@GetMapping(path = "/{id}")
public ResponseEntity<Video> getVideoById(@PathVariable Long id){
return ResponseEntity.ok(videoService.getVideoById(id));
}
}

View File

@@ -11,18 +11,15 @@ public class ProcessService {
@PostConstruct
public void init() throws InterruptedException, IOException {
System.out.println("Init...");
execute("yt-dlp", "");
}
ProcessBuilder builder = new ProcessBuilder("/app/yt-dlp");
public void execute(String... command) throws InterruptedException, IOException {
ProcessBuilder builder = new ProcessBuilder(command);
builder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
builder.redirectError(ProcessBuilder.Redirect.INHERIT);
Process process = builder.start();
process.waitFor();
String text = new String(process.getInputStream().readAllBytes(), StandardCharsets.UTF_8);
String text2 = new String(process.getErrorStream().readAllBytes(), StandardCharsets.UTF_8);
System.out.println(text);
System.out.println(text2);
System.out.println("Marker...");
}

View File

@@ -7,6 +7,9 @@ import com.example.video_downloader.repositories.VideoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
public class VideoService {
@@ -22,4 +25,14 @@ public class VideoService {
return video;
}
public List<Video> getVideos(){
return videoRepository.findAll();
}
public Video getVideoById(Long id){
return videoRepository.findById(id)
.orElseThrow(() -> new RuntimeException("Video not found with id: " + id));
}
}

View File

@@ -1,13 +0,0 @@
package com.example.video_downloader;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class VideoDownloaderApplicationTests {
@Test
void contextLoads() {
}
}