Introduced groups and added all related endpoints
All checks were successful
AWS Deploy on Push / build (push) Successful in 7m39s
All checks were successful
AWS Deploy on Push / build (push) Successful in 7m39s
This commit is contained in:
@@ -13,6 +13,7 @@ from app.models import (
|
||||
ChannelURL,
|
||||
ChannelURLCreate,
|
||||
ChannelURLResponse,
|
||||
Group,
|
||||
)
|
||||
from app.models.auth import CognitoUser
|
||||
from app.models.schemas import ChannelURLUpdate
|
||||
@@ -29,12 +30,20 @@ def create_channel(
|
||||
user: CognitoUser = Depends(get_current_user),
|
||||
):
|
||||
"""Create a new channel"""
|
||||
# Check for duplicate channel (same group_title + name)
|
||||
# Check if group exists
|
||||
group = db.query(Group).filter(Group.id == channel.group_id).first()
|
||||
if not group:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Group not found",
|
||||
)
|
||||
|
||||
# Check for duplicate channel (same group_id + name)
|
||||
existing_channel = (
|
||||
db.query(ChannelDB)
|
||||
.filter(
|
||||
and_(
|
||||
ChannelDB.group_title == channel.group_title,
|
||||
ChannelDB.group_id == channel.group_id,
|
||||
ChannelDB.name == channel.name,
|
||||
)
|
||||
)
|
||||
@@ -44,7 +53,7 @@ def create_channel(
|
||||
if existing_channel:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_409_CONFLICT,
|
||||
detail="Channel with same group_title and name already exists",
|
||||
detail="Channel with same group_id and name already exists",
|
||||
)
|
||||
|
||||
# Create channel without URLs first
|
||||
@@ -96,20 +105,27 @@ def update_channel(
|
||||
status_code=status.HTTP_404_NOT_FOUND, detail="Channel not found"
|
||||
)
|
||||
|
||||
# Only check for duplicates if name or group_title are being updated
|
||||
if channel.name is not None or channel.group_title is not None:
|
||||
# Only check for duplicates if name or group_id are being updated
|
||||
if channel.name is not None or channel.group_id is not None:
|
||||
name = channel.name if channel.name is not None else db_channel.name
|
||||
group_title = (
|
||||
channel.group_title
|
||||
if channel.group_title is not None
|
||||
else db_channel.group_title
|
||||
group_id = (
|
||||
channel.group_id if channel.group_id is not None else db_channel.group_id
|
||||
)
|
||||
|
||||
# Check if new group exists
|
||||
if channel.group_id is not None:
|
||||
group = db.query(Group).filter(Group.id == channel.group_id).first()
|
||||
if not group:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Group not found",
|
||||
)
|
||||
|
||||
existing_channel = (
|
||||
db.query(ChannelDB)
|
||||
.filter(
|
||||
and_(
|
||||
ChannelDB.group_title == group_title,
|
||||
ChannelDB.group_id == group_id,
|
||||
ChannelDB.name == name,
|
||||
ChannelDB.id != channel_id,
|
||||
)
|
||||
@@ -120,7 +136,7 @@ def update_channel(
|
||||
if existing_channel:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_409_CONFLICT,
|
||||
detail="Channel with same group_title and name already exists",
|
||||
detail="Channel with same group_id and name already exists",
|
||||
)
|
||||
|
||||
# Update only provided fields
|
||||
@@ -163,9 +179,69 @@ def list_channels(
|
||||
return db.query(ChannelDB).offset(skip).limit(limit).all()
|
||||
|
||||
|
||||
# New endpoint to get channels by group
|
||||
@router.get("/groups/{group_id}/channels", response_model=list[ChannelResponse])
|
||||
def get_channels_by_group(
|
||||
group_id: UUID,
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
"""Get all channels for a specific group"""
|
||||
group = db.query(Group).filter(Group.id == group_id).first()
|
||||
if not group:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND, detail="Group not found"
|
||||
)
|
||||
return db.query(ChannelDB).filter(ChannelDB.group_id == group_id).all()
|
||||
|
||||
|
||||
# New endpoint to update a channel's group
|
||||
@router.put("/{channel_id}/group", response_model=ChannelResponse)
|
||||
@require_roles("admin")
|
||||
def update_channel_group(
|
||||
channel_id: UUID,
|
||||
group_id: UUID,
|
||||
db: Session = Depends(get_db),
|
||||
user: CognitoUser = Depends(get_current_user),
|
||||
):
|
||||
"""Update a channel's group"""
|
||||
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"
|
||||
)
|
||||
|
||||
group = db.query(Group).filter(Group.id == group_id).first()
|
||||
if not group:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND, detail="Group not found"
|
||||
)
|
||||
|
||||
# Check for duplicate channel name in new group
|
||||
existing_channel = (
|
||||
db.query(ChannelDB)
|
||||
.filter(
|
||||
and_(
|
||||
ChannelDB.group_id == group_id,
|
||||
ChannelDB.name == channel.name,
|
||||
ChannelDB.id != channel_id,
|
||||
)
|
||||
)
|
||||
.first()
|
||||
)
|
||||
|
||||
if existing_channel:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_409_CONFLICT,
|
||||
detail="Channel with same name already exists in target group",
|
||||
)
|
||||
|
||||
channel.group_id = group_id
|
||||
db.commit()
|
||||
db.refresh(channel)
|
||||
return channel
|
||||
|
||||
|
||||
# URL Management Endpoints
|
||||
|
||||
|
||||
@router.post(
|
||||
"/{channel_id}/urls",
|
||||
response_model=ChannelURLResponse,
|
||||
|
||||
Reference in New Issue
Block a user