|
| 1 | +import uuid |
| 2 | +from typing import Any, List |
| 3 | + |
| 4 | +from fastapi import APIRouter, Depends, HTTPException |
| 5 | +from sqlmodel import Session |
| 6 | + |
| 7 | +from app import crud, models # Removed schemas |
| 8 | +from app.api import deps |
| 9 | + |
| 10 | +router = APIRouter() |
| 11 | + |
| 12 | +# Helper function to check if user is participant or creator of the event associated with a speech |
| 13 | +def check_event_access_for_speech(db: Session, speech_id: uuid.UUID, user: models.User) -> models.SecretSpeech: |
| 14 | + speech = crud.get_speech(session=db, speech_id=speech_id) |
| 15 | + if not speech: |
| 16 | + raise HTTPException(status_code=404, detail="Speech not found") |
| 17 | + |
| 18 | + event = crud.get_event(session=db, event_id=speech.event_id) |
| 19 | + if not event: |
| 20 | + raise HTTPException(status_code=404, detail="Associated event not found") # Should not happen if DB is consistent |
| 21 | + |
| 22 | + is_participant = any(p.user_id == user.id for p in event.participants) |
| 23 | + if not is_participant and event.creator_id != user.id: |
| 24 | + raise HTTPException(status_code=403, detail="User does not have access to the event of this speech") |
| 25 | + return speech |
| 26 | + |
| 27 | +# Helper function to check if user is participant or creator of an event |
| 28 | +def check_event_membership(db: Session, event_id: uuid.UUID, user: models.User) -> models.CoordinationEvent: |
| 29 | + event = crud.get_event(session=db, event_id=event_id) |
| 30 | + if not event: |
| 31 | + raise HTTPException(status_code=404, detail="Event not found") |
| 32 | + |
| 33 | + is_participant = any(p.user_id == user.id for p in event.participants) |
| 34 | + if not is_participant and event.creator_id != user.id: |
| 35 | + raise HTTPException(status_code=403, detail="User must be a participant or creator of the event") |
| 36 | + return event |
| 37 | + |
| 38 | + |
| 39 | +@router.post("/", response_model=models.SecretSpeechPublic, status_code=201) |
| 40 | +def create_speech( |
| 41 | + *, |
| 42 | + db: Session = Depends(deps.get_db), |
| 43 | + speech_in: models.SecretSpeechWithInitialVersionCreate, # Use the new combined schema |
| 44 | + current_user: deps.CurrentUser, |
| 45 | +) -> Any: |
| 46 | + """ |
| 47 | + Create a new secret speech. The current user will be set as the creator. |
| 48 | + An initial version of the speech is created with the provided draft. |
| 49 | + User must be a participant of the specified event. |
| 50 | + """ |
| 51 | + # Check if user has access to the event |
| 52 | + event = check_event_membership(db=db, event_id=speech_in.event_id, user=current_user) |
| 53 | + if not event: # Should be handled by check_event_membership raising HTTPException |
| 54 | + raise HTTPException(status_code=404, detail="Event not found or user not participant.") |
| 55 | + |
| 56 | + # The SecretSpeechCreate model is currently empty, so we pass an instance. |
| 57 | + # The actual speech metadata (event_id, creator_id) are passed directly to crud.create_speech |
| 58 | + db_speech = crud.create_speech( |
| 59 | + session=db, |
| 60 | + speech_in=models.SecretSpeechCreate(), # Pass empty base model if no direct fields |
| 61 | + event_id=speech_in.event_id, |
| 62 | + creator_id=current_user.id, |
| 63 | + initial_draft=speech_in.initial_speech_draft, |
| 64 | + initial_tone=speech_in.initial_speech_tone, |
| 65 | + initial_duration=speech_in.initial_estimated_duration_minutes, |
| 66 | + ) |
| 67 | + return db_speech |
| 68 | + |
| 69 | + |
| 70 | +@router.get("/event/{event_id}", response_model=List[models.SecretSpeechPublic]) |
| 71 | +def list_event_speeches( |
| 72 | + *, |
| 73 | + db: Session = Depends(deps.get_db), |
| 74 | + event_id: uuid.UUID, |
| 75 | + current_user: deps.CurrentUser, |
| 76 | +) -> Any: |
| 77 | + """ |
| 78 | + Get all speeches for a given event. User must be a participant of the event. |
| 79 | + """ |
| 80 | + check_event_membership(db=db, event_id=event_id, user=current_user) |
| 81 | + speeches = crud.get_event_speeches(session=db, event_id=event_id) |
| 82 | + return speeches |
| 83 | + |
| 84 | + |
| 85 | +@router.get("/{speech_id}", response_model=models.SecretSpeechPublic) # Consider a more detailed model for owner |
| 86 | +def get_speech_details( |
| 87 | + *, |
| 88 | + db: Session = Depends(deps.get_db), |
| 89 | + speech_id: uuid.UUID, |
| 90 | + current_user: deps.CurrentUser, |
| 91 | +) -> Any: |
| 92 | + """ |
| 93 | + Get a specific speech. User must have access to the event of this speech. |
| 94 | + If the user is the creator of the speech, they might get more details |
| 95 | + (e.g. draft of the current version - this needs handling in response shaping). |
| 96 | + """ |
| 97 | + speech = check_event_access_for_speech(db=db, speech_id=speech_id, user=current_user) |
| 98 | + # Basic SecretSpeechPublic doesn't include version details. |
| 99 | + # If we want to embed current version, we'd fetch it and combine. |
| 100 | + # For now, returning speech metadata. API consumer can fetch versions separately. |
| 101 | + return speech |
| 102 | + |
| 103 | + |
| 104 | +@router.post("/{speech_id}/versions", response_model=models.SecretSpeechVersionPublic, status_code=201) |
| 105 | +def create_speech_version( |
| 106 | + *, |
| 107 | + db: Session = Depends(deps.get_db), |
| 108 | + speech_id: uuid.UUID, |
| 109 | + version_in: models.SecretSpeechVersionCreate, |
| 110 | + current_user: deps.CurrentUser, |
| 111 | +) -> Any: |
| 112 | + """ |
| 113 | + Create a new version for a secret speech. |
| 114 | + User must be the creator of the speech or a participant in the event (adjust as needed). |
| 115 | + """ |
| 116 | + speech = crud.get_speech(session=db, speech_id=speech_id) |
| 117 | + if not speech: |
| 118 | + raise HTTPException(status_code=404, detail="Speech not found") |
| 119 | + |
| 120 | + # Permission: only speech creator can add versions |
| 121 | + if speech.creator_id != current_user.id: |
| 122 | + # Or, check event participation if that's the rule: |
| 123 | + # check_event_access_for_speech(db=db, speech_id=speech_id, user=current_user) |
| 124 | + raise HTTPException(status_code=403, detail="Only the speech creator can add new versions.") |
| 125 | + |
| 126 | + new_version = crud.create_speech_version( |
| 127 | + session=db, version_in=version_in, speech_id=speech_id, creator_id=current_user.id |
| 128 | + ) |
| 129 | + return new_version |
| 130 | + |
| 131 | + |
| 132 | +@router.get("/{speech_id}/versions", response_model=List[models.SecretSpeechVersionPublic]) |
| 133 | +def list_speech_versions( |
| 134 | + *, |
| 135 | + db: Session = Depends(deps.get_db), |
| 136 | + speech_id: uuid.UUID, |
| 137 | + current_user: deps.CurrentUser, |
| 138 | +) -> Any: |
| 139 | + """ |
| 140 | + List all versions of a speech. |
| 141 | + If current user is speech creator, they see full details (including draft). |
| 142 | + Otherwise, they see the public version (no draft). |
| 143 | + """ |
| 144 | + speech = check_event_access_for_speech(db=db, speech_id=speech_id, user=current_user) |
| 145 | + versions = crud.get_speech_versions(session=db, speech_id=speech_id) |
| 146 | + |
| 147 | + public_versions = [] |
| 148 | + for v in versions: |
| 149 | + if speech.creator_id == current_user.id or v.creator_id == current_user.id: # Speech creator or version creator sees draft |
| 150 | + public_versions.append(models.SecretSpeechVersionDetailPublic.model_validate(v)) |
| 151 | + else: |
| 152 | + public_versions.append(models.SecretSpeechVersionPublic.model_validate(v)) |
| 153 | + return public_versions |
| 154 | + |
| 155 | + |
| 156 | +@router.put("/{speech_id}/versions/{version_id}/set-current", response_model=models.SecretSpeechPublic) |
| 157 | +def set_current_speech_version( |
| 158 | + *, |
| 159 | + db: Session = Depends(deps.get_db), |
| 160 | + speech_id: uuid.UUID, |
| 161 | + version_id: uuid.UUID, |
| 162 | + current_user: deps.CurrentUser, |
| 163 | +) -> Any: |
| 164 | + """ |
| 165 | + Set a specific version of a speech as the current one. |
| 166 | + User must be the creator of the speech. |
| 167 | + """ |
| 168 | + speech = crud.get_speech(session=db, speech_id=speech_id) |
| 169 | + if not speech: |
| 170 | + raise HTTPException(status_code=404, detail="Speech not found") |
| 171 | + if speech.creator_id != current_user.id: |
| 172 | + raise HTTPException(status_code=403, detail="Only the speech creator can set the current version.") |
| 173 | + |
| 174 | + updated_speech = crud.set_current_speech_version( |
| 175 | + session=db, speech_id=speech_id, version_id=version_id |
| 176 | + ) |
| 177 | + if not updated_speech: |
| 178 | + raise HTTPException(status_code=404, detail="Version not found or does not belong to this speech.") |
| 179 | + return updated_speech |
0 commit comments