This commit is contained in:
Marek
2026-04-05 14:54:10 +02:00
parent 7d66746969
commit 6bbadb69c7
11 changed files with 129 additions and 147 deletions

View File

@@ -12,6 +12,6 @@ class Video(Base):
title = Column(String, nullable=False)
youtuber = Column(String, nullable=False)
thumbnail_url = Column(String, nullable=False)
youtube_url = Column(String, nullable=False, unique=True)
youtube_url = Column(String, nullable=False)
file_path = Column(String, nullable=True)
created_at = Column(DateTime, default=datetime.utcnow)

View File

@@ -13,10 +13,16 @@ from services.download_service import download_video, stream_video_live
router = APIRouter(prefix="/videos", tags=["videos"])
@router.post("", response_model=VideoResponse)
def create_video(video_data: VideoCreate, db: Session = Depends(get_db)):
video = video_service.create_video(db, video_data)
return VideoResponse.from_model(video)
@router.post("", response_model=list[VideoResponse])
def create_videos(videos_data: list[VideoCreate], db: Session = Depends(get_db)):
created_ids = []
for video_data in reversed(videos_data):
video_id_match = video_data.youtube_url.split("v=")[-1].split("&")[0]
video_service.delete_by_youtube_id(db, video_id_match)
video = video_service.create_video(db, video_data)
created_ids.append(video.id)
videos = [video_service.get_video(db, vid) for vid in created_ids]
return [VideoResponse.from_model(v) for v in videos if v]
@router.get("", response_model=list[VideoResponse])

View File

@@ -13,17 +13,22 @@ def create_video(db: Session, video_data: VideoCreate) -> Video:
def get_all_videos(db: Session) -> list[Video]:
return db.query(Video).order_by(Video.created_at.desc()).all()
return db.query(Video).order_by(Video.id.desc()).all()
def get_downloaded_videos(db: Session) -> list[Video]:
return db.query(Video).filter(Video.file_path.isnot(None)).order_by(Video.created_at.desc()).all()
return db.query(Video).filter(Video.file_path.isnot(None)).order_by(Video.id.desc()).all()
def get_video(db: Session, video_id: int) -> Video | None:
return db.query(Video).filter(Video.id == video_id).first()
def delete_by_youtube_id(db: Session, youtube_id: str):
db.query(Video).filter(Video.youtube_url.contains(youtube_id)).delete(synchronize_session=False)
db.commit()
def update_file_path(db: Session, video_id: int, path: str):
video = get_video(db, video_id)
if video: