2024-11-13 02:49:43 +00:00
|
|
|
from database import db
|
2024-11-12 01:33:31 +00:00
|
|
|
from models import User, Organization
|
|
|
|
|
|
|
|
|
|
|
|
class UserService:
|
2024-11-13 02:49:43 +00:00
|
|
|
@staticmethod
|
|
|
|
def create_user(username: str, full_name: str, email: str, public_key: str, org: Organization = None) -> User:
|
2024-11-12 01:33:31 +00:00
|
|
|
user = User(
|
|
|
|
username=username,
|
|
|
|
full_name=full_name,
|
|
|
|
email=email,
|
2024-11-15 01:18:23 +00:00
|
|
|
public_keys={org.id: public_key} if org else {},
|
2024-11-12 01:33:31 +00:00
|
|
|
orgs=[org] if org else []
|
|
|
|
)
|
2024-11-13 02:49:43 +00:00
|
|
|
db.add(user)
|
|
|
|
db.commit()
|
|
|
|
db.refresh(user)
|
2024-11-12 01:33:31 +00:00
|
|
|
return user
|
|
|
|
|
2024-11-13 02:49:43 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_user(user_id: int) -> User | None:
|
|
|
|
return db.query(User).filter(User.id == user_id).first()
|
2024-11-12 01:33:31 +00:00
|
|
|
|
2024-11-13 02:49:43 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_user_by_username(username: str) -> User | None:
|
|
|
|
return db.query(User).filter(User.username == username).first()
|
2024-11-12 01:33:31 +00:00
|
|
|
|
2024-11-13 02:49:43 +00:00
|
|
|
@staticmethod
|
|
|
|
def add_org_to_user(user: User, org: Organization) -> User:
|
2024-11-12 01:33:31 +00:00
|
|
|
user.orgs.append(org)
|
2024-11-13 02:49:43 +00:00
|
|
|
db.commit()
|
|
|
|
db.refresh(user)
|
2024-11-15 01:18:23 +00:00
|
|
|
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)
|
2024-11-12 01:33:31 +00:00
|
|
|
return user
|