import uuid
from logging import getLogger
from typing import Optional

from fastapi import UploadFile
from sqlmodel import select, Session

from .models import User

logger = getLogger("uvicorn.error")


def get_user(username: str, session: Session) -> Optional[User]:
    statement = select(User).where(User.username == username)
    user = session.exec(statement).first()
    if user:
        return user
    return None


def save_file(file: UploadFile) -> str:
    filename = uuid.uuid4()
    path = f"static/{filename}"
    with open(path, "wb") as f:
        content = file.file.read()
        f.write(content)

    return path

logging.getLogger uuid.uuid4 sqlmodel.select
[((175, 201), 'logging.getLogger', 'getLogger', (['"""uvicorn.error"""'], {}), "('uvicorn.error')\n", (184, 201), False, 'from logging import getLogger\n'), ((480, 492), 'uuid.uuid4', 'uuid.uuid4', ([], {}), '()\n', (490, 492), False, 'import uuid\n'), ((285, 297), 'sqlmodel.select', 'select', (['User'], {}), '(User)\n', (291, 297), False, 'from sqlmodel import select, Session\n')]