Refactored DB schema. Channels table to have uuid as primary key. Modified endpoints accordingly
Some checks failed
AWS Deploy on Push / build (push) Failing after 1m9s
Some checks failed
AWS Deploy on Push / build (push) Failing after 1m9s
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from sqlalchemy.orm import Session
|
||||
from typing import List
|
||||
from uuid import UUID
|
||||
from sqlalchemy import and_
|
||||
|
||||
from app.models import ChannelDB, ChannelCreate, ChannelResponse
|
||||
from app.models import ChannelDB, ChannelCreate, ChannelUpdate, ChannelResponse
|
||||
from app.utils.database import get_db
|
||||
from app.auth.dependencies import get_current_user, require_roles
|
||||
from app.models.auth import CognitoUser
|
||||
@@ -20,19 +22,33 @@ def create_channel(
|
||||
user: CognitoUser = Depends(get_current_user)
|
||||
):
|
||||
"""Create a new channel"""
|
||||
# Check for duplicate channel (same group_title + name)
|
||||
existing_channel = db.query(ChannelDB).filter(
|
||||
and_(
|
||||
ChannelDB.group_title == channel.group_title,
|
||||
ChannelDB.name == channel.name
|
||||
)
|
||||
).first()
|
||||
|
||||
if existing_channel:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_409_CONFLICT,
|
||||
detail="Channel with same group_title and name already exists"
|
||||
)
|
||||
|
||||
db_channel = ChannelDB(**channel.model_dump())
|
||||
db.add(db_channel)
|
||||
db.commit()
|
||||
db.refresh(db_channel)
|
||||
return db_channel
|
||||
|
||||
@router.get("/{tvg_id}", response_model=ChannelResponse)
|
||||
@router.get("/{channel_id}", response_model=ChannelResponse)
|
||||
def get_channel(
|
||||
tvg_id: str,
|
||||
channel_id: UUID,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""Get a channel by tvg_id"""
|
||||
channel = db.query(ChannelDB).filter(ChannelDB.tvg_id == tvg_id).first()
|
||||
"""Get a channel by id"""
|
||||
channel = db.query(ChannelDB).filter(ChannelDB.id == channel_id).first()
|
||||
if not channel:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
@@ -40,21 +56,36 @@ def get_channel(
|
||||
)
|
||||
return channel
|
||||
|
||||
@router.put("/{tvg_id}", response_model=ChannelResponse)
|
||||
@router.put("/{channel_id}", response_model=ChannelResponse)
|
||||
@require_roles("admin")
|
||||
def update_channel(
|
||||
tvg_id: str,
|
||||
channel: ChannelCreate,
|
||||
channel_id: UUID,
|
||||
channel: ChannelUpdate,
|
||||
db: Session = Depends(get_db),
|
||||
user: CognitoUser = Depends(get_current_user)
|
||||
):
|
||||
"""Update a channel"""
|
||||
db_channel = db.query(ChannelDB).filter(ChannelDB.tvg_id == tvg_id).first()
|
||||
db_channel = db.query(ChannelDB).filter(ChannelDB.id == channel_id).first()
|
||||
if not db_channel:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Channel not found"
|
||||
)
|
||||
|
||||
# Check for duplicate channel (same group_title + name) excluding current channel
|
||||
existing_channel = db.query(ChannelDB).filter(
|
||||
and_(
|
||||
ChannelDB.group_title == channel.group_title,
|
||||
ChannelDB.name == channel.name,
|
||||
ChannelDB.id != channel_id
|
||||
)
|
||||
).first()
|
||||
|
||||
if existing_channel:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_409_CONFLICT,
|
||||
detail="Channel with same group_title and name already exists"
|
||||
)
|
||||
|
||||
for key, value in channel.model_dump().items():
|
||||
setattr(db_channel, key, value)
|
||||
@@ -63,15 +94,15 @@ def update_channel(
|
||||
db.refresh(db_channel)
|
||||
return db_channel
|
||||
|
||||
@router.delete("/{tvg_id}", status_code=status.HTTP_204_NO_CONTENT)
|
||||
@router.delete("/{channel_id}", status_code=status.HTTP_204_NO_CONTENT)
|
||||
@require_roles("admin")
|
||||
def delete_channel(
|
||||
tvg_id: str,
|
||||
channel_id: UUID,
|
||||
db: Session = Depends(get_db),
|
||||
user: CognitoUser = Depends(get_current_user)
|
||||
):
|
||||
"""Delete a channel"""
|
||||
channel = db.query(ChannelDB).filter(ChannelDB.tvg_id == tvg_id).first()
|
||||
channel = db.query(ChannelDB).filter(ChannelDB.id == channel_id).first()
|
||||
if not channel:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
|
||||
Reference in New Issue
Block a user