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

36 lines
1.2 KiB
Python
Raw Normal View History

from sqlalchemy.orm import Session
from models import Organization
class OrganizationService:
def __init__(self, db: Session):
self.db = db
def create_organization(self, name: str, username: str, full_name: str, email: str, public_key: str) -> Organization:
from services import UserService
user = UserService(self.db).get_user_by_username(username)
if not user:
user = UserService(self.db).create_user(username, full_name, email, public_key)
organization = Organization(
name=name,
owner_id=user.id,
owner=user
)
self.db.add(organization)
self.db.commit()
self.db.refresh(organization)
UserService(self.db).add_org_to_user(user, organization)
return organization
def get_organization(self, org_id: int) -> Organization | None:
return self.db.query(Organization).filter(Organization.id == org_id).first()
def get_organization_by_name(self, name: str) -> Organization | None:
return self.db.query(Organization).filter(Organization.name == name).first()
def list_organizations(self):
return self.db.query(Organization).all()