42 lines
1.2 KiB
Python
42 lines
1.2 KiB
Python
from database import db
|
|
from models import User, Organization
|
|
|
|
|
|
class UserService:
|
|
@staticmethod
|
|
def create_user(username: str, full_name: str, email: str, public_key: str, org: Organization = None) -> User:
|
|
user = User(
|
|
username=username,
|
|
full_name=full_name,
|
|
email=email,
|
|
public_keys={org.id: public_key} if org else {},
|
|
orgs=[org] if org else []
|
|
)
|
|
db.add(user)
|
|
db.commit()
|
|
db.refresh(user)
|
|
return user
|
|
|
|
@staticmethod
|
|
def get_user(user_id: int) -> User | None:
|
|
return db.query(User).filter(User.id == user_id).first()
|
|
|
|
@staticmethod
|
|
def get_user_by_username(username: str) -> User | None:
|
|
return db.query(User).filter(User.username == username).first()
|
|
|
|
@staticmethod
|
|
def add_org_to_user(user: User, org: Organization) -> User:
|
|
user.orgs.append(org)
|
|
db.commit()
|
|
db.refresh(user)
|
|
return user
|
|
|
|
@staticmethod
|
|
def add_public_key_to_user(user: User, org: Organization, public_key: str) -> User:
|
|
public_keys = user.public_keys.copy()
|
|
public_keys[org.id] = public_key
|
|
user.public_keys = public_keys
|
|
db.commit()
|
|
db.refresh(user)
|
|
return user |