Files
card-game/backend/app/routers/profiles.py

49 lines
2.0 KiB
Python

from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.orm import Session
from app import schemas, models, database
from typing import List
router = APIRouter(
prefix="/profiles",
tags=["profiles"],
)
@router.get("/{nickname}", response_model=schemas.UserProfilePublic)
def get_public_profile(nickname: str, db: Session = Depends(database.get_db)):
profile = db.query(models.UserProfile).filter(models.UserProfile.nickname == nickname).first()
if not profile:
raise HTTPException(status_code=404, detail="User not found")
# We return the profile directly, which matches UserProfilePublic schema (nickname).
# But UserProfile model has created_at?
# Let's check model. UserProfile has keys: user_id, nickname, created_at?
# Actually models.UserProfile might not have created_at. Let's check.
# If not, we should rely on User.created_at or add it.
# For now assuming UserProfile has what we need or we fetch from User.
return profile
@router.get("/{nickname}/collection", response_model=List[schemas.UserCardResponse])
def get_public_collection(nickname: str, db: Session = Depends(database.get_db)):
# 1. Get User ID from nickname
profile = db.query(models.UserProfile).filter(models.UserProfile.nickname == nickname).first()
if not profile:
raise HTTPException(status_code=404, detail="User not found")
# 2. Get Collection
# Note: Privacy check would go here.
cards = db.query(models.UserCard).filter(models.UserCard.user_id == profile.user_id).all()
if not cards:
return []
# We need to reshape to UserCardResponse (card_name, rarity, obtained_at)
# models.UserCard has 'card' relationship.
response = []
for user_card in cards:
response.append({
"card_id": user_card.card_id,
"card_name": user_card.card.name,
"rarity": user_card.card.rarity,
"obtained_at": user_card.obtained_at
})
return response