Moved channel URLs to channels_urls table. Create CRUD endpoints for new table.
All checks were successful
AWS Deploy on Push / build (push) Successful in 1m8s
All checks were successful
AWS Deploy on Push / build (push) Successful in 1m8s
This commit is contained in:
@@ -4,7 +4,15 @@ from typing import List
|
||||
from uuid import UUID
|
||||
from sqlalchemy import and_
|
||||
|
||||
from app.models import ChannelDB, ChannelCreate, ChannelUpdate, ChannelResponse
|
||||
from app.models import (
|
||||
ChannelDB,
|
||||
ChannelURL,
|
||||
ChannelCreate,
|
||||
ChannelUpdate,
|
||||
ChannelResponse,
|
||||
ChannelURLCreate,
|
||||
ChannelURLResponse,
|
||||
)
|
||||
from app.utils.database import get_db
|
||||
from app.auth.dependencies import get_current_user, require_roles
|
||||
from app.models.auth import CognitoUser
|
||||
@@ -36,10 +44,21 @@ def create_channel(
|
||||
detail="Channel with same group_title and name already exists"
|
||||
)
|
||||
|
||||
db_channel = ChannelDB(**channel.model_dump())
|
||||
# Create channel without URLs first
|
||||
channel_data = channel.model_dump()
|
||||
urls = channel_data.pop('urls', [])
|
||||
db_channel = ChannelDB(**channel_data)
|
||||
db.add(db_channel)
|
||||
db.commit()
|
||||
db.refresh(db_channel)
|
||||
|
||||
# Add URLs
|
||||
for url in urls:
|
||||
db_url = ChannelURL(channel_id=db_channel.id, url=url)
|
||||
db.add(db_url)
|
||||
|
||||
db.commit()
|
||||
db.refresh(db_channel)
|
||||
return db_channel
|
||||
|
||||
@router.get("/{channel_id}", response_model=ChannelResponse)
|
||||
@@ -121,4 +140,71 @@ def list_channels(
|
||||
user: CognitoUser = Depends(get_current_user)
|
||||
):
|
||||
"""List all channels with pagination"""
|
||||
return db.query(ChannelDB).offset(skip).limit(limit).all()
|
||||
return db.query(ChannelDB).offset(skip).limit(limit).all()
|
||||
|
||||
# URL Management Endpoints
|
||||
|
||||
@router.post("/{channel_id}/urls", response_model=ChannelURLResponse, status_code=status.HTTP_201_CREATED)
|
||||
@require_roles("admin")
|
||||
def add_channel_url(
|
||||
channel_id: UUID,
|
||||
url: ChannelURLCreate,
|
||||
db: Session = Depends(get_db),
|
||||
user: CognitoUser = Depends(get_current_user)
|
||||
):
|
||||
"""Add a new URL to a channel"""
|
||||
channel = db.query(ChannelDB).filter(ChannelDB.id == channel_id).first()
|
||||
if not channel:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Channel not found"
|
||||
)
|
||||
|
||||
db_url = ChannelURL(channel_id=channel_id, url=url.url)
|
||||
db.add(db_url)
|
||||
db.commit()
|
||||
db.refresh(db_url)
|
||||
return db_url
|
||||
|
||||
@router.delete("/{channel_id}/urls/{url_id}", status_code=status.HTTP_204_NO_CONTENT)
|
||||
@require_roles("admin")
|
||||
def delete_channel_url(
|
||||
channel_id: UUID,
|
||||
url_id: UUID,
|
||||
db: Session = Depends(get_db),
|
||||
user: CognitoUser = Depends(get_current_user)
|
||||
):
|
||||
"""Delete a URL from a channel"""
|
||||
url = db.query(ChannelURL).filter(
|
||||
and_(
|
||||
ChannelURL.id == url_id,
|
||||
ChannelURL.channel_id == channel_id
|
||||
)
|
||||
).first()
|
||||
|
||||
if not url:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="URL not found"
|
||||
)
|
||||
|
||||
db.delete(url)
|
||||
db.commit()
|
||||
return None
|
||||
|
||||
@router.get("/{channel_id}/urls", response_model=List[ChannelURLResponse])
|
||||
@require_roles("admin")
|
||||
def list_channel_urls(
|
||||
channel_id: UUID,
|
||||
db: Session = Depends(get_db),
|
||||
user: CognitoUser = Depends(get_current_user)
|
||||
):
|
||||
"""List all URLs for a channel"""
|
||||
channel = db.query(ChannelDB).filter(ChannelDB.id == channel_id).first()
|
||||
if not channel:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Channel not found"
|
||||
)
|
||||
|
||||
return db.query(ChannelURL).filter(ChannelURL.channel_id == channel_id).all()
|
||||
Reference in New Issue
Block a user