JourneyPoint / journeypoint / app / api / routes / useradditional.py
useradditional.py
Raw
from fastapi import APIRouter, Depends, HTTPException, UploadFile, File
from sqlalchemy.orm import Session
from app.api.dependencies import get_db
from app.models.useradditional import UserProfile
from app.schemas.useradditional import UserProfileCreate, UserProfileResponse
from app.s3_utils import upload_image_to_s3

router = APIRouter()


@router.put("/useradditional", response_model=UserProfileResponse)
async def create_user_profile(
        username: str,
        bio: str = None,
        profile_picture: UploadFile = File(None),
        db: Session = Depends(get_db)
):

    existing_profile = db.query(UserProfile).filter(UserProfile.username == username).first()
    s3_url = ""
    if existing_profile:
        existing_profile = UserProfile(username=username, bio=bio or None)
        if profile_picture:
            s3_url = upload_image_to_s3(profile_picture, profile_picture.filename)
        existing_profile.profile_picture = s3_url

        db.add(existing_profile)
        db.commit()
        db.refresh(existing_profile)
        return existing_profile

    else:
        new_profile = UserProfile(username=username, bio=bio or None)
        if profile_picture:
            s3_url = upload_image_to_s3(profile_picture, profile_picture.filename)
            new_profile.profile_picture = s3_url

        db.add(new_profile)
        db.commit()
        db.refresh(new_profile)
        return new_profile


@router.get("/useradditional", response_model=UserProfileResponse)
def get_user_profile(username: str, db: Session = Depends(get_db)):
    profile = db.query(UserProfile).filter(UserProfile.username == username).first()
    if not profile:
        raise HTTPException(status_code=404, detail="Profile not found")
    return profile