sio-2425/delivery1/server/services/users.py

32 lines
1011 B
Python
Raw Normal View History

from sqlalchemy.orm import Session
from models import User, Organization
class UserService:
def __init__(self, db: Session):
self.db = db
def create_user(self, 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 []
)
self.db.add(user)
self.db.commit()
self.db.refresh(user)
return user
def get_user(self, user_id: int) -> User | None:
return self.db.query(User).filter(User.id == user_id).first()
def get_user_by_username(self, username: str) -> User | None:
return self.db.query(User).filter(User.username == username).first()
def add_org_to_user(self, user: User, org: Organization) -> User:
user.orgs.append(org)
self.db.commit()
self.db.refresh(user)
return user