Refactor user CRUD functions
This commit is contained in:
parent
fa9b7265ea
commit
a8e620d89c
1 changed files with 6 additions and 7 deletions
|
|
@ -1,22 +1,21 @@
|
|||
from datetime import datetime, timezone
|
||||
from typing import Union
|
||||
from typing import Optional, Union
|
||||
from uuid import UUID
|
||||
|
||||
from fastapi import Depends, HTTPException, status
|
||||
from langflow.services.database.models.user.model import User, UserUpdate
|
||||
from langflow.services.deps import get_session
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlmodel import Session
|
||||
from typing import Optional
|
||||
|
||||
from sqlalchemy.orm.attributes import flag_modified
|
||||
from sqlmodel import Session, select
|
||||
|
||||
|
||||
def get_user_by_username(db: Session, username: str) -> Union[User, None]:
|
||||
return db.query(User).filter(User.username == username).first()
|
||||
return db.exec(select(User).where(User.username == username)).first()
|
||||
|
||||
|
||||
def get_user_by_id(db: Session, id: UUID) -> Union[User, None]:
|
||||
return db.query(User).filter(User.id == id).first()
|
||||
return db.exec(select(User).where(User.id == id)).first()
|
||||
|
||||
|
||||
def update_user(user_db: Optional[User], user: UserUpdate, db: Session = Depends(get_session)) -> User:
|
||||
|
|
@ -27,7 +26,7 @@ def update_user(user_db: Optional[User], user: UserUpdate, db: Session = Depends
|
|||
# if user_db_by_username and user_db_by_username.id != user_id:
|
||||
# raise HTTPException(status_code=409, detail="Username already exists")
|
||||
|
||||
user_data = user.dict(exclude_unset=True)
|
||||
user_data = user.model_dump(exclude_unset=True)
|
||||
changed = False
|
||||
for attr, value in user_data.items():
|
||||
if hasattr(user_db, attr) and value is not None:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue