Add delete functionality for event themes
Introduce a new endpoint to delete event themes, supporting both soft and hard deletes. Hard deletes are restricted to superusers, while soft deletes deactivate the theme. Also, improve error handling and user permission checks for this operation.
This commit is contained in:
@@ -2,11 +2,14 @@
|
||||
from typing import List
|
||||
from uuid import UUID
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from fastapi import APIRouter, Depends, HTTPException, status, Query
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.api.dependencies.auth import get_current_user
|
||||
from app.core.database import get_db
|
||||
from app.crud.event_theme import event_theme
|
||||
from app.crud.event_theme import event_theme as event_theme_crud
|
||||
from app.models import User
|
||||
from app.schemas.event_themes import EventThemeCreate, EventThemeResponse, EventThemeUpdate
|
||||
|
||||
router = APIRouter()
|
||||
@@ -19,7 +22,7 @@ def create_theme(
|
||||
theme_in: EventThemeCreate
|
||||
) -> EventThemeResponse:
|
||||
"""Create new event theme."""
|
||||
theme = event_theme.create(db, obj_in=theme_in)
|
||||
theme = event_theme_crud.create(db, obj_in=theme_in)
|
||||
print(theme)
|
||||
return theme
|
||||
|
||||
@@ -31,7 +34,7 @@ def list_themes(
|
||||
limit: int = 100
|
||||
) -> List[EventThemeResponse]:
|
||||
"""List event themes."""
|
||||
themes = event_theme.get_multi(db, skip=skip, limit=limit)
|
||||
themes = event_theme_crud.get_multi(db, skip=skip, limit=limit)
|
||||
return themes
|
||||
|
||||
|
||||
@@ -42,7 +45,7 @@ def get_theme(
|
||||
theme_id: UUID
|
||||
) -> EventThemeResponse:
|
||||
"""Get specific theme by ID."""
|
||||
theme = event_theme.get(db, id=theme_id)
|
||||
theme = event_theme_crud.get(db, id=theme_id)
|
||||
if not theme:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
@@ -50,6 +53,7 @@ def get_theme(
|
||||
)
|
||||
return theme
|
||||
|
||||
|
||||
@router.patch("/{theme_id}", response_model=EventThemeResponse, operation_id="update_event_theme")
|
||||
def update_theme(
|
||||
*,
|
||||
@@ -58,11 +62,64 @@ def update_theme(
|
||||
theme_in: EventThemeUpdate
|
||||
) -> EventThemeResponse:
|
||||
"""Update specific theme by ID."""
|
||||
theme = event_theme.get(db, id=theme_id)
|
||||
theme = event_theme_crud.get(db, id=theme_id)
|
||||
if not theme:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Theme not found"
|
||||
)
|
||||
theme = event_theme.update(db, db_obj=theme, obj_in=theme_in)
|
||||
theme = event_theme_crud.update(db, db_obj=theme, obj_in=theme_in)
|
||||
return theme
|
||||
|
||||
|
||||
@router.delete("/{theme_id}", operation_id="delete_event_theme")
|
||||
def delete_theme(
|
||||
*,
|
||||
db: Session = Depends(get_db),
|
||||
theme_id: UUID,
|
||||
current_user: User = Depends(get_current_user),
|
||||
hard_delete: bool = Query(False, description="Perform hard delete instead of soft delete")
|
||||
|
||||
):
|
||||
"""Delete specific theme by ID."""
|
||||
if current_user is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Invalid authentication credentials",
|
||||
headers={"WWW-Authenticate": "Bearer"},
|
||||
)
|
||||
|
||||
try:
|
||||
event_theme_obj = event_theme_crud.get(db=db, id=theme_id)
|
||||
if not event_theme_obj:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Event theme not found"
|
||||
)
|
||||
|
||||
# Only creator or superuser can delete
|
||||
if not current_user.is_superuser:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Not enough permissions to delete this event theme"
|
||||
)
|
||||
|
||||
if hard_delete:
|
||||
# Hard delete - only for superusers
|
||||
if not current_user.is_superuser:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Only administrators can perform hard delete"
|
||||
)
|
||||
event_theme_crud.remove(db=db, id=theme_id)
|
||||
else:
|
||||
# Soft delete - set is_active to False
|
||||
event_theme_crud.update(db=db, db_obj=event_theme_obj, obj_in={"is_active": False})
|
||||
|
||||
return None # 204 No Content
|
||||
except SQLAlchemyError:
|
||||
db.rollback()
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail="Error deleting event"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user