33 lines
954 B
Python
33 lines
954 B
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_key=public_key,
|
|
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 |