diff --git a/backend/routers/users.py b/backend/routers/users.py index a1afd86..fc35bb7 100644 --- a/backend/routers/users.py +++ b/backend/routers/users.py @@ -3,8 +3,8 @@ from fastapi import APIRouter, Depends, HTTPException, Query from sqlmodel import Session, select, func from pydantic import BaseModel from database import get_session -from models import User, Review, Attendance, Group, GroupMember, Show -from schemas import UserRead, ReviewRead, ShowRead, GroupRead +from models import User, Review, Attendance, Group, GroupMember, Show, UserPreferences, Profile +from schemas import UserRead, ReviewRead, ShowRead, GroupRead, UserPreferencesUpdate from auth import get_current_user router = APIRouter(prefix="/users", tags=["users"]) @@ -12,6 +12,8 @@ router = APIRouter(prefix="/users", tags=["users"]) class UserProfileUpdate(BaseModel): bio: Optional[str] = None avatar: Optional[str] = None + username: Optional[str] = None + display_name: Optional[str] = None @router.get("/{user_id}", response_model=UserRead) def get_user_public(user_id: int, session: Session = Depends(get_session)): @@ -27,17 +29,85 @@ def update_my_profile( current_user: User = Depends(get_current_user), session: Session = Depends(get_session) ): - """Update current user's bio and avatar""" + """Update current user's bio, avatar, and primary profile""" if update.bio is not None: current_user.bio = update.bio if update.avatar is not None: current_user.avatar = update.avatar + if update.username or update.display_name: + # Find or create primary profile + query = select(Profile).where(Profile.user_id == current_user.id) + profile = session.exec(query).first() + + if not profile: + if not update.username: + raise HTTPException(status_code=400, detail="Username required for new profile") + + # Check uniqueness (naive check) + existing = session.exec(select(Profile).where(Profile.username == update.username)).first() + if existing: + raise HTTPException(status_code=400, detail="Username taken") + + profile = Profile( + user_id=current_user.id, + username=update.username, + display_name=update.display_name or update.username + ) + session.add(profile) + else: + if update.username: + # Check uniqueness if changing + if update.username != profile.username: + existing = session.exec(select(Profile).where(Profile.username == update.username)).first() + if existing: + raise HTTPException(status_code=400, detail="Username taken") + profile.username = update.username + + if update.display_name: + profile.display_name = update.display_name + + session.add(profile) + session.add(current_user) session.commit() session.refresh(current_user) return current_user +@router.patch("/me/preferences", response_model=UserPreferencesUpdate) +def update_preferences( + prefs: UserPreferencesUpdate, + current_user: User = Depends(get_current_user), + session: Session = Depends(get_session) +): + # Find or create + if not current_user.preferences: + # Need to create + db_prefs = UserPreferences(user_id=current_user.id) + current_user.preferences = db_prefs # Link it? + # Actually, if relation is set up, adding to session should work. + # But safest to create explicitly. + db_prefs = UserPreferences( + user_id=current_user.id, + wiki_mode=prefs.wiki_mode if prefs.wiki_mode is not None else False, + show_ratings=prefs.show_ratings if prefs.show_ratings is not None else True, + show_comments=prefs.show_comments if prefs.show_comments is not None else True + ) + session.add(db_prefs) + else: + db_prefs = current_user.preferences + if prefs.wiki_mode is not None: + db_prefs.wiki_mode = prefs.wiki_mode + if prefs.show_ratings is not None: + db_prefs.show_ratings = prefs.show_ratings + if prefs.show_comments is not None: + db_prefs.show_comments = prefs.show_comments + session.add(db_prefs) + + session.commit() + session.refresh(db_prefs) + return db_prefs + # --- User Stats --- @router.get("/{user_id}/stats")